By works, I meant 'doesnt work'. Works now..

master
daz 2014-01-08 04:53:36 +01:00
parent 8b7cc5fad1
commit 53313383a5
1 changed files with 17 additions and 18 deletions

View File

@ -3,6 +3,7 @@ from PIL import Image
from io import BytesIO from io import BytesIO
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from papiezator.papiezator_settings import REDIS_TTL, SAGE_REDIS from papiezator.papiezator_settings import REDIS_TTL, SAGE_REDIS
try: try:
from redis import Redis from redis import Redis
from redis.exceptions import ConnectionError as RedisConnectionError from redis.exceptions import ConnectionError as RedisConnectionError
@ -11,7 +12,7 @@ except ImportError:
redis = None redis = None
def select_best_pope(aspect_ratio, pope): def select_best_pope(aspect_ratio, pope):
""" (float, pope) -> PopeImage """ (float, Pope) -> PopeImage
""" """
pp = PerfectPope.objects.filter(aspect_ratio=aspect_ratio, pope=pope) pp = PerfectPope.objects.filter(aspect_ratio=aspect_ratio, pope=pope)
@ -22,32 +23,29 @@ def select_best_pope(aspect_ratio, pope):
gte = PopeImage.objects.filter(aspect_ratio__gte=aspect_ratio, pope=pope).order_by('aspect_ratio')[0:1] gte = PopeImage.objects.filter(aspect_ratio__gte=aspect_ratio, pope=pope).order_by('aspect_ratio')[0:1]
if gte and lte: if gte and lte:
lte, gte = lte[0], gte[0]
lte_distance = abs(aspect_ratio - lte.aspect_ratio) p = min([lte[0], gte[0]],
gte_distance = abs(aspect_ratio - gte.aspect_ratio) key= lambda x: abs(aspect_ratio - x.aspect_ratio)
)
if lte_distance >= gte_distance: return_PopeImage = p
return_image_path = gte
else:
return_image_path = lte
# if there are no popes above or below this ratio # if there are no popes above or below this ratio
elif gte: elif gte:
return_image_path = gte[0] return_PopeImage = gte[0]
elif lte: elif lte:
return_image_path = lte[0] return_PopeImage = lte[0]
# or even if there are no popes at all :c # or even if there are no popes at all :c
else: else:
return None return None
return habemus_papam(aspect_ratio, image_path=return_image_path, pope=pope) return habemus_papam(aspect_ratio, PopeImage=return_PopeImage, pope=pope)
def habemus_papam(aspect_ratio, image_path, pope): def habemus_papam(aspect_ratio, PopeImage, pope):
pp = PerfectPope(aspect_ratio=aspect_ratio, image=image_path, pope=pope) import ipdb; ipdb.set_trace()
pp = PerfectPope(aspect_ratio=aspect_ratio, image=PopeImage, pope=pope)
pp.save() pp.save()
return image_path return PopeImage
def unpopable(width, height): def unpopable(width, height):
@ -69,8 +67,8 @@ def read_pope_from_fs(width, height, pope_image):
return f.read() return f.read()
def read_pope(width, height, pope_image): def read_pope(width, height, pope_image):
""" (int, int, Pope) -> bytes """ (int, int, PopeImage) -> bytes
get pope and scale for display get pope for display
""" """
if redis and not SAGE_REDIS: if redis and not SAGE_REDIS:
try: try:
@ -80,7 +78,8 @@ def read_pope(width, height, pope_image):
if not im: if not im:
im = read_pope_from_fs(width, height, pope_image) im = read_pope_from_fs(width, height, pope_image)
redis.set(key, im) redis.set(key, im)
redis.expire(key, REDIS_TTL)
redis.expire(key, REDIS_TTL)
except RedisConnectionError: except RedisConnectionError:
im = read_pope_from_fs(width, height, pope_image) im = read_pope_from_fs(width, height, pope_image)