2024-07-08 11:44:39 -04:00
from django . db import models
from django . contrib . auth import get_user_model
from django . contrib . postgres . fields import ArrayField
2024-07-09 13:01:56 -04:00
from django . forms import ValidationError
ADVENTURE_TYPES = [
( ' visited ' , ' Visited ' ) ,
( ' planned ' , ' Planned ' ) ,
( ' featured ' , ' Featured ' )
]
2024-07-08 11:44:39 -04:00
# Assuming you have a default user ID you want to use
default_user_id = 1 # Replace with an actual user ID
User = get_user_model ( )
class Adventure ( models . Model ) :
id = models . AutoField ( primary_key = True )
user_id = models . ForeignKey (
User , on_delete = models . CASCADE , default = default_user_id )
2024-07-09 13:01:56 -04:00
type = models . CharField ( max_length = 100 , choices = ADVENTURE_TYPES )
2024-07-08 11:44:39 -04:00
name = models . CharField ( max_length = 200 )
location = models . CharField ( max_length = 200 , blank = True , null = True )
activity_types = ArrayField ( models . CharField (
max_length = 100 ) , blank = True , null = True )
description = models . TextField ( blank = True , null = True )
rating = models . FloatField ( blank = True , null = True )
link = models . URLField ( blank = True , null = True )
image = models . ImageField ( null = True , blank = True , upload_to = ' images/ ' )
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 )
2024-07-09 13:01:56 -04:00
trip = models . ForeignKey ( ' Trip ' , on_delete = models . CASCADE , blank = True , null = True )
def clean ( self ) :
if self . trip :
if self . trip . is_public and not self . is_public :
2024-07-09 13:26:45 -04:00
raise ValidationError ( ' Adventures associated with a public trip must be public. Trip: ' + self . trip . name + ' Adventure: ' + self . name )
2024-07-09 13:01:56 -04:00
if self . user_id != self . trip . user_id :
2024-07-09 13:26:45 -04:00
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 )
2024-07-10 17:27:43 -04:00
if self . type == ' featured ' and not self . is_public :
raise ValidationError ( ' Featured adventures must be public. Adventure: ' + self . name )
2024-07-09 13:26:39 -04:00
def __str__ ( self ) :
return self . name
2024-07-09 13:01:56 -04:00
class Trip ( 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 )
is_public = models . BooleanField ( default = False )
2024-07-09 13:26:39 -04:00
# if connected adventures are private and trip is public, raise an error
2024-07-09 13:01:56 -04:00
def clean ( self ) :
2024-07-09 13:43:32 -04:00
if self . is_public and self . pk : # Only check if the instance has a primary key
2024-07-09 13:26:39 -04:00
for adventure in self . adventure_set . all ( ) :
if not adventure . is_public :
2024-07-09 13:26:45 -04:00
raise ValidationError ( ' Public trips cannot be associated with private adventures. Trip: ' + self . name + ' Adventure: ' + adventure . name )
2024-07-09 13:43:32 -04:00
if self . type == ' featured ' and not self . is_public :
raise ValidationError ( ' Featured trips must be public. Trip: ' + self . name )
2024-07-08 11:44:39 -04:00
def __str__ ( self ) :
2024-07-09 13:43:32 -04:00
return self . name