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

chore: Add created_at field to Adventure and Collection models

This commit is contained in:
Sean Morley 2024-07-18 15:48:14 -04:00
parent 040d5a755f
commit 704eb6f6de
6 changed files with 87 additions and 4 deletions

View file

@ -0,0 +1,26 @@
# Generated by Django 5.0.6 on 2024-07-18 19:27
import django.utils.timezone
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('adventures', '0009_alter_adventure_image'),
]
operations = [
migrations.AddField(
model_name='adventure',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='collection',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
]

View file

@ -35,6 +35,7 @@ class Adventure(models.Model):
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)
collection = models.ForeignKey('Collection', on_delete=models.CASCADE, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
def clean(self):
if self.collection:
@ -53,6 +54,7 @@ class Collection(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(blank=True, null=True)
is_public = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
# if connected adventures are private and collection is public, raise an error
def clean(self):

View file

@ -31,11 +31,11 @@ class AdventureViewSet(viewsets.ModelViewSet):
pagination_class = StandardResultsSetPagination
def apply_sorting(self, queryset):
order_by = self.request.query_params.get('order_by', 'name')
order_by = self.request.query_params.get('order_by', 'created_at')
order_direction = self.request.query_params.get('order_direction', 'asc')
include_collections = self.request.query_params.get('include_collections', 'true')
valid_order_by = ['name', 'type', 'date', 'rating']
valid_order_by = ['name', 'type', 'date', 'rating', 'created_at']
if order_by not in valid_order_by:
order_by = 'name'
@ -52,6 +52,13 @@ class AdventureViewSet(viewsets.ModelViewSet):
if order_direction == 'desc':
ordering = f'-{ordering}'
# reverse ordering for created_at field
if order_by == 'created_at':
if order_direction == 'asc':
ordering = '-created_at'
else:
ordering = 'created_at'
print(f"Ordering by: {ordering}") # For debugging
if include_collections == 'false':