Skip to content

Commit

Permalink
Add new model fields and remove old ones
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmorley15 committed Sep 22, 2024
1 parent 78f56a3 commit eab312e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 32 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', 'date', 'is_public', 'image_display')
list_display = ('name', 'type', 'user_id', 'is_public', 'image_display')
list_filter = ('type', 'user_id', 'is_public')

def image_display(self, obj):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 5.0.8 on 2024-09-22 04:02

import django.db.models.deletion
import uuid
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('adventures', '0006_alter_adventure_link'),
]

operations = [
migrations.RemoveField(
model_name='adventure',
name='date',
),
migrations.RemoveField(
model_name='adventure',
name='end_date',
),
migrations.CreateModel(
name='Visit',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)),
('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)),
('adventure', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='visits', to='adventures.adventure')),
],
),
]
31 changes: 18 additions & 13 deletions backend/server/adventures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,23 @@

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 @@ -45,8 +59,6 @@ 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 @@ -55,10 +67,7 @@ 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 @@ -69,7 +78,6 @@ 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 @@ -95,7 +103,6 @@ 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 @@ -129,7 +136,6 @@ 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 @@ -153,7 +159,6 @@ 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 @@ -175,7 +180,6 @@ 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 @@ -202,4 +206,5 @@ 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: 34 additions & 18 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
from .models import Adventure, AdventureImage, ChecklistItem, Collection, Note, Transportation, Checklist, Visit
from rest_framework import serializers

class AdventureImageSerializer(serializers.ModelSerializer):
Expand All @@ -17,20 +17,49 @@ 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', 'date', 'is_public', 'collection', 'created_at', 'updated_at', 'images', 'link', 'type', 'longitude', 'latitude', 'end_date']
fields = ['id', 'user_id', 'name', 'description', 'rating', 'activity_types', 'location', 'is_public', 'collection', 'created_at', 'updated_at', 'images', 'link', 'type', 'longitude', 'latitude', 'visits']
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 @@ -59,17 +88,7 @@ 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 @@ -133,9 +152,6 @@ 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 eab312e

Please sign in to comment.