-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
65 lines (52 loc) · 1.69 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import urllib2
from urllib2 import HTTPError
from os.path import splitext
from google.appengine.ext import db
from google.appengine.api.images import Image, PNG, JPEG
from settings import EXPIRATION_DB
MAX_WIDTH = 1280
MAX_HEIGHT = 800
class ImageNotFoundError(RuntimeError):
pass
class Picture(db.Model):
url = db.StringProperty()
picture = db.BlobProperty(default=None)
last_fetch = db.DateTimeProperty(auto_now=True)
@property
def extension(self):
return splitext(self.url)[-1]
@property
def image_format(self):
return self.extension in ('.jpg', '.jpeg') and JPEG or PNG
def is_expired(self):
expiration_time = self.last_fetch + datetime.timedelta(seconds=EXPIRATION_DB)
return datetime.datetime.now() >= expiration_time
def fetch_image(self):
try:
response = urllib2.urlopen(self.url)
except HTTPError:
raise ImageNotFoundError()
contents = response.read()
self.store_picture(contents)
self.last_fetch = datetime.datetime.now()
return Image(contents)
def store_picture(self, picture):
self.picture = db.Blob(picture)
def rebalance_picture_size(self, resize_method):
img = Image(self.picture)
resize_width = img.width > img.height and MAX_WIDTH or 0
resize_height = img.height >= img.width and MAX_HEIGHT or 0
new_picture = resize_method(
img,
resize_width,
'',
resize_height,
'',
self.image_format,
'center',
'middle'
)
self.store_picture(new_picture)