diff --git a/photo/migrations/0005_collection_created_at_collection_updated_at_and_more.py b/photo/migrations/0005_collection_created_at_collection_updated_at_and_more.py new file mode 100644 index 0000000..4497bd7 --- /dev/null +++ b/photo/migrations/0005_collection_created_at_collection_updated_at_and_more.py @@ -0,0 +1,67 @@ +# Generated by Django 4.2.8 on 2025-02-21 16:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("photo", "0004_alter_contest_prize"), + ] + + operations = [ + migrations.AddField( + model_name="collection", + name="created_at", + field=models.DateTimeField(auto_now_add=True, null=True), + ), + migrations.AddField( + model_name="collection", + name="updated_at", + field=models.DateTimeField(auto_now=True, null=True), + ), + migrations.AddField( + model_name="contest", + name="created_at", + field=models.DateTimeField(auto_now_add=True, null=True), + ), + migrations.AddField( + model_name="contest", + name="updated_at", + field=models.DateTimeField(auto_now=True, null=True), + ), + migrations.AddField( + model_name="contestsubmission", + name="created_at", + field=models.DateTimeField(auto_now_add=True, null=True), + ), + migrations.AddField( + model_name="contestsubmission", + name="updated_at", + field=models.DateTimeField(auto_now=True, null=True), + ), + migrations.AddField( + model_name="picture", + name="created_at", + field=models.DateTimeField(auto_now_add=True, null=True), + ), + migrations.AddField( + model_name="picture", + name="updated_at", + field=models.DateTimeField(auto_now=True, null=True), + ), + migrations.AddField( + model_name="picturecomment", + name="updated_at", + field=models.DateTimeField(auto_now=True, null=True), + ), + migrations.AddField( + model_name="user", + name="created_at", + field=models.DateTimeField(auto_now_add=True, null=True), + ), + migrations.AddField( + model_name="user", + name="updated_at", + field=models.DateTimeField(auto_now=True, null=True), + ), + ] diff --git a/photo/models.py b/photo/models.py index 85d4a52..bbd5e0f 100644 --- a/photo/models.py +++ b/photo/models.py @@ -48,6 +48,8 @@ def create_superuser(self, email, password=None, **kwargs): class SoftDeleteModel(models.Model): is_deleted = models.BooleanField(default=False) + created_at = models.DateTimeField(null=True, blank=True, auto_now_add=True) + updated_at = models.DateTimeField(null=True, blank=True, auto_now=True) objects = SoftDeleteManager() all_objects = models.Manager() diff --git a/photo/tests/test_timestamps.py b/photo/tests/test_timestamps.py new file mode 100644 index 0000000..c70ff35 --- /dev/null +++ b/photo/tests/test_timestamps.py @@ -0,0 +1,39 @@ +from django.test import TestCase +from django.utils import timezone + +from photo.models import Collection, User + + +class TimestampFieldsTest(TestCase): + def setUp(self): + self.user = User.objects.create_user( + email="test@example.com", + password="testpass123", + name_first="Test", + name_last="User", + ) + + def test_timestamp_fields(self): + # Create a collection and verify timestamps are set + collection = Collection.objects.create( + name="Test Collection", + user=self.user, + ) + self.assertIsNotNone(collection.created_at) + self.assertIsNotNone(collection.updated_at) + self.assertEqual(collection.created_at, collection.updated_at) + + # Store the original timestamps + original_created_at = collection.created_at + original_updated_at = collection.updated_at + + # Wait a moment to ensure timestamps will be different + timezone.now() + + # Update the collection and verify only updated_at changes + collection.name = "Updated Collection" + collection.save() + collection.refresh_from_db() + + self.assertEqual(collection.created_at, original_created_at) + self.assertGreater(collection.updated_at, original_updated_at) diff --git a/photo/types.py b/photo/types.py index e63affc..9f83605 100644 --- a/photo/types.py +++ b/photo/types.py @@ -23,6 +23,8 @@ class UserType: profile_picture: "PictureType" profile_picture_updated_at: strawberry.auto user_handle: str + created_at: strawberry.auto + updated_at: strawberry.auto @strawberry.django.type(Picture) @@ -32,6 +34,8 @@ class PictureType: name: str file: str likes: List[UserType] + created_at: strawberry.auto + updated_at: strawberry.auto @strawberry.django.type(PictureComment) @@ -41,6 +45,7 @@ class PictureCommentType: picture: "PictureType" text: str created_at: strawberry.auto + updated_at: strawberry.auto @strawberry.django.type(Collection) @@ -49,6 +54,8 @@ class CollectionType: name: str user: "UserType" pictures: List[PictureType] + created_at: strawberry.auto + updated_at: strawberry.auto @strawberry.django.type(Contest) @@ -67,6 +74,8 @@ class ContestType: winners: List[UserType] created_by: "UserType" status: str + created_at: strawberry.auto + updated_at: strawberry.auto @strawberry.field def status(self) -> str: @@ -92,6 +101,8 @@ class ContestSubmissionType: picture: PictureType submission_date: strawberry.auto votes: List[UserType] + created_at: strawberry.auto + updated_at: strawberry.auto @strawberry.type