1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-19 12:59: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)
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)
def __str__(self):
return self.name
@ -60,13 +62,12 @@ class Trip(models.Model):
# if connected adventures are private and trip is public, raise an error
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():
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)
def __str__(self):
return self.name
return self.name