Skip to content

Commit

Permalink
Merge pull request #128 from seanmorley15/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
seanmorley15 authored Jul 15, 2024
2 parents 2166781 + eddbda5 commit fa1f2b1
Show file tree
Hide file tree
Showing 29 changed files with 1,924 additions and 262 deletions.
4 changes: 2 additions & 2 deletions backend/server/adventures/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from django.contrib import admin
from django.utils.html import mark_safe
from .models import Adventure, Trip
from .models import Adventure, Collection
from worldtravel.models import Country, Region, VisitedRegion


Expand Down Expand Up @@ -65,7 +65,7 @@ def image_display(self, obj):
admin.site.register(Country, CountryAdmin)
admin.site.register(Region, RegionAdmin)
admin.site.register(VisitedRegion)
admin.site.register(Trip)
admin.site.register(Collection)

admin.site.site_header = 'AdventureLog Admin'
admin.site.site_title = 'AdventureLog Admin Site'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 5.0.6 on 2024-07-15 12:57

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('adventures', '0006_alter_adventure_type_alter_trip_type'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.RemoveField(
model_name='adventure',
name='trip',
),
migrations.AlterField(
model_name='adventure',
name='type',
field=models.CharField(choices=[('visited', 'Visited'), ('planned', 'Planned')], max_length=100),
),
migrations.CreateModel(
name='Collection',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=200)),
('is_public', models.BooleanField(default=False)),
('user_id', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.AddField(
model_name='adventure',
name='collection',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='adventures.collection'),
),
migrations.DeleteModel(
name='Trip',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.6 on 2024-07-15 13:05

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('adventures', '0007_remove_adventure_trip_alter_adventure_type_and_more'),
]

operations = [
migrations.AddField(
model_name='collection',
name='description',
field=models.TextField(blank=True, null=True),
),
]
30 changes: 10 additions & 20 deletions backend/server/adventures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
ADVENTURE_TYPES = [
('visited', 'Visited'),
('planned', 'Planned'),
('featured', 'Featured')
]


Expand All @@ -34,40 +33,31 @@ class Adventure(models.Model):
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)
trip = models.ForeignKey('Trip', on_delete=models.CASCADE, blank=True, null=True)
collection = models.ForeignKey('Collection', on_delete=models.CASCADE, blank=True, null=True)

def clean(self):
if self.trip:
if self.trip.is_public and not self.is_public:
raise ValidationError('Adventures associated with a public trip must be public. Trip: ' + self.trip.name + ' Adventure: ' + self.name)
if self.user_id != self.trip.user_id:
raise ValidationError('Adventures must be associated with trips owned by the same user. Trip owner: ' + self.trip.user_id.username + ' Adventure owner: ' + self.user_id.username)
if self.type != self.trip.type:
raise ValidationError('Adventure type must match trip type. Trip type: ' + self.trip.type + ' Adventure type: ' + self.type)
if self.type == 'featured' and not self.is_public:
raise ValidationError('Featured adventures must be public. 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)
if self.user_id != self.collection.user_id:
raise ValidationError('Adventures must be associated with collections owned by the same user. Collection owner: ' + self.collection.user_id.username + ' Adventure owner: ' + self.user_id.username)
def __str__(self):
return self.name

class Trip(models.Model):
class Collection(models.Model):
id = models.AutoField(primary_key=True)
user_id = models.ForeignKey(
User, on_delete=models.CASCADE, default=default_user_id)
name = models.CharField(max_length=200)
type = models.CharField(max_length=100, choices=ADVENTURE_TYPES)
location = models.CharField(max_length=200, blank=True, null=True)
date = models.DateField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
is_public = models.BooleanField(default=False)

# if connected adventures are private and trip is public, raise an error
# if connected adventures are private and collection is public, raise an error
def clean(self):
if self.is_public and self.pk: # Only check if the instance has a primary key
for adventure in self.adventure_set.all():
if not adventure.is_public:
raise ValidationError('Public trips cannot be associated with private adventures. Trip: ' + self.name + ' Adventure: ' + adventure.name)
if self.type == 'featured' and not self.is_public:
raise ValidationError('Featured trips must be public. Trip: ' + self.name)
raise ValidationError('Public collections cannot be associated with private adventures. Collection: ' + self.name + ' Adventure: ' + adventure.name)

def __str__(self):
return self.name
8 changes: 4 additions & 4 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, Trip
from .models import Adventure, Collection
from rest_framework import serializers

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

class TripSerializer(serializers.ModelSerializer):
class CollectionSerializer(serializers.ModelSerializer):
adventures = AdventureSerializer(many=True, read_only=True, source='adventure_set')

class Meta:
model = Trip
model = Collection
# fields are all plus the adventures field
fields = ['id', 'user_id', 'name', 'type', 'location', 'date', 'is_public', 'adventures']
fields = ['id', 'description', 'user_id', 'name', 'is_public', 'adventures']



4 changes: 2 additions & 2 deletions backend/server/adventures/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from .views import AdventureViewSet, TripViewSet, StatsViewSet, GenerateDescription
from .views import AdventureViewSet, CollectionViewSet, StatsViewSet, GenerateDescription

router = DefaultRouter()
router.register(r'adventures', AdventureViewSet, basename='adventures')
router.register(r'trips', TripViewSet, basename='trips')
router.register(r'collections', CollectionViewSet, basename='collections')
router.register(r'stats', StatsViewSet, basename='stats')
router.register(r'generate', GenerateDescription, basename='generate')

Expand Down
Loading

0 comments on commit fa1f2b1

Please sign in to comment.