summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2012-12-28 04:13:56 +0100
committerDan McGee <dan@archlinux.org>2012-12-28 07:35:26 +0100
commitbf4385a26c1b6f07bf9bdcddf7160b5eb4a71d9a (patch)
tree05485a88f64655f6d8134e2a24b2b5482d0e4a67
parent6667b017669ffbae98ce70f5472f107b91da36d5 (diff)
downloadarchweb-bf4385a26c1b6f07bf9bdcddf7160b5eb4a71d9a.tar.gz
archweb-bf4385a26c1b6f07bf9bdcddf7160b5eb4a71d9a.tar.xz
Move the body of set_last_modified to main/utils
Instead of having multiple methods, move this into our single 'created' setter method. If the 'last_modified' property is present, we now update it accordingly when saving any model with this signal attached. Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r--devel/models.py14
-rw-r--r--main/utils.py10
2 files changed, 10 insertions, 14 deletions
diff --git a/devel/models.py b/devel/models.py
index f30bba8..5f0a831 100644
--- a/devel/models.py
+++ b/devel/models.py
@@ -8,7 +8,7 @@ from django.utils.timezone import now
from django_countries import CountryField
from .fields import PGPKeyField
-from main.utils import make_choice
+from main.utils import make_choice, set_created_field
class UserProfile(models.Model):
@@ -104,17 +104,7 @@ class PGPSignature(models.Model):
return u'%s → %s' % (self.signer, self.signee)
-def set_last_modified(sender, **kwargs):
- '''This will set the 'last_modified' field on the user profile to the
- current UTC time when either the profile is updated. For use as a pre_save
- signal handler.'''
- obj = kwargs['instance']
- if hasattr(obj, 'last_modified'):
- obj.last_modified = now()
-
-
-# connect signals needed to keep cache in line with reality
-pre_save.connect(set_last_modified, sender=UserProfile,
+pre_save.connect(set_created_field, sender=UserProfile,
dispatch_uid="devel.models")
# vim: set ts=4 sw=4 et:
diff --git a/main/utils.py b/main/utils.py
index d12e5e1..17ca386 100644
--- a/main/utils.py
+++ b/main/utils.py
@@ -106,10 +106,16 @@ def retrieve_latest(sender, latest_by=None):
def set_created_field(sender, **kwargs):
'''This will set the 'created' field on any object to the current UTC time
- if it is unset. For use as a pre_save signal handler.'''
+ if it is unset.
+ Additionally, this will set the 'last_modified' field on any object to the
+ current UTC time on any save of the object.
+ For use as a pre_save signal handler.'''
obj = kwargs['instance']
+ time = now()
if hasattr(obj, 'created') and not obj.created:
- obj.created = now()
+ obj.created = time
+ if hasattr(obj, 'last_modified'):
+ obj.last_modified = time
def database_vendor(model, mode='read'):