Mercurial > personas_backend
changeset 10:fd3e24f5ba18
Added some meta information to models, made save() method of Revision raise an error if client code attempts to modify a revision.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 15 Feb 2008 04:24:00 -0600 |
parents | d7e7916c7c34 |
children | 2b9ce9a2ba72 |
files | PersonasBackend/personas/models.py |
diffstat | 1 files changed, 32 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/PersonasBackend/personas/models.py Thu Feb 14 23:28:48 2008 -0600 +++ b/PersonasBackend/personas/models.py Fri Feb 15 04:24:00 2008 -0600 @@ -11,9 +11,13 @@ return self.name class Category(models.Model): + class Meta: + verbose_name_plural = "categories" + name = models.CharField( "Category name", maxlength=50, + unique=True, ) def __str__(self): @@ -119,6 +123,7 @@ choices=(("published", "Published"), ("deleted", "Deleted")), blank=False, + default="published", ) def __str__(self): @@ -176,8 +181,7 @@ >>> p = Persona(name='Test Persona', ... description='Thos is a test.', - ... category=Category.objects.get(name='General'), - ... status='published') + ... category=Category.objects.get(name='General')) >>> p.save() >>> p.revision 0 @@ -202,6 +206,9 @@ super(Persona, self).save() class Revision(models.Model): + class Meta: + unique_together = (("revision_of", "revision"),) + # Auto-generated fields revision_of = models.ForeignKey( @@ -270,3 +277,26 @@ def __str__(self): return "%s - r%s" % (self.revision_of.name, self.revision) + + def save(self): + """ + The save method for a Revision can only be called once, + because Revision objects are meant to be immutable: + + >>> p = Persona(name='Test Persona', + ... category=Category.objects.get(name='General')) + >>> p.save() + >>> p.description = 'This is a test.' + >>> p.save() + >>> r = p.revisions.get(revision=0) + >>> r.status = "deleted" + >>> r.save() + Traceback (most recent call last): + ... + AssertionError: Revisions are immutable + """ + + if self.id == None: + super(Revision, self).save() + else: + raise AssertionError( "Revisions are immutable" )