]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/photo/models.py
1e2cc6842da83f4c41f3311d7f761dc1f0257209
[sommitrealweird.git] / sommitrealweird / photo / models.py
1 from django.db import models
2 from django.conf import settings
3
4 class Album(models.Model):
5     name = models.CharField(max_length=120)
6     caption = models.TextField(blank=True, null=True)
7     slug = models.SlugField()
8
9     def get_main_image(self):
10         main_photo = Photo.objects.filter(album=self).order_by('order','image')[0]
11         return main_photo.image
12
13     def __unicode__(self):
14         return "%s" %(self.name,)
15
16 def get_upload_path(photo, filename):
17     if photo.album:
18         return "photos/%s/%s" %(photo.album.slug, filename)
19     else:
20         return "photos/%s" %(filename)
21
22 class Photo(models.Model):
23     image = models.ImageField(upload_to=get_upload_path, null=True)
24     caption = models.TextField(blank=True, null=True)
25     order = models.IntegerField(blank=True, null=True)
26     album = models.ForeignKey('Album')
27
28     def __unicode__(self):
29         return "%s" %(self.image)