1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-21 22:09:36 +02:00

feat: Add validation for adventure type matching trip type

This commit is contained in:
Sean Morley 2024-07-09 13:43:32 -04:00
parent 374963f305
commit aaa83ea2c0
3 changed files with 19 additions and 14 deletions

View file

@ -44,6 +44,8 @@ class Adventure(models.Model):
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) 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: if self.type != self.trip.type:
raise ValidationError('Adventure type must match trip type. Trip type: ' + self.trip.type + ' Adventure type: ' + self.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)
def __str__(self): def __str__(self):
return self.name return self.name
@ -60,13 +62,12 @@ class Trip(models.Model):
# if connected adventures are private and trip is public, raise an error # if connected adventures are private and trip is public, raise an error
def clean(self): def clean(self):
if self.is_public: if self.is_public and self.pk: # Only check if the instance has a primary key
for adventure in self.adventure_set.all(): for adventure in self.adventure_set.all():
if not adventure.is_public: if not adventure.is_public:
raise ValidationError('Public trips cannot be associated with private adventures. Trip: ' + self.name + ' Adventure: ' + adventure.name) 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)
def __str__(self): def __str__(self):
return self.name return self.name

View file

@ -6,7 +6,7 @@ class AdventureSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Adventure model = Adventure
fields = '__all__' # Serialize all fields of the Adventure model fields = '__all__'
def to_representation(self, instance): def to_representation(self, instance):
representation = super().to_representation(instance) representation = super().to_representation(instance)
@ -19,9 +19,12 @@ class AdventureSerializer(serializers.ModelSerializer):
return representation return representation
class TripSerializer(serializers.ModelSerializer): class TripSerializer(serializers.ModelSerializer):
adventures = AdventureSerializer(many=True, read_only=True, source='adventure_set')
class Meta: class Meta:
model = Trip model = Trip
fields = '__all__' # Serialize all fields of the Adventure model # fields are all plus the adventures field
fields = ['id', 'user_id', 'name', 'type', 'location', 'date', 'is_public', 'adventures']

View file

@ -4,7 +4,7 @@ from rest_framework.response import Response
from .models import Adventure, Trip from .models import Adventure, Trip
from .serializers import AdventureSerializer, TripSerializer from .serializers import AdventureSerializer, TripSerializer
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from django.db.models import Q from django.db.models import Q, Prefetch
from .permissions import IsOwnerOrReadOnly from .permissions import IsOwnerOrReadOnly
class AdventureViewSet(viewsets.ModelViewSet): class AdventureViewSet(viewsets.ModelViewSet):
@ -47,6 +47,10 @@ class TripViewSet(viewsets.ModelViewSet):
def get_queryset(self): def get_queryset(self):
return Trip.objects.filter( return Trip.objects.filter(
Q(is_public=True) | Q(user_id=self.request.user.id) Q(is_public=True) | Q(user_id=self.request.user.id)
).prefetch_related(
Prefetch('adventure_set', queryset=Adventure.objects.filter(
Q(is_public=True) | Q(user_id=self.request.user.id)
))
) )
def perform_create(self, serializer): def perform_create(self, serializer):
@ -54,21 +58,18 @@ class TripViewSet(viewsets.ModelViewSet):
@action(detail=False, methods=['get']) @action(detail=False, methods=['get'])
def visited(self, request): def visited(self, request):
trips = Trip.objects.filter( trips = self.get_queryset().filter(type='visited', user_id=request.user.id)
type='visited', user_id=request.user.id)
serializer = self.get_serializer(trips, many=True) serializer = self.get_serializer(trips, many=True)
return Response(serializer.data) return Response(serializer.data)
@action(detail=False, methods=['get']) @action(detail=False, methods=['get'])
def planned(self, request): def planned(self, request):
trips = Trip.objects.filter( trips = self.get_queryset().filter(type='planned', user_id=request.user.id)
type='planned', user_id=request.user.id)
serializer = self.get_serializer(trips, many=True) serializer = self.get_serializer(trips, many=True)
return Response(serializer.data) return Response(serializer.data)
@action(detail=False, methods=['get']) @action(detail=False, methods=['get'])
def featured(self, request): def featured(self, request):
trips = Trip.objects.filter( trips = self.get_queryset().filter(type='featured', is_public=True)
type='featured', is_public=True)
serializer = self.get_serializer(trips, many=True) serializer = self.get_serializer(trips, many=True)
return Response(serializer.data) return Response(serializer.data)