changeset 7:0eed448723cb

Decided to use a separate table for persona revisions after all. Tested this model out manually and it seems to work okay.
author Atul Varma <varmaa@toolness.com>
date Thu, 14 Feb 2008 19:08:23 -0600
parents 37be5ab15cea
children bde567ef753d
files PersonasBackend/personas/models.py
diffstat 1 files changed, 125 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/PersonasBackend/personas/models.py	Thu Feb 14 17:58:31 2008 -0600
+++ b/PersonasBackend/personas/models.py	Thu Feb 14 19:08:23 2008 -0600
@@ -10,6 +10,10 @@
 
     '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 = {}
@@ -19,7 +23,25 @@
         newValue = changes[attr]
         if origValue != newValue:
             delta[attr] = origValue
-    # TODO: Finish this.
+
+    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(
@@ -28,44 +50,39 @@
         help_text="This is the user's Mozilla ID.",
         )
 
+    def __str__(self):
+        return self.name
+
 class Category(models.Model):
     name = models.CharField(
         "Category name",
         maxlength=50,
         )
 
+    def __str__(self):
+        return self.name
+
 class Persona(models.Model):
+    MAX_NAME_LENGTH = 50
+
     name = models.CharField(
         "Title of the Persona",
-        maxlength=50,
-        null=True,
-        )
-
-    revision_of = models.ForeignKey(
-        "self",
-        verbose_name="The Persona that this record is an old revision of",
-        null=True,
-        related_name="revisions",
+        maxlength=MAX_NAME_LENGTH,
+        blank=False,
         )
 
     category = models.ForeignKey(
         Category,
         verbose_name="Category that the Persona is filed under",
-        null=True,
         related_name="personas",
-        )
-
-    creator = models.ForeignKey(
-        User,
-        verbose_name="Creator of the Persona",
-        null=True,
-        related_name="created_personas",
+        null=False,
         )
 
     description = models.TextField(
         "Description of the Persona",
         help_text = "HTML is allowed.",
-        null=True,
+        # TODO: Really allow blank descriptions?
+        blank=True,
         )
 
     header_img = models.URLField(
@@ -74,7 +91,7 @@
                    "the browser's top chrome.  Only needs to be "
                    "provided if no URL for the Persona is supplied."),
         verify_exists=True,
-        null=True,
+        blank=True,
         )
 
     footer_img = models.URLField(
@@ -83,7 +100,7 @@
                    "the browser's bottom chrome.  Only needs to be "
                    "provided if no URL for the Persona is supplied."),
         verify_exists=True,
-        null=True,
+        blank=True,
         )
 
     url = models.URLField(
@@ -92,7 +109,7 @@
                    "if no header/footer image for the Persona is "
                    "supplied."),
         verify_exists=True,
-        null=True,
+        blank=True,
         )
 
     update_interval = models.TimeField(
@@ -104,41 +121,118 @@
         null=True,
         )
 
+    MAX_COLOR_SCHEME_LENGTH = 10
+
     color_scheme = models.CharField(
         "The Persona's color scheme",
         help_text=("If 'light', any text displayed over the "
                    "Persona will be dark.  If 'dark', any text "
                    "displayed over the Persona will be light."),
-        maxlength=5,
+        maxlength=MAX_COLOR_SCHEME_LENGTH,
         choices=(("light", "Light"),
                  ("dark", "Dark")),
-        null=True,
+        blank=False,
         )
 
     date_updated = models.DateTimeField(
         "Update date for this revision",
         auto_now=True,
-        null=True,
+        null=False,
         )
 
     updater = models.ForeignKey(
         User,
         verbose_name="User who made this revision",
-        null=True,
-        related_name="updated_personas",
+        related_name="personas",
+        null=False,
         )
 
     revision = models.PositiveIntegerField(
         "Revision number for the Persona",
         help_text=("This number is incremented whenever the Persona "
                    "is changed."),
+        null=False,
+        )
+
+    MAX_STATUS_LENGTH = 10
+
+    status = models.CharField(
+        "Current status of the Persona",
+        maxlength=MAX_STATUS_LENGTH,
+        choices=(("published", "Published"),
+                 ("deleted", "Deleted")),
+        blank=False,
+        )
+
+    def __str__(self):
+        return self.name
+
+class Revision(models.Model):
+    # Non-null fields
+
+    revision_of = models.ForeignKey(
+        Persona,
+        verbose_name="The Persona that this record is an old revision of",
+        related_name="revisions",
+        null=False,
+        )
+
+    date_updated = models.DateTimeField(
+        null=False,
+        )
+
+    updater = models.ForeignKey(
+        User,
+        related_name="revisions",
+        null=False,
+        )
+
+    revision = models.PositiveIntegerField(
+        null=False,
+        )
+
+    # Null fields
+
+    name = models.CharField(
+        maxlength=Persona.MAX_NAME_LENGTH,
+        null=True,
+        )
+
+    category = models.ForeignKey(
+        Category,
+        related_name="revisions",
+        null=True,
+        )
+
+    description = models.TextField(
+        null=True,
+        )
+
+    header_img = models.URLField(
+        null=True,
+        )
+
+    footer_img = models.URLField(
+        null=True,
+        )
+
+    url = models.URLField(
+        null=True,
+        )
+
+    update_interval = models.TimeField(
+        null=True,
+        )
+
+    color_scheme = models.CharField(
+        maxlength=Persona.MAX_COLOR_SCHEME_LENGTH,
         null=True,
         )
 
     status = models.CharField(
-        "Current status of the Persona",
-        maxlength=10,
-        choices=(("published", "Published"),
-                 ("deleted", "Deleted")),
+        maxlength=Persona.MAX_STATUS_LENGTH,
         null=True,
         )
+
+    def __str__(self):
+        return "%s - r%s" % (self.revision_of.name, self.revision)