diff banners/models.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/banners/models.py@9a68fd3bd8a5
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/banners/models.py	Sat May 05 17:10:48 2012 -0500
@@ -0,0 +1,60 @@
+"""
+Models for the banners application.
+
+"""
+import datetime
+
+from django.db import models
+
+
+class Campaign(models.Model):
+    """
+    A model to represent an ad or banner campaign.
+
+    """
+    name = models.CharField(max_length=128)
+    slug = models.SlugField()
+    creation_date = models.DateTimeField(blank=True)
+
+    def __unicode__(self):
+        return self.name
+
+    class Meta:
+        ordering = ['name']
+
+    def save(self, *args, **kwargs):
+        if not self.pk and not self.creation_date:
+            self.creation_date = datetime.datetime.now()
+
+        super(Campaign, self).save(*args, **kwargs)
+
+
+def banner_upload_to(instance, filename):
+    """
+    An "upload_to" function for the Banner model.
+
+    """
+    return "banners/%s/%s" % (instance.campaign.slug, filename)
+
+
+class Banner(models.Model):
+    """
+    A model to represent a banner.
+
+    """
+    campaign = models.ForeignKey(Campaign)
+    image = models.ImageField(upload_to=banner_upload_to)
+    description = models.CharField(max_length=128)
+    creation_date = models.DateTimeField(blank=True)
+
+    def __unicode__(self):
+        return self.description
+
+    class Meta:
+        ordering = ['-creation_date']
+
+    def save(self, *args, **kwargs):
+        if not self.pk and not self.creation_date:
+            self.creation_date = datetime.datetime.now()
+
+        super(Banner, self).save(*args, **kwargs)