Skip to content

Fix issue #197: Add timestamps to models demo #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Generated by Django 4.2.8 on 2025-02-11 11:40

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),
),
]
2 changes: 2 additions & 0 deletions photo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
57 changes: 57 additions & 0 deletions photo/tests/test_timestamps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django.test import TestCase
from django.utils import timezone
from photo.models import User, Picture, Collection, Contest, ContestSubmission
from django.core.files.uploadedfile import SimpleUploadedFile


class TimestampFieldsTest(TestCase):
def setUp(self):
self.user = User.objects.create_user(
email="test@example.com",
password="testpass123",
name_first="Test",
name_last="User"
)

self.picture = Picture.objects.create(
user=self.user,
name="Test Picture",
file=SimpleUploadedFile(
"test.jpg",
b"file_content",
content_type="image/jpeg"
)
)

def test_timestamp_fields_on_create(self):
"""Test that created_at and updated_at are set on model creation"""
collection = Collection.objects.create(
name="Test Collection",
user=self.user
)

self.assertIsNotNone(collection.created_at)
self.assertIsNotNone(collection.updated_at)
self.assertIsInstance(collection.created_at, timezone.datetime)
self.assertIsInstance(collection.updated_at, timezone.datetime)
self.assertEqual(collection.created_at.date(), timezone.now().date())
self.assertEqual(collection.updated_at.date(), timezone.now().date())

def test_updated_at_changes_on_update(self):
"""Test that updated_at changes when model is updated"""
collection = Collection.objects.create(
name="Test Collection",
user=self.user
)
original_updated_at = collection.updated_at

# Wait a moment to ensure time difference
import time
time.sleep(0.1)

collection.name = "Updated Collection"
collection.save()

self.assertNotEqual(collection.updated_at, original_updated_at)
self.assertEqual(collection.created_at.date(), timezone.now().date())
self.assertEqual(collection.updated_at.date(), timezone.now().date())
11 changes: 11 additions & 0 deletions photo/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class UserType:
profile_picture: "PictureType"
profile_picture_updated_at: strawberry.auto
user_handle: str
created_at: strawberry.auto | None
updated_at: strawberry.auto | None


@strawberry.django.type(Picture)
Expand All @@ -32,6 +34,8 @@ class PictureType:
name: str
file: str
likes: List[UserType]
created_at: strawberry.auto | None
updated_at: strawberry.auto | None


@strawberry.django.type(PictureComment)
Expand All @@ -41,6 +45,7 @@ class PictureCommentType:
picture: "PictureType"
text: str
created_at: strawberry.auto
updated_at: strawberry.auto | None


@strawberry.django.type(Collection)
Expand All @@ -49,6 +54,8 @@ class CollectionType:
name: str
user: "UserType"
pictures: List[PictureType]
created_at: strawberry.auto | None
updated_at: strawberry.auto | None


@strawberry.django.type(Contest)
Expand All @@ -67,6 +74,8 @@ class ContestType:
winners: List[UserType]
created_by: "UserType"
status: str
created_at: strawberry.auto | None
updated_at: strawberry.auto | None

@strawberry.field
def status(self) -> str:
Expand All @@ -92,6 +101,8 @@ class ContestSubmissionType:
picture: PictureType
submission_date: strawberry.auto
votes: List[UserType]
created_at: strawberry.auto | None
updated_at: strawberry.auto | None


@strawberry.type
Expand Down
Loading