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..e464048 --- /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-01-24 17:02 + +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..e647f04 100644 --- a/photo/models.py +++ b/photo/models.py @@ -47,6 +47,8 @@ def create_superuser(self, email, password=None, **kwargs): class SoftDeleteModel(models.Model): + created_at = models.DateTimeField(auto_now_add=True, null=True) + updated_at = models.DateTimeField(auto_now=True, null=True) is_deleted = models.BooleanField(default=False) objects = SoftDeleteManager() all_objects = models.Manager() diff --git a/photo/tests/factories.py b/photo/tests/factories.py index 12c706b..f771630 100644 --- a/photo/tests/factories.py +++ b/photo/tests/factories.py @@ -27,6 +27,8 @@ class Meta: name_last = factory.Faker("last_name") username = factory.Sequence(lambda n: "user{0}".format(n)) password = factory.Sequence(lambda n: "user{0}password".format(n)) + created_at = factory.Faker("date_time", tzinfo=pytz.UTC) + updated_at = factory.Faker("date_time", tzinfo=pytz.UTC) profile_picture_updated_at = timezone.now() @factory.post_generation @@ -147,6 +149,7 @@ class Meta: model = ContestSubmission skip_postgeneration_save = True + django_get_or_create = ('contest', 'picture') contest = factory.SubFactory(ContestFactory) picture = factory.SubFactory( PictureFactory, user=factory.SubFactory(UserFactory, user_profile_picture=True) diff --git a/photo/tests/test_database/test_collection.py b/photo/tests/test_database/test_collection.py index 7faf4c4..22ac4fb 100644 --- a/photo/tests/test_database/test_collection.py +++ b/photo/tests/test_database/test_collection.py @@ -31,3 +31,14 @@ def test_factory_null(self): def test_factory_pk(self): with self.assertRaises(IntegrityError): CollectionFactory(user=self.collection.user, name=self.collection.name) + + def test_created_at_and_updated_at_nullable(self): + collection = Collection.objects.create(user=self.user) + self.assertIsNotNone(collection.created_at) + self.assertIsNotNone(collection.updated_at) + + def test_created_at_and_updated_at_update(self): + collection = Collection.objects.create(user=self.user) + collection.name = "New Name" + collection.save() + self.assertIsNotNone(collection.updated_at) diff --git a/photo/tests/test_database/test_contest.py b/photo/tests/test_database/test_contest.py index 4295128..ccbbcbe 100644 --- a/photo/tests/test_database/test_contest.py +++ b/photo/tests/test_database/test_contest.py @@ -33,3 +33,14 @@ def test_factory_null(self): def test_factory_pk(self): with self.assertRaises(IntegrityError): ContestFactory(id=self.contest.id) + + def test_created_at_and_updated_at_nullable(self): + contest = Contest.objects.create(created_by=self.winners[0]) + self.assertIsNotNone(contest.created_at) + self.assertIsNotNone(contest.updated_at) + + def test_created_at_and_updated_at_update(self): + contest = Contest.objects.create(created_by=self.winners[0]) + contest.title = "New Title" + contest.save() + self.assertIsNotNone(contest.updated_at) diff --git a/photo/tests/test_database/test_contest_submission.py b/photo/tests/test_database/test_contest_submission.py index 830a976..ba65d1e 100644 --- a/photo/tests/test_database/test_contest_submission.py +++ b/photo/tests/test_database/test_contest_submission.py @@ -39,3 +39,13 @@ def test_second_submission_same_user(self): picture=PictureFactory(user=self.contest_submission.picture.user), contest=self.contest_submission.contest, ) + + def test_created_at_and_updated_at_nullable(self): + submission = ContestSubmission.objects.create(contest=self.contest_submission.contest, picture=self.contest_submission.picture) + self.assertIsNotNone(submission.created_at) + self.assertIsNotNone(submission.updated_at) + + def test_created_at_and_updated_at_update(self): + submission = ContestSubmission.objects.create(contest=self.contest_submission.contest, picture=self.contest_submission.picture) + submission.save() + self.assertIsNotNone(submission.updated_at) diff --git a/photo/tests/test_database/test_picture.py b/photo/tests/test_database/test_picture.py index b6132fc..6d5569c 100644 --- a/photo/tests/test_database/test_picture.py +++ b/photo/tests/test_database/test_picture.py @@ -29,6 +29,15 @@ def test_factory(self): for like in self.picture.likes.all(): self.assertTrue(User.objects.filter(email=like.email).exists()) + def test_created_at_and_updated_at_nullable(self): + picture = Picture.objects.create(user=self.user) + self.assertIsNotNone(picture.created_at) + self.assertIsNotNone(picture.updated_at) + + def test_created_at_and_updated_at_update(self): + picture = Picture.objects.create(user=self.user) + picture.save() + self.assertIsNotNone(picture.updated_at) class PictureUploadTest(TestCase): def setUp(self): diff --git a/photo/tests/test_database/test_picture_comment.py b/photo/tests/test_database/test_picture_comment.py index ea658d5..7f6d999 100644 --- a/photo/tests/test_database/test_picture_comment.py +++ b/photo/tests/test_database/test_picture_comment.py @@ -25,3 +25,13 @@ def test_factory_null(self): def test_factory_pk(self): with self.assertRaises(IntegrityError): PictureCommentFactory(id=self.picture_comment.id) + + def test_created_at_and_updated_at_nullable(self): + comment = PictureComment.objects.create(user=self.picture_comment.user, picture=self.picture_comment.picture) + self.assertIsNotNone(comment.created_at) + self.assertIsNotNone(comment.updated_at) + + def test_created_at_and_updated_at_update(self): + comment = PictureComment.objects.create(user=self.picture_comment.user, picture=self.picture_comment.picture) + comment.save() + self.assertIsNotNone(comment.updated_at) diff --git a/photo/tests/test_queries/graphql_queries.py b/photo/tests/test_queries/graphql_queries.py index 3a1875f..aca54cb 100644 --- a/photo/tests/test_queries/graphql_queries.py +++ b/photo/tests/test_queries/graphql_queries.py @@ -11,6 +11,8 @@ pictures { id } + created_at + updated_at } } """ @@ -55,6 +57,8 @@ id } status + created_at + updated_at } } """ @@ -131,6 +135,8 @@ votes { email } + created_at + updated_at } } """ @@ -169,6 +175,8 @@ likes { email } + created_at + updated_at } } """ diff --git a/photo/types.py b/photo/types.py index e63affc..d2dc484 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) @@ -42,6 +46,7 @@ class PictureCommentType: text: str created_at: strawberry.auto + updated_at: strawberry.auto @strawberry.django.type(Collection) class CollectionType: @@ -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