comparison 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
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Models for the banners application.
3
4 """
5 import datetime
6
7 from django.db import models
8
9
10 class Campaign(models.Model):
11 """
12 A model to represent an ad or banner campaign.
13
14 """
15 name = models.CharField(max_length=128)
16 slug = models.SlugField()
17 creation_date = models.DateTimeField(blank=True)
18
19 def __unicode__(self):
20 return self.name
21
22 class Meta:
23 ordering = ['name']
24
25 def save(self, *args, **kwargs):
26 if not self.pk and not self.creation_date:
27 self.creation_date = datetime.datetime.now()
28
29 super(Campaign, self).save(*args, **kwargs)
30
31
32 def banner_upload_to(instance, filename):
33 """
34 An "upload_to" function for the Banner model.
35
36 """
37 return "banners/%s/%s" % (instance.campaign.slug, filename)
38
39
40 class Banner(models.Model):
41 """
42 A model to represent a banner.
43
44 """
45 campaign = models.ForeignKey(Campaign)
46 image = models.ImageField(upload_to=banner_upload_to)
47 description = models.CharField(max_length=128)
48 creation_date = models.DateTimeField(blank=True)
49
50 def __unicode__(self):
51 return self.description
52
53 class Meta:
54 ordering = ['-creation_date']
55
56 def save(self, *args, **kwargs):
57 if not self.pk and not self.creation_date:
58 self.creation_date = datetime.datetime.now()
59
60 super(Banner, self).save(*args, **kwargs)