-
Notifications
You must be signed in to change notification settings - Fork 10
/
models.py
155 lines (127 loc) · 4.41 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
Copyright (c) 2008, Myles Braithwaite
All rights reserved.
This software is provided without warranty under the terms of the BSD
license included in photos/LICENSE.markdown and may be redistributed only under
the conditions described in the aforementioned license. This license is also
available online at http://code.google.com/p/django-photo-gallery/wiki/License
Author: Myles Braithwaite
"""
import os
from PIL import Image
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.models import permalink
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.conf import settings
from django.utils.text import truncate_words
from photos.managers import *
class Module(models.Model):
"""
Photo gallery site.
"""
MODULE_TYPE_CHOICES = (
(1, 'Footer'),
(2, 'Description'),
)
site = models.ForeignKey(Site)
module_type = models.IntegerField(_('module type'), choices=MODULE_TYPE_CHOICES)
body = models.TextField(_('body'), help_text=_("Use Raw HTML"))
class Meta:
verbose_name = _('module')
verbose_name_plural = _('modules')
db_table = 'photo_modules'
ordering = ('module_type', 'site',)
def __unicode__(self):
return "%s - %s" % (self.site, self.module_type)
class Gallery(models.Model):
"""
Gallery model.
"""
title = models.CharField(_('title'), max_length=100)
slug = models.SlugField(_('slug'), unique=True)
description = models.TextField(_('description'), blank=True, null=True)
created = models.DateTimeField(_('created'), auto_now_add=True)
modified = models.DateTimeField(_('modified'), auto_now=True)
class Meta:
verbose_name = _('gallery')
verbose_name_plural = _('galleries')
db_table = 'photo_galleries'
ordering = ('title',)
def __unicode__(self):
return u"%s" % self.title
@permalink
def get_absolute_url(self):
return ('photo_gallery_title', None, {
'gallery_slug' : self.slug
})
@permalink
def get_gallery_url(self):
return ('photo_gallery_detail', None, {
'gallery_slug' : self.slug
})
@property
def latest_photo(self):
photo = Photo.objects.filter(gallery=self).order_by('-created')[0]
return photo
@property
def description_truncate(self):
return u"%s" % truncate_words(self.description, 20)
@property
def photo_count(self):
"""
How many photos are in a given gallery.
"""
count = Photo.objects.filter(gallery=self).count()
return u"%s" % count
@property
def date_last_photo(self):
"""
Date of last photo uploaded to the gallery.
"""
photo = Photo.objects.order_by('-created')[:0]
return photo.created
class Photo(models.Model):
"""
Photo model.
"""
title = models.CharField(_('title'), max_length=200)
slug = models.SlugField(_('slug'), unique=True)
location = models.CharField(_('location'), max_length=50, blank=True, null=True)
description = models.TextField(_('description'), blank=True, null=True)
gallery = models.ForeignKey(Gallery)
favorite = models.BooleanField(_('favorite'), default=False)
original = models.ImageField(_('original'), upload_to='photos/o/%Y-%m-%d')
large = models.ImageField(_('large'), upload_to='photos/l/%Y-%m-%d', editable=False) # Image size no greater than 480x480
thumbnail = models.ImageField(_('thumbnail'), upload_to='photos/t/%Y-%m-%d', editable=False) # Image size no greater than 220x220
small = models.ImageField(_('small'), upload_to='photos/s/%Y-%m-%d', editable=False) # Image size no greater than 90x90
created = models.DateTimeField(_('created'), auto_now_add=True)
modified = models.DateTimeField(_('modified'), auto_now=True)
class Meta:
verbose_name = _('photo')
verbose_name_plural = _('photos')
db_table = 'photos'
ordering = ('modified',)
def __unicode__(self):
return u"%s" % self.title
@permalink
def get_absolute_url(self):
return ('photo_gallery_photo_detail', None, {
'photo_slug' : self.slug,
'gallery_slug' : self.gallery.slug,
})
def save(self):
LARGE_SIZE = (480, 480)
THUMBNAIL_SIZE = (220, 220)
SMALL_SIZE = (90, 90)
# TODO: Still thinking of a good way to to this.
# Need to convert the Photos to Large and Small thumbnails.
super(Photo, self).save()
def admin_thumbnail(self):
"""
Adds a photo thumbnail to the administration interface.
"""
return u"<img src=\"%s/%s\" />" % (settings.MEDIA_URL, self.small)
admin_thumbnail.sort_description = 'Thumbnail'
admin_thumbnail.allow_tags = True