bgneal@558: """ bgneal@558: Models for the banners application. bgneal@558: bgneal@558: """ bgneal@558: import datetime bgneal@558: bgneal@558: from django.db import models bgneal@558: bgneal@558: bgneal@558: class Campaign(models.Model): bgneal@558: """ bgneal@558: A model to represent an ad or banner campaign. bgneal@558: bgneal@558: """ bgneal@558: name = models.CharField(max_length=128) bgneal@558: slug = models.SlugField() bgneal@558: creation_date = models.DateTimeField(blank=True) bgneal@558: bgneal@558: def __unicode__(self): bgneal@558: return self.name bgneal@558: bgneal@558: class Meta: bgneal@558: ordering = ['name'] bgneal@558: bgneal@558: def save(self, *args, **kwargs): bgneal@558: if not self.pk and not self.creation_date: bgneal@558: self.creation_date = datetime.datetime.now() bgneal@558: bgneal@558: super(Campaign, self).save(*args, **kwargs) bgneal@558: bgneal@558: bgneal@558: def banner_upload_to(instance, filename): bgneal@558: """ bgneal@558: An "upload_to" function for the Banner model. bgneal@558: bgneal@558: """ bgneal@558: return "banners/%s/%s" % (instance.campaign.slug, filename) bgneal@558: bgneal@558: bgneal@558: class Banner(models.Model): bgneal@558: """ bgneal@558: A model to represent a banner. bgneal@558: bgneal@558: """ bgneal@558: campaign = models.ForeignKey(Campaign) bgneal@558: image = models.ImageField(upload_to=banner_upload_to) bgneal@558: description = models.CharField(max_length=128) bgneal@558: creation_date = models.DateTimeField(blank=True) bgneal@558: bgneal@558: def __unicode__(self): bgneal@558: return self.description bgneal@558: bgneal@558: class Meta: bgneal@558: ordering = ['-creation_date'] bgneal@558: bgneal@558: def save(self, *args, **kwargs): bgneal@558: if not self.pk and not self.creation_date: bgneal@558: self.creation_date = datetime.datetime.now() bgneal@558: bgneal@558: super(Banner, self).save(*args, **kwargs)