annotate banners/models.py @ 661:15dbe0ccda95

Prevent exceptions when viewing downloads in the admin when the file doesn't exist on the filesystem. This is usually seen in development but can also happen in production if the file is missing.
author Brian Neal <bgneal@gmail.com>
date Tue, 14 May 2013 21:02:47 -0500
parents ee87ea74d46b
children
rev   line source
bgneal@558 1 """
bgneal@558 2 Models for the banners application.
bgneal@558 3
bgneal@558 4 """
bgneal@558 5 import datetime
bgneal@558 6
bgneal@558 7 from django.db import models
bgneal@558 8
bgneal@558 9
bgneal@558 10 class Campaign(models.Model):
bgneal@558 11 """
bgneal@558 12 A model to represent an ad or banner campaign.
bgneal@558 13
bgneal@558 14 """
bgneal@558 15 name = models.CharField(max_length=128)
bgneal@558 16 slug = models.SlugField()
bgneal@558 17 creation_date = models.DateTimeField(blank=True)
bgneal@558 18
bgneal@558 19 def __unicode__(self):
bgneal@558 20 return self.name
bgneal@558 21
bgneal@558 22 class Meta:
bgneal@558 23 ordering = ['name']
bgneal@558 24
bgneal@558 25 def save(self, *args, **kwargs):
bgneal@558 26 if not self.pk and not self.creation_date:
bgneal@558 27 self.creation_date = datetime.datetime.now()
bgneal@558 28
bgneal@558 29 super(Campaign, self).save(*args, **kwargs)
bgneal@558 30
bgneal@558 31
bgneal@558 32 def banner_upload_to(instance, filename):
bgneal@558 33 """
bgneal@558 34 An "upload_to" function for the Banner model.
bgneal@558 35
bgneal@558 36 """
bgneal@558 37 return "banners/%s/%s" % (instance.campaign.slug, filename)
bgneal@558 38
bgneal@558 39
bgneal@558 40 class Banner(models.Model):
bgneal@558 41 """
bgneal@558 42 A model to represent a banner.
bgneal@558 43
bgneal@558 44 """
bgneal@558 45 campaign = models.ForeignKey(Campaign)
bgneal@558 46 image = models.ImageField(upload_to=banner_upload_to)
bgneal@558 47 description = models.CharField(max_length=128)
bgneal@558 48 creation_date = models.DateTimeField(blank=True)
bgneal@558 49
bgneal@558 50 def __unicode__(self):
bgneal@558 51 return self.description
bgneal@558 52
bgneal@558 53 class Meta:
bgneal@558 54 ordering = ['-creation_date']
bgneal@558 55
bgneal@558 56 def save(self, *args, **kwargs):
bgneal@558 57 if not self.pk and not self.creation_date:
bgneal@558 58 self.creation_date = datetime.datetime.now()
bgneal@558 59
bgneal@558 60 super(Banner, self).save(*args, **kwargs)