Mercurial > personas_backend
changeset 8:bde567ef753d
Added a unit test, refactored a commit function into the save method of the Persona model class.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 14 Feb 2008 23:13:16 -0600 |
parents | 0eed448723cb |
children | d7e7916c7c34 |
files | PersonasBackend/personas/models.py |
diffstat | 1 files changed, 84 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/PersonasBackend/personas/models.py Thu Feb 14 19:08:23 2008 -0600 +++ b/PersonasBackend/personas/models.py Thu Feb 14 23:13:16 2008 -0600 @@ -1,48 +1,5 @@ from django.db import models -def commitRevision( persona, user, changes ): - """ - Commit changes to a persona as a user. - - 'persona' is the Persona object being changed. - - 'user' is the User object making the changes. - - 'changes' is a dictionary whose keys are the names of attributes - in 'persona' to change, with their new values. - - Returns True if the commit was successful. If the commit actually - would've resulted in no changes being made to the persona, False - is returned. - """ - - delta = {} - - for attr in changes: - origValue = getattr( persona, attr ) - newValue = changes[attr] - if origValue != newValue: - delta[attr] = origValue - - if not delta: - return False - - rev = Revision( - revision_of = persona, - date_updated = persona.date_updated, - updater = user, - revision = persona.revision - ) - - for attr in delta: - setattr( persona, attr, changes[attr] ) - setattr( rev, attr, delta[attr] ) - - persona.save() - rev.save() - - return True - class User(models.Model): name = models.CharField( "Name of the user", @@ -144,7 +101,7 @@ User, verbose_name="User who made this revision", related_name="personas", - null=False, + null=True, ) revision = models.PositiveIntegerField( @@ -167,8 +124,88 @@ def __str__(self): return self.name + VERSIONED_PROPERTIES = [ + "name", + "category", + "description", + "header_img", + "footer_img", + "url", + "update_interval", + "color_scheme", + "status", + ] + + def __makeNewRevision(self): + """ + Detect if any of our versioned properties have changed, and if + so, make a new revision. + """ + + original = Persona.objects.get(id=self.id) + + assert original.revision == self.revision + assert original.date_updated == self.date_updated + + delta = {} + for attr in self.VERSIONED_PROPERTIES: + origValue = getattr(original, attr) + newValue = getattr(self, attr) + if origValue != newValue: + delta[attr] = origValue + if delta: + rev = Revision( + revision_of = original, + date_updated = original.date_updated, + updater = original.updater, + revision = original.revision + ) + + for attr in delta: + setattr(rev, attr, delta[attr]) + rev.save() + + self.revision += 1 + + def save(self): + """ + Saves the object. If a versioned property is changed, a new + revision is generated automatically. + + Example: + + >>> c = Category(name='General') + >>> c.save() + + >>> p = Persona(name='Test Persona', + ... description='Thos is a test.', + ... category=c, + ... status='published') + >>> p.save() + >>> p.revision + 0 + >>> p.revisions.all() + [] + + >>> p.description = 'This is a test.' + >>> p.save() + >>> p.revision + 1 + >>> p.revisions.all() + [<Revision: Test Persona - r0>] + >>> p.revisions.get(revision=0).description + 'Thos is a test.' + """ + + if self.id == None: + self.revision = 0 + else: + self.__makeNewRevision() + + super(Persona, self).save() + class Revision(models.Model): - # Non-null fields + # Auto-generated fields revision_of = models.ForeignKey( Persona, @@ -184,14 +221,14 @@ updater = models.ForeignKey( User, related_name="revisions", - null=False, + null=True, ) revision = models.PositiveIntegerField( null=False, ) - # Null fields + # Versioned fields name = models.CharField( maxlength=Persona.MAX_NAME_LENGTH,