diff gpp/downloads/models.py @ 204:b4305e18d3af

Resolve ticket #74. Add user badges. Some extra credit was done here: also refactored how pending news, links, and downloads are handled.
author Brian Neal <bgneal@gmail.com>
date Sat, 01 May 2010 21:53:59 +0000
parents 341759e1cda1
children 272d3a8c98e8
line wrap: on
line diff
--- a/gpp/downloads/models.py	Wed Apr 28 03:00:31 2010 +0000
+++ b/gpp/downloads/models.py	Sat May 01 21:53:59 2010 +0000
@@ -45,16 +45,44 @@
                 is_public=True).select_related()
 
 
-class Download(models.Model):
-    """Model to represent a download."""
+class DownloadBase(models.Model):
+    """Abstract model to collect common download fields."""
     title = models.CharField(max_length=128)
     category = models.ForeignKey(Category)
     description = models.TextField()
     html = models.TextField(blank=True)
     file = models.FileField(upload_to=download_path)
     user = models.ForeignKey(User)
-    date_added = models.DateTimeField(auto_now_add=True)
+    date_added = models.DateTimeField()
     ip_address = models.IPAddressField('IP Address')
+
+    class Meta:
+        abstract = True
+
+    def size(self):
+        return filesizeformat(self.file.size)
+
+
+class PendingDownload(DownloadBase):
+    """This model represents pending downloads created by users. These pending
+    downloads must be approved by an admin before they turn into "real"
+    Downloads and are visible on site.
+    """
+    class Meta:
+        ordering = ('date_added', )
+
+    def __unicode__(self):
+        return self.title
+
+    def save(self, *args, **kwargs):
+        if not self.pk:
+            self.date_added = datetime.datetime.now()
+        self.html = site_markup(self.description)
+        super(PendingDownload, self).save(*args, **kwargs)
+
+
+class Download(DownloadBase):
+    """Model to represent a download."""
     hits = models.IntegerField(default=0)
     average_score = models.FloatField(default=0.0)
     total_votes = models.IntegerField(default=0)
@@ -83,9 +111,6 @@
         self.average_score = total_score / self.total_votes
         return self.average_score
 
-    def size(self):
-        return filesizeformat(self.file.size)
-
 
 class AllowedExtensionManager(models.Manager):
     def get_extension_list(self):