diff --git a/backend/server/adventures/serializers.py b/backend/server/adventures/serializers.py index ce543894..129758c9 100644 --- a/backend/server/adventures/serializers.py +++ b/backend/server/adventures/serializers.py @@ -2,8 +2,10 @@ import os from .models import Adventure, AdventureImage, ChecklistItem, Collection, Note, Transportation, Checklist, Visit, Category from rest_framework import serializers +from main.utils import CustomModelSerializer -class AdventureImageSerializer(serializers.ModelSerializer): + +class AdventureImageSerializer(CustomModelSerializer): class Meta: model = AdventureImage fields = ['id', 'image', 'adventure'] @@ -19,11 +21,12 @@ def to_representation(self, instance): representation['image'] = f"{public_url}/media/{instance.image.name}" return representation -class CategorySerializer(serializers.ModelSerializer): +class CategorySerializer(CustomModelSerializer): + num_adventures = serializers.SerializerMethodField() class Meta: model = Category - fields = ['id', 'name', 'display_name', 'icon', 'user_id'] - read_only_fields = ['id', 'user_id'] + fields = ['id', 'name', 'display_name', 'icon', 'user_id', 'num_adventures'] + read_only_fields = ['id', 'user_id', 'num_adventures'] def validate_name(self, value): if Category.objects.filter(name=value).exists(): @@ -31,14 +34,17 @@ def validate_name(self, value): return value -class VisitSerializer(serializers.ModelSerializer): + def get_num_adventures(self, obj): + return Adventure.objects.filter(category=obj, user_id=obj.user_id).count() + +class VisitSerializer(CustomModelSerializer): class Meta: model = Visit fields = ['id', 'start_date', 'end_date', 'notes'] read_only_fields = ['id'] -class AdventureSerializer(serializers.ModelSerializer): +class AdventureSerializer(CustomModelSerializer): images = AdventureImageSerializer(many=True, read_only=True) visits = VisitSerializer(many=True, read_only=False) category = CategorySerializer(read_only=True) @@ -102,7 +108,7 @@ def update(self, instance, validated_data): return instance -class TransportationSerializer(serializers.ModelSerializer): +class TransportationSerializer(CustomModelSerializer): class Meta: model = Transportation @@ -113,7 +119,7 @@ class Meta: ] read_only_fields = ['id', 'created_at', 'updated_at', 'user_id'] -class NoteSerializer(serializers.ModelSerializer): +class NoteSerializer(CustomModelSerializer): class Meta: model = Note @@ -123,7 +129,7 @@ class Meta: ] read_only_fields = ['id', 'created_at', 'updated_at', 'user_id'] -class ChecklistItemSerializer(serializers.ModelSerializer): +class ChecklistItemSerializer(CustomModelSerializer): class Meta: model = ChecklistItem fields = [ @@ -131,7 +137,7 @@ class Meta: ] read_only_fields = ['id', 'created_at', 'updated_at', 'user_id', 'checklist'] -class ChecklistSerializer(serializers.ModelSerializer): +class ChecklistSerializer(CustomModelSerializer): items = ChecklistItemSerializer(many=True, source='checklistitem_set') class Meta: model = Checklist @@ -194,7 +200,7 @@ def validate(self, data): return data -class CollectionSerializer(serializers.ModelSerializer): +class CollectionSerializer(CustomModelSerializer): adventures = AdventureSerializer(many=True, read_only=True, source='adventure_set') transportations = TransportationSerializer(many=True, read_only=True, source='transportation_set') notes = NoteSerializer(many=True, read_only=True, source='note_set') diff --git a/backend/server/main/utils.py b/backend/server/main/utils.py new file mode 100644 index 00000000..0963ba62 --- /dev/null +++ b/backend/server/main/utils.py @@ -0,0 +1,10 @@ +from rest_framework import serializers + +def get_user_uuid(user): + return str(user.uuid) + +class CustomModelSerializer(serializers.ModelSerializer): + def to_representation(self, instance): + representation = super().to_representation(instance) + representation['user_id'] = get_user_uuid(instance.user_id) + return representation \ No newline at end of file diff --git a/backend/server/users/serializers.py b/backend/server/users/serializers.py index 63b5b7dc..776bb78e 100644 --- a/backend/server/users/serializers.py +++ b/backend/server/users/serializers.py @@ -196,6 +196,7 @@ def to_representation(self, instance): # remove any ' from the url public_url = public_url.replace("'", "") representation['profile_pic'] = f"{public_url}/media/{instance.profile_pic.name}" + del representation['pk'] # remove the pk field from the response return representation class MyPasswordResetSerializer(PasswordResetSerializer): diff --git a/backend/server/worldtravel/serializers.py b/backend/server/worldtravel/serializers.py index 386f673a..0f2ed734 100644 --- a/backend/server/worldtravel/serializers.py +++ b/backend/server/worldtravel/serializers.py @@ -1,6 +1,8 @@ import os from .models import Country, Region, VisitedRegion from rest_framework import serializers +from main.utils import CustomModelSerializer + class CountrySerializer(serializers.ModelSerializer): def get_public_url(self, obj): @@ -36,7 +38,7 @@ class Meta: fields = '__all__' read_only_fields = ['id', 'name', 'country', 'longitude', 'latitude'] -class VisitedRegionSerializer(serializers.ModelSerializer): +class VisitedRegionSerializer(CustomModelSerializer): longitude = serializers.DecimalField(source='region.longitude', max_digits=9, decimal_places=6, read_only=True) latitude = serializers.DecimalField(source='region.latitude', max_digits=9, decimal_places=6, read_only=True) name = serializers.CharField(source='region.name', read_only=True) diff --git a/frontend/src/lib/components/AdventureCard.svelte b/frontend/src/lib/components/AdventureCard.svelte index 519d9761..c8f0de35 100644 --- a/frontend/src/lib/components/AdventureCard.svelte +++ b/frontend/src/lib/components/AdventureCard.svelte @@ -167,7 +167,7 @@