[PATCH] models: Add timestamps to appropriate models
Stephen Finucane
stephen.finucane at intel.com
Mon May 30 20:26:26 AEST 2016
At the moment, there is no way to show when any of the models were
created or last modified. For some models, this feature would be
useful. For example: timestamps on the patch model would allow
sorting by "last updated".
Add a mixin that enables this functionality, and use the mixin in
appropriate models.
Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
patchwork/migrations/0013_add_timestamp_mixin.py | 65 ++++++++++++++++++++++
patchwork/models.py | 34 ++++++++++--
2 files changed, 94 insertions(+), 5 deletions(-)
create mode 100644 patchwork/migrations/0013_add_timestamp_mixin.py
diff --git a/patchwork/migrations/0013_add_timestamp_mixin.py b/patchwork/migrations/0013_add_timestamp_mixin.py
new file mode 100644
index 0000000..f3d60c2
--- /dev/null
+++ b/patchwork/migrations/0013_add_timestamp_mixin.py
@@ -0,0 +1,65 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import datetime
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('patchwork', '0012_add_coverletter_model'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='bundle',
+ name='created_at',
+ field=models.DateTimeField(default=datetime.datetime.now),
+ ),
+ migrations.AddField(
+ model_name='bundle',
+ name='updated_at',
+ field=models.DateTimeField(default=datetime.datetime.now),
+ ),
+ migrations.AddField(
+ model_name='person',
+ name='created_at',
+ field=models.DateTimeField(default=datetime.datetime.now),
+ ),
+ migrations.AddField(
+ model_name='person',
+ name='updated_at',
+ field=models.DateTimeField(default=datetime.datetime.now),
+ ),
+ migrations.AddField(
+ model_name='project',
+ name='created_at',
+ field=models.DateTimeField(default=datetime.datetime.now),
+ ),
+ migrations.AddField(
+ model_name='project',
+ name='updated_at',
+ field=models.DateTimeField(default=datetime.datetime.now),
+ ),
+ migrations.AddField(
+ model_name='submission',
+ name='created_at',
+ field=models.DateTimeField(default=datetime.datetime.now),
+ ),
+ migrations.AddField(
+ model_name='submission',
+ name='updated_at',
+ field=models.DateTimeField(default=datetime.datetime.now),
+ ),
+ migrations.AddField(
+ model_name='userprofile',
+ name='created_at',
+ field=models.DateTimeField(default=datetime.datetime.now),
+ ),
+ migrations.AddField(
+ model_name='userprofile',
+ name='updated_at',
+ field=models.DateTimeField(default=datetime.datetime.now),
+ ),
+ ]
diff --git a/patchwork/models.py b/patchwork/models.py
index 4d454c7..eb0849c 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -38,8 +38,28 @@ from patchwork.fields import HashField
from patchwork.parser import extract_tags, hash_patch
+class TimestampMixin(models.Model):
+ """Mixin for models with timestamps.
+
+ This mixin does not make use of the 'auto_now' and 'auto_now_add'
+ parameters, owing to the fact that we override 'save' in some
+ places where this mixin is used. See:
+ https://code.djangoproject.com/ticket/22995
+ """
+ # timestamps
+
+ created_at = models.DateTimeField(default=datetime.datetime.now)
+ updated_at = models.DateTimeField(default=datetime.datetime.now)
+
+ def pre_save(self, instance, add):
+ instance.updated_at = datetime.datetime.now()
+
+ class Meta:
+ abstract = True
+
+
@python_2_unicode_compatible
-class Person(models.Model):
+class Person(TimestampMixin, models.Model):
# properties
email = models.CharField(max_length=255, unique=True)
@@ -62,7 +82,7 @@ class Person(models.Model):
@python_2_unicode_compatible
-class Project(models.Model):
+class Project(TimestampMixin, models.Model):
# properties
linkname = models.CharField(max_length=255, unique=True)
@@ -115,7 +135,7 @@ class DelegationRule(models.Model):
@python_2_unicode_compatible
-class UserProfile(models.Model):
+class UserProfile(TimestampMixin, models.Model):
user = models.OneToOneField(User, unique=True, related_name='profile')
# projects
@@ -290,7 +310,7 @@ class EmailMixin(models.Model):
@python_2_unicode_compatible
-class Submission(EmailMixin, models.Model):
+class Submission(EmailMixin, TimestampMixin, models.Model):
# parent
project = models.ForeignKey(Project)
@@ -359,6 +379,9 @@ class Patch(Submission):
for tag in tags:
self._set_tag(tag, counter[tag])
+ # save, thus ensuring timestamps are updated
+ self.save()
+
def save(self):
if not hasattr(self, 'state') or not self.state:
self.state = get_default_initial_patch_state()
@@ -366,6 +389,7 @@ class Patch(Submission):
if self.hash is None and self.diff is not None:
self.hash = hash_patch(self.diff).hexdigest()
+ TimestampMixin.save()
super(Patch, self).save()
self.refresh_tag_counts()
@@ -484,7 +508,7 @@ class Comment(EmailMixin, models.Model):
unique_together = [('msgid', 'submission')]
-class Bundle(models.Model):
+class Bundle(TimestampMixin, models.Model):
owner = models.ForeignKey(User)
project = models.ForeignKey(Project)
name = models.CharField(max_length=50, null=False, blank=False)
--
1.7.4.1
More information about the Patchwork
mailing list