Merge branch 'development' of https://github.com/WDI-Ideas/goodtimes into development
This commit is contained in:
@@ -107,6 +107,12 @@ class EventListSerializer(serializers.ModelSerializer):
|
||||
# "draft",
|
||||
]
|
||||
|
||||
# def to_representation(self, instance):
|
||||
# """Customize the representation of the instance."""
|
||||
# representation = super().to_representation(instance)
|
||||
# representation['key_guest'] = instance.get_key_guests() # Return as a list
|
||||
# return representation
|
||||
|
||||
|
||||
class EventDetailSerializer(serializers.ModelSerializer):
|
||||
tags = TagSerializer(many=True, read_only=True)
|
||||
@@ -178,6 +184,12 @@ class EventDetailSerializer(serializers.ModelSerializer):
|
||||
"status_display": interaction.get_status_display(),
|
||||
}
|
||||
return None
|
||||
|
||||
# def to_representation(self, instance):
|
||||
# """Customize the representation of the instance."""
|
||||
# representation = super().to_representation(instance)
|
||||
# representation['key_guest'] = instance.get_key_guests() # Return as a list
|
||||
# return representation
|
||||
|
||||
|
||||
class CreateEventSerializer(serializers.ModelSerializer):
|
||||
@@ -212,9 +224,16 @@ class CreateEventSerializer(serializers.ModelSerializer):
|
||||
"coupon_description",
|
||||
]
|
||||
|
||||
def validate_key_guest(self, value):
|
||||
if value and not isinstance(value, str):
|
||||
raise serializers.ValidationError("key_guest must be a string")
|
||||
return value
|
||||
|
||||
def create(self, validated_data):
|
||||
tags = validated_data.pop("tags", None)
|
||||
images_data = validated_data.pop("images", None)
|
||||
key_guest = validated_data.pop("key_guest", None)
|
||||
|
||||
event = Event.objects.create(**validated_data)
|
||||
|
||||
if tags:
|
||||
@@ -224,11 +243,15 @@ class CreateEventSerializer(serializers.ModelSerializer):
|
||||
for image_data in images_data:
|
||||
EventImage.objects.create(event=event, image=image_data)
|
||||
|
||||
if key_guest:
|
||||
event.set_key_guests(key_guest)
|
||||
event.save()
|
||||
return event
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
tags = validated_data.pop("tags", None)
|
||||
images_data = validated_data.pop("images", None)
|
||||
key_guest = validated_data.pop("key_guest", None)
|
||||
|
||||
# Update fields if there is any change.
|
||||
if tags is not None:
|
||||
@@ -241,6 +264,9 @@ class CreateEventSerializer(serializers.ModelSerializer):
|
||||
for image_data in images_data:
|
||||
EventImage.objects.create(event=instance, image=image_data)
|
||||
|
||||
if key_guest is not None:
|
||||
instance.set_key_guests(key_guest)
|
||||
|
||||
# Update other fields
|
||||
for attr, value in validated_data.items():
|
||||
setattr(instance, attr, value)
|
||||
@@ -248,6 +274,12 @@ class CreateEventSerializer(serializers.ModelSerializer):
|
||||
|
||||
return instance
|
||||
|
||||
# def to_representation(self, instance):
|
||||
# """Customize the representation of the instance."""
|
||||
# representation = super().to_representation(instance)
|
||||
# representation['key_guest'] = instance.get_key_guests() # Return as a list
|
||||
# return representation
|
||||
|
||||
|
||||
class CreateVenueSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
|
||||
@@ -129,6 +129,19 @@ class Event(BaseModel):
|
||||
self.social_media_shares_count += 1
|
||||
self.save()
|
||||
|
||||
def set_key_guests(self, guests):
|
||||
"""Set the key guests as a comma-seperated string."""
|
||||
if isinstance(guests, list):
|
||||
self.key_guest = ",".join(guests)
|
||||
elif isinstance(guests, str):
|
||||
self.key_guest = guests
|
||||
else:
|
||||
raise ValueError("Guests must be a comma-seperated string")
|
||||
|
||||
def get_key_guests(self):
|
||||
"""Return the key guests as a list of strings."""
|
||||
return self.key_guest.split(",") if self.key_guest else []
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
||||
Reference in New Issue
Block a user