-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
executable file
·87 lines (69 loc) · 2.78 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.db import models
from datetime import datetime
from django.contrib.auth.models import User
from PIL import Image
import os
import settings
class Album(models.Model):
author = models.ForeignKey(User)
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)
description = models.TextField(blank=True)
is_published = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
publish = models.DateTimeField(default=datetime.now)
enable_comments = models.BooleanField(default=True)
def __unicode__(self):
dt = unicode(self.publish)[:10]
return "[%s] %s" % (dt, self.title)
def get_cover_image(self):
""" Get cover or first image """
covers = self.photo_set.filter(is_cover=True)[:1]
if len(covers) > 0:
cover = covers[0].image
return cover
else:
first_image = self.photo_set.all()[0]
return first_image.image
@models.permalink
def get_absolute_url(self):
return ('album_detail',[self.id])
def get_image_path(instance, filename):
return os.path.join('gallery', unicode(instance.album.slug), filename)
class Photo(models.Model):
author = models.ForeignKey(User, blank=True, null=True)
album = models.ForeignKey(Album, blank=True, null=True)
title = models.CharField(max_length=100, blank=True, null=True)
image = models.ImageField(upload_to=get_image_path)
description = models.TextField(blank=True)
is_cover = models.BooleanField(default=False)
position = models.PositiveIntegerField(default=0)
is_published = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
publish = models.DateTimeField(default=datetime.now)
enable_comments = models.BooleanField(default=True)
def save(self, size=(1280, 1280)):
if not self.id and not self.image:
return
super(Photo, self).save()
filename = settings.MEDIA_ROOT + self.image.name
image = Image.open(filename)
image.thumbnail(size, Image.ANTIALIAS)
image.save(filename, quality=95)
#self.image = get_thumbnailer(self.image).get_thumbnail(dict(size=size))
def __unicode__(self):
return "[%s-%s] %s" % (self.album.id, self.id, self.title)
@models.permalink
def get_absolute_url(self):
return ('photo_detail',[self.album.id,self.id])
"""
def rotate(self, angle=90):
filename = settings.MEDIA_ROOT + self.image.name
image = Image.open(filename)
image = image.rotate(angle)
image.save(filename, quality=95)
"""