Fixes in event handling
This commit is contained in:
@@ -9,8 +9,8 @@ class ParticipationSerializer(serializers.Serializer):
|
||||
|
||||
event = serializers.PrimaryKeyRelatedField(queryset=Event.objects.all())
|
||||
user = serializers.CharField()
|
||||
status = serializers.CharField(required=False, default="-")
|
||||
comment = serializers.CharField(required=False, allow_blank=True, default="")
|
||||
status = serializers.CharField(required=False)
|
||||
comment = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
def to_representation(self, instance):
|
||||
"""Serialize an EventParticipation instance."""
|
||||
@@ -32,14 +32,19 @@ class ParticipationSerializer(serializers.Serializer):
|
||||
"""Create or update EventParticipation based on event and user."""
|
||||
event = validated_data.get("event")
|
||||
user = validated_data.get("user")
|
||||
status = validated_data.get("status", "-")
|
||||
comment = validated_data.get("comment", "")
|
||||
|
||||
# Use update_or_create to handle both new and existing participations
|
||||
# Only include fields that were actually present in the request,
|
||||
# so a status-only update doesn't wipe the comment (and vice versa).
|
||||
defaults = {}
|
||||
if "status" in validated_data:
|
||||
defaults["status"] = validated_data["status"]
|
||||
if "comment" in validated_data:
|
||||
defaults["comment"] = validated_data["comment"]
|
||||
|
||||
participation, created = EventParticipation.objects.update_or_create(
|
||||
event=event,
|
||||
user=user,
|
||||
defaults={"status": status, "comment": comment},
|
||||
defaults=defaults,
|
||||
)
|
||||
|
||||
return participation
|
||||
|
||||
Reference in New Issue
Block a user