mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-19 12:59:36 +02:00
transportation
This commit is contained in:
parent
66a2e30711
commit
ae9a7f5479
7 changed files with 155 additions and 6 deletions
|
@ -1,5 +1,5 @@
|
|||
import os
|
||||
from .models import Adventure, Collection
|
||||
from .models import Adventure, Collection, Transportation
|
||||
from rest_framework import serializers
|
||||
|
||||
class AdventureSerializer(serializers.ModelSerializer):
|
||||
|
@ -31,5 +31,38 @@ class CollectionSerializer(serializers.ModelSerializer):
|
|||
# fields are all plus the adventures field
|
||||
fields = ['id', 'description', 'user_id', 'name', 'is_public', 'adventures', 'created_at', 'start_date', 'end_date']
|
||||
|
||||
class TransportationSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Transportation
|
||||
fields = [
|
||||
'id', 'user_id', 'type', 'name', 'description', 'rating',
|
||||
'link', 'date', 'flight_number', 'from_location', 'to_location',
|
||||
'is_public', 'collection', 'collection_id', 'created_at', 'updated_at'
|
||||
]
|
||||
read_only_fields = ['id', 'created_at', 'updated_at']
|
||||
|
||||
def validate(self, data):
|
||||
# Check if the collection is public and the transportation is not
|
||||
collection = data.get('collection')
|
||||
is_public = data.get('is_public', False)
|
||||
if collection and collection.is_public and not is_public:
|
||||
raise serializers.ValidationError(
|
||||
'Transportations associated with a public collection must be public.'
|
||||
)
|
||||
|
||||
# Check if the user owns the collection
|
||||
request = self.context.get('request')
|
||||
if request and collection and collection.user_id != request.user:
|
||||
raise serializers.ValidationError(
|
||||
'Transportations must be associated with collections owned by the same user.'
|
||||
)
|
||||
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
# Set the user_id to the current user
|
||||
validated_data['user_id'] = self.context['request'].user
|
||||
return super().create(validated_data)
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue