Skip to content

Commit

Permalink
Revert "Add new model fields and remove old ones"
Browse files Browse the repository at this point in the history
This reverts commit eab312e.
  • Loading branch information
seanmorley15 committed Sep 22, 2024
1 parent eab312e commit 7c5b448
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 88 deletions.
2 changes: 1 addition & 1 deletion backend/server/adventures/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class AdventureAdmin(admin.ModelAdmin):
list_display = ('name', 'type', 'user_id', 'is_public', 'image_display')
list_display = ('name', 'type', 'user_id', 'date', 'is_public', 'image_display')
list_filter = ('type', 'user_id', 'is_public')

def image_display(self, obj):
Expand Down

This file was deleted.

31 changes: 13 additions & 18 deletions backend/server/adventures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,9 @@

User = get_user_model()

class Visit(models.Model):
id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
adventure = models.ForeignKey('Adventure', on_delete=models.CASCADE, related_name='visits')
start_date = models.DateField()
end_date = models.DateField()
notes = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def clean(self):
if self.start_date > self.end_date:
raise ValidationError('The start date must be before or equal to the end date.')

def __str__(self):
return f"{self.adventure.name} - {self.start_date} to {self.end_date}"

class Adventure(models.Model):
#id = models.AutoField(primary_key=True)
id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
user_id = models.ForeignKey(
User, on_delete=models.CASCADE, default=default_user_id)
Expand All @@ -59,6 +45,8 @@ class Adventure(models.Model):
rating = models.FloatField(blank=True, null=True)
link = models.URLField(blank=True, null=True, max_length=2083)
image = ResizedImageField(force_format="WEBP", quality=75, null=True, blank=True, upload_to='images/')
date = models.DateField(blank=True, null=True)
end_date = models.DateField(blank=True, null=True)
is_public = models.BooleanField(default=False)
longitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
Expand All @@ -67,7 +55,10 @@ class Adventure(models.Model):
updated_at = models.DateTimeField(auto_now=True)

def clean(self):

if self.date and self.end_date and self.date > self.end_date:
raise ValidationError('The start date must be before the end date. Start date: ' + str(self.date) + ' End date: ' + str(self.end_date))
if self.end_date and not self.date:
raise ValidationError('Adventures must have an end date. Adventure: ' + self.name)
if self.collection:
if self.collection.is_public and not self.is_public:
raise ValidationError('Adventures associated with a public collection must be public. Collection: ' + self.trip.name + ' Adventure: ' + self.name)
Expand All @@ -78,6 +69,7 @@ def __str__(self):
return self.name

class Collection(models.Model):
#id = models.AutoField(primary_key=True)
id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
user_id = models.ForeignKey(
User, on_delete=models.CASCADE, default=default_user_id)
Expand All @@ -103,6 +95,7 @@ def __str__(self):
return self.name

class Transportation(models.Model):
#id = models.AutoField(primary_key=True)
id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
user_id = models.ForeignKey(
User, on_delete=models.CASCADE, default=default_user_id)
Expand Down Expand Up @@ -136,6 +129,7 @@ def __str__(self):
return self.name

class Note(models.Model):
#id = models.AutoField(primary_key=True)
id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
user_id = models.ForeignKey(
User, on_delete=models.CASCADE, default=default_user_id)
Expand All @@ -159,6 +153,7 @@ def __str__(self):
return self.name

class Checklist(models.Model):
# id = models.AutoField(primary_key=True)
id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
user_id = models.ForeignKey(
User, on_delete=models.CASCADE, default=default_user_id)
Expand All @@ -180,6 +175,7 @@ def __str__(self):
return self.name

class ChecklistItem(models.Model):
#id = models.AutoField(primary_key=True)
id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
user_id = models.ForeignKey(
User, on_delete=models.CASCADE, default=default_user_id)
Expand All @@ -206,5 +202,4 @@ class AdventureImage(models.Model):
adventure = models.ForeignKey(Adventure, related_name='images', on_delete=models.CASCADE)

def __str__(self):
return self.image.url

return self.image.url
52 changes: 18 additions & 34 deletions backend/server/adventures/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from .models import Adventure, AdventureImage, ChecklistItem, Collection, Note, Transportation, Checklist, Visit
from .models import Adventure, AdventureImage, ChecklistItem, Collection, Note, Transportation, Checklist
from rest_framework import serializers

class AdventureImageSerializer(serializers.ModelSerializer):
Expand All @@ -17,49 +17,20 @@ def to_representation(self, instance):
public_url = public_url.replace("'", "")
representation['image'] = f"{public_url}/media/{instance.image.name}"
return representation

class VisitSerializer(serializers.ModelSerializer):
class Meta:
model = Visit
fields = ['id', 'start_date', 'end_date', 'notes']
read_only_fields = ['id']



class AdventureSerializer(serializers.ModelSerializer):
images = AdventureImageSerializer(many=True, read_only=True)
visits = VisitSerializer(many=True, read_only=False)
class Meta:
model = Adventure
fields = ['id', 'user_id', 'name', 'description', 'rating', 'activity_types', 'location', 'is_public', 'collection', 'created_at', 'updated_at', 'images', 'link', 'type', 'longitude', 'latitude', 'visits']
fields = ['id', 'user_id', 'name', 'description', 'rating', 'activity_types', 'location', 'date', 'is_public', 'collection', 'created_at', 'updated_at', 'images', 'link', 'type', 'longitude', 'latitude', 'end_date']
read_only_fields = ['id', 'created_at', 'updated_at', 'user_id']

def to_representation(self, instance):
representation = super().to_representation(instance)
return representation

def create(self, validated_data):
visits_data = validated_data.pop('visits', [])
adventure = Adventure.objects.create(**validated_data)
for visit_data in visits_data:
Visit.objects.create(adventure=adventure, **visit_data)
return adventure

def update(self, instance, validated_data):
visits_data = validated_data.pop('visits', [])
instance = super().update(instance, validated_data)

# Update or create visits
for visit_data in visits_data:
visit_id = visit_data.get('id', None)
if visit_id:
visit = Visit.objects.get(id=visit_id, adventure=instance)
for attr, value in visit_data.items():
setattr(visit, attr, value)
visit.save()
else:
Visit.objects.create(adventure=instance, **visit_data)

return instance

class TransportationSerializer(serializers.ModelSerializer):

class Meta:
Expand Down Expand Up @@ -88,7 +59,17 @@ class Meta:
'id', 'user_id', 'name', 'is_checked', 'checklist', 'created_at', 'updated_at'
]
read_only_fields = ['id', 'created_at', 'updated_at', 'user_id', 'checklist']


# def validate(self, data):
# # Check if the checklist is public and the checklist item is not
# checklist = data.get('checklist')
# is_checked = data.get('is_checked', False)
# if checklist and checklist.is_public and not is_checked:
# raise serializers.ValidationError(
# 'Checklist items associated with a public checklist must be checked.'
# )


class ChecklistSerializer(serializers.ModelSerializer):
items = ChecklistItemSerializer(many=True, source='checklistitem_set')
class Meta:
Expand Down Expand Up @@ -152,6 +133,9 @@ def validate(self, data):

return data




class CollectionSerializer(serializers.ModelSerializer):
adventures = AdventureSerializer(many=True, read_only=True, source='adventure_set')
transportations = TransportationSerializer(many=True, read_only=True, source='transportation_set')
Expand Down

0 comments on commit 7c5b448

Please sign in to comment.