summaryrefslogtreecommitdiff
path: root/app/photos/models.py
blob: f0d554074d3f3e71973d258e7837bca6157e5b81 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import os.path
import datetime
from PIL import Image

from django.contrib.gis.db import models
from django.contrib.sitemaps import Sitemap
from django.utils.encoding import force_text
from django.conf import settings

from taggit.managers import TaggableManager
from locations.models import Location, Region


def get_upload_path(self, filename):
    return "images/galleries/original/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)


class LuxImage(models.Model):
    image = models.FileField(blank=True, null=True, upload_to=get_upload_path)
    title = models.CharField(null=True, blank=True, max_length=300)
    caption = models.TextField(blank=True, null=True)
    pub_date = models.DateTimeField()
    exif_raw = models.TextField(blank=True, null=True)
    exif_aperture = models.CharField(max_length=50, blank=True, null=True)
    exif_make = models.CharField(max_length=50, blank=True, null=True)
    exif_model = models.CharField(max_length=50, blank=True, null=True)
    exif_exposure = models.CharField(max_length=50, blank=True, null=True)
    exif_iso = models.CharField(max_length=50, blank=True, null=True)
    exif_focal_length = models.CharField(max_length=50, blank=True, null=True)
    exif_lens = models.CharField(max_length=50, blank=True, null=True)
    exif_date = models.DateTimeField(blank=True, null=True)
    height = models.CharField(max_length=6, blank=True, null=True)
    width = models.CharField(max_length=6, blank=True, null=True)
    point = models.PointField(null=True, blank=True)
    location = models.ForeignKey(Location, null=True, blank=True)
    is_public = models.BooleanField(default=True)
    is_video = models.BooleanField(default=False)
    flickr_id = models.CharField(null=True, blank=True, max_length=80)

    class Meta:
        ordering = ('-pub_date', 'id')
        verbose_name_plural = 'Images'
        get_latest_by = 'pub_date'

    def __str__(self):
        return "%s" % self.pk

    def get_image_name(self):
        return self.image.url.split("galleries/original/")[1]

    def get_image_size(self, size="original"):
        base = self.get_image_name()
        return "%sgalleries/%s/%s" % (settings.IMAGES_URL, size, base)

    def admin_thumbnail(self):
        return force_text('<a href="%s"><img src="%s"></a>' % (self.image.url, self.image.url))
    admin_thumbnail.allow_tags = True
    admin_thumbnail.short_description = 'Thumbnail'


class LuxGallery(models.Model):
    title = models.CharField(blank=True, max_length=300)
    description = models.TextField(blank=True, null=True)
    slug = models.CharField(blank=True, max_length=300)
    thumb = models.CharField(blank=True, max_length=300)
    image = models.ManyToManyField(LuxImage)
    pub_date = models.DateTimeField(null=True)
    point = models.PointField(null=True, blank=True)
    location = models.ForeignKey(Location, null=True, blank=True)
    is_public = models.BooleanField(default=True)

    class Meta:
        ordering = ('-pub_date', 'id')
        verbose_name_plural = 'Galleries'
        get_latest_by = 'pub_date'

    def __str__(self):
        return self.title

    def get_main_image(self):
        return "%sgallery_thumbs/%s.jpg" % (settings.IMAGES_URL, self.id)

    def get_absolute_url(self):
        return "/photos/galleries/%s/" % (self.slug)


class Photo(models.Model):
    description = models.TextField(blank=True, null=True)
    title = models.CharField(blank=True, max_length=300)
    pub_date = models.DateTimeField()
    tags = TaggableManager(blank=True)
    exif_aperture = models.CharField(max_length=50, blank=True, null=True)
    exif_make = models.CharField(max_length=50, blank=True, null=True)
    exif_model = models.CharField(max_length=50, blank=True, null=True)
    exif_exposure = models.CharField(max_length=50, blank=True, null=True)
    exif_iso = models.CharField(max_length=50, blank=True, null=True)
    exif_focal_length = models.CharField(max_length=50, blank=True, null=True)
    exif_lens = models.CharField(max_length=50, blank=True, null=True)
    exif_date = models.DateTimeField()
    """Flickr Specific Stuff"""
    # Vlickr id is varchar since Flickr ids are larger than than integerfield can handle and BigIntegerField gets weird in Postgres.
    flickr_id = models.CharField(max_length=300)
    flickr_owner = models.CharField(max_length=20)
    flickr_server = models.IntegerField()
    flickr_farm = models.IntegerField()
    flickr_secret = models.CharField(max_length=50)
    flickr_originalsecret = models.CharField(max_length=50)
    lon = models.FloatField('Longitude', help_text="Longitude of centerpoint", null=True)
    lat = models.FloatField('Latitude', help_text="Latitude of centerpoint", null=True)
    location = models.ForeignKey(Location, null=True)
    region = models.ForeignKey(Region, null=True)
    slideshowimage_width = models.CharField(max_length=4, blank=True, null=True)
    slideshowimage_height = models.CharField(max_length=4, blank=True, null=True)
    slideshowimage_margintop = models.CharField(max_length=4, blank=True, null=True)
    slideshowimage_marginleft = models.CharField(max_length=4, blank=True, null=True)
    is_public = models.BooleanField(default=True)

    class Meta:
        ordering = ('-pub_date',)

    def admin_thumbnail(self):
        return force_text('<a href="%s"><img src="%s"></a>' % (self.get_absolute_url(), self.get_small_square_url()))
    admin_thumbnail.allow_tags = True
    admin_thumbnail.short_description = 'Thumbnail'

    def get_local_medium_url(self):
        return '%sflickr/med/%s/%s.jpg' % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), self.flickr_id)

    def get_local_orig_url(self):
        return '%sflickr/full/%s/%s.jpg' % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), self.flickr_id)

    def get_local_slideshow_url(self):
        return '%sslideshow/%s/%s.jpg' % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), self.flickr_id)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return "/photo/%s/" % (self.id)

    def get_model_name(self):
        return 'photo'

    def get_small_square_url(self):
        return self.get_pic_url(size="small_square")

    def get_large_url(self):
        return self.get_pic_url(size="large")

    def get_small_url(self):
        return self.get_pic_url(size="small")

    def get_medium_url(self):
        return self.get_pic_url(size="medium")

    def get_original_url(self):
        # return self.get_pic_url(size="original")
        return "http://farm%s.static.flickr.com/%s/%s_%s_o.jpg" % (self.flickr_farm, self.flickr_server, self.flickr_id, self.flickr_originalsecret)

    def get_retina_slideshow_url(self):
        return '%sslideshow/%s/%sx2.jpg' % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), self.flickr_id)

    def has_retina(self):
        return os.path.isfile('%s/slideshow/%s/%sx2.jpg' % (settings.IMAGES_ROOT, self.pub_date.strftime("%Y"), self.flickr_id))

    @property
    def get_height(self):
        im = Image.open('%s/slideshow/%s/%s.jpg' % (settings.IMAGES_ROOT,
                        self.pub_date.strftime("%Y"), self.flickr_id))
        xsize, ysize = im.size
        return ysize

    @property
    def get_width(self):
        im = Image.open('%s/slideshow/%s/%s.jpg' % (settings.IMAGES_ROOT,
                        self.pub_date.strftime("%Y"), self.flickr_id))
        xsize, ysize = im.size
        return xsize

    @property
    def get_margin_top(self):
        im = Image.open('%s/slideshow/%s/%s.jpg' % (settings.IMAGES_ROOT,
                        self.pub_date.strftime("%Y"), self.flickr_id))
        xsize, ysize = im.size
        mtop = 340 - (ysize / 2)
        return mtop

    @property
    def get_margin_left(self):
        im = Image.open('%s/slideshow/%s/%s.jpg' % (settings.IMAGES_ROOT,
                        self.pub_date.strftime("%Y"), self.flickr_id))
        xsize, ysize = im.size
        mtop = 500 - (xsize / 2)
        return mtop

    @property
    def flickr_link(self):
        return '%s%s/' % ('http://www.flickr.com/photos/luxagraf/', self.flickr_id)

    @property
    def is_portait(self):
        if int(self.slideshowimage_height) > int(self.slideshowimage_width):
            return True
        else:
            return False

    def get_pic_url(self, size='small'):
        # small_square=75x75
        # thumb=100 on longest side
        # small=240 on longest side
        # medium=500 on longest side
        # large=1024 on longest side
        # original=duh
        base_url = "http://static.flickr.com"
        size_char = 's'  # default to small_square
        if size == 'small_square':
            size_char = '_s'
        elif size == 'thumb':
            size_char = '_t'
        elif size == 'small':
            size_char = '_m'
        elif size == 'medium':
            size_char = ''
        elif size == 'large':
            size_char = '_b'
        elif size == 'original':
            size_char = '_o'

        return "http://farm%s.static.flickr.com/%s/%s_%s%s.jpg" % (self.flickr_farm, self.flickr_server, self.flickr_id, self.flickr_secret, size_char)

    def get_tumble_image(self):
        return "%s/crops/%s/%s.jpg" % (settings.IMAGES_URL, self.pub_date.strftime("%Y/%b").lower(), self.id)

    def get_previous_published(self):
        return self.get_previous_by_pub_date()

    def get_next_published(self):
        return self.get_next_by_pub_date()

    def comment_period_open(self):
        return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date

    def save(self, *args, **kwargs):
        super(Photo, self).save()


class PhotoGallery(models.Model):
    set_id = models.CharField(blank=True, max_length=300)
    set_title = models.CharField(blank=True, max_length=300)
    set_desc = models.TextField(blank=True, null=True)
    set_slug = models.CharField(blank=True, max_length=300)
    primary = models.CharField(blank=True, max_length=300)
    photos = models.ManyToManyField(Photo)
    location = models.ForeignKey(Location, null=True)
    region = models.ForeignKey(Region, null=True)
    pub_date = models.DateTimeField(null=True)

    class Meta:
        ordering = ('-pub_date', 'id')
        verbose_name_plural = 'Photo Galleries'
        get_latest_by = 'pub_date'

    def __str__(self):
        return self.set_title

    def get_main_image(self):
        return "%sgallery_thumbs/%s.jpg" % (settings.IMAGES_URL, self.id)

    def get_absolute_url(self):
        return "/photos/galleries/%s/" % (self.set_slug)


class PhotoGallerySitemap(Sitemap):
    changefreq = "never"
    priority = 0.7
    protocol = "https"

    def items(self):
        return PhotoGallery.objects.all()

    def lastmod(self, obj):
        return obj.pub_date