Skip to content

Fix issue #200: Add timestamps to models demo 2 #201

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-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),
),
]
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
39 changes: 39 additions & 0 deletions photo/tests/test_timestamps.py
Original file line number Diff line number Diff line change
@@ -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)
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
updated_at: strawberry.auto


@strawberry.django.type(Picture)
Expand All @@ -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)
Expand All @@ -41,6 +45,7 @@ class PictureCommentType:
picture: "PictureType"
text: str
created_at: strawberry.auto
updated_at: strawberry.auto


@strawberry.django.type(Collection)
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand Down
Loading