1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-05 13:15:18 +02:00

Add date range validation for visit's adventure within a collection

This commit is contained in:
Giiid 2025-07-23 23:14:35 +01:00
parent 47efae6f91
commit d7d75e8a1e
2 changed files with 30 additions and 1 deletions

View file

@ -114,6 +114,11 @@ class VisitAdmin(admin.ModelAdmin):
search_fields = ('notes',) search_fields = ('notes',)
def save_model(self, request, obj, form, change):
obj.full_clean()
super().save_model(request, obj, form, change)
def image_display(self, obj): def image_display(self, obj):
if obj.image: # Ensure this field matches your model's image field if obj.image: # Ensure this field matches your model's image field
public_url = os.environ.get('PUBLIC_URL', 'http://127.0.0.1:8000').rstrip('/') public_url = os.environ.get('PUBLIC_URL', 'http://127.0.0.1:8000').rstrip('/')

View file

@ -548,9 +548,33 @@ class Visit(models.Model):
updated_at = models.DateTimeField(auto_now=True) updated_at = models.DateTimeField(auto_now=True)
def clean(self): def clean(self):
if self.start_date > self.end_date: super().clean()
# Validate start date is before end date
if self.start_date and self.end_date and self.start_date > self.end_date:
raise ValidationError('The start date must be before or equal to the end date.') raise ValidationError('The start date must be before or equal to the end date.')
# Validates that visit dates fall within their collection date range
if self.start_date and self.end_date and self.adventure:
collections = self.adventure.collections.filter(start_date__isnull=False, end_date__isnull=False)
if collections.exists():
visit_start_date = self.start_date.date() if hasattr(self.start_date, 'date') else self.start_date
visit_end_date = self.end_date.date() if hasattr(self.end_date, 'date') else self.end_date
for collection in collections:
collection_start_date = collection.start_date
collection_end_date = collection.end_date
if not (
collection_start_date <= visit_start_date <= collection_end_date and
collection_start_date <= visit_end_date <= collection_end_date
):
raise ValidationError(
f'Visit dates ({visit_start_date}) to ({visit_end_date}) for {self.adventure.name}' \
'should be between the collection date range'
)
def __str__(self): def __str__(self):
return f"{self.adventure.name} - {self.start_date} to {self.end_date}" return f"{self.adventure.name} - {self.start_date} to {self.end_date}"