2024-07-08 11:44:39 -04:00
|
|
|
import os
|
2024-07-15 09:36:07 -04:00
|
|
|
from .models import Adventure, Collection
|
2024-07-08 11:44:39 -04:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
class AdventureSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Adventure
|
2024-07-09 13:43:32 -04:00
|
|
|
fields = '__all__'
|
2024-07-08 11:44:39 -04:00
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
representation = super().to_representation(instance)
|
|
|
|
if instance.image:
|
|
|
|
public_url = os.environ.get('PUBLIC_URL', 'http://127.0.0.1:8000').rstrip('/')
|
|
|
|
print(public_url)
|
|
|
|
# remove any ' from the url
|
|
|
|
public_url = public_url.replace("'", "")
|
|
|
|
representation['image'] = f"{public_url}/media/{instance.image.name}"
|
2024-07-09 13:26:39 -04:00
|
|
|
return representation
|
|
|
|
|
2024-07-18 11:26:14 -04:00
|
|
|
def validate_activity_types(self, value):
|
|
|
|
if value:
|
|
|
|
return [activity.lower() for activity in value]
|
|
|
|
return value
|
|
|
|
|
2024-07-15 09:36:07 -04:00
|
|
|
class CollectionSerializer(serializers.ModelSerializer):
|
2024-07-09 13:43:32 -04:00
|
|
|
adventures = AdventureSerializer(many=True, read_only=True, source='adventure_set')
|
2024-07-09 13:26:39 -04:00
|
|
|
|
|
|
|
class Meta:
|
2024-07-15 09:36:07 -04:00
|
|
|
model = Collection
|
2024-07-09 13:43:32 -04:00
|
|
|
# fields are all plus the adventures field
|
2024-07-15 18:01:49 -04:00
|
|
|
fields = ['id', 'description', 'user_id', 'name', 'is_public', 'adventures']
|
2024-07-09 13:43:32 -04:00
|
|
|
|
2024-07-09 13:26:39 -04:00
|
|
|
|
|
|
|
|