From 53313383a536fd18c50fdd4f0bb27ddd5d063637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dzikie=20dro=C5=BCd=C5=BCe?= Date: Wed, 8 Jan 2014 04:53:36 +0100 Subject: [PATCH] By works, I meant 'doesnt work'. Works now.. --- src/papiezator/pope_utils.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/papiezator/pope_utils.py b/src/papiezator/pope_utils.py index 2b4166d..b09b45d 100644 --- a/src/papiezator/pope_utils.py +++ b/src/papiezator/pope_utils.py @@ -3,6 +3,7 @@ from PIL import Image from io import BytesIO from django.core.exceptions import ObjectDoesNotExist from papiezator.papiezator_settings import REDIS_TTL, SAGE_REDIS + try: from redis import Redis from redis.exceptions import ConnectionError as RedisConnectionError @@ -11,7 +12,7 @@ except ImportError: redis = None def select_best_pope(aspect_ratio, pope): - """ (float, pope) -> PopeImage + """ (float, Pope) -> PopeImage """ 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] if gte and lte: - lte, gte = lte[0], gte[0] - lte_distance = abs(aspect_ratio - lte.aspect_ratio) - gte_distance = abs(aspect_ratio - gte.aspect_ratio) - - if lte_distance >= gte_distance: - return_image_path = gte - else: - return_image_path = lte + p = min([lte[0], gte[0]], + key= lambda x: abs(aspect_ratio - x.aspect_ratio) + ) + return_PopeImage = p # if there are no popes above or below this ratio elif gte: - return_image_path = gte[0] + return_PopeImage = gte[0] elif lte: - return_image_path = lte[0] + return_PopeImage = lte[0] # or even if there are no popes at all :c else: 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): - pp = PerfectPope(aspect_ratio=aspect_ratio, image=image_path, pope=pope) +def habemus_papam(aspect_ratio, PopeImage, pope): + import ipdb; ipdb.set_trace() + pp = PerfectPope(aspect_ratio=aspect_ratio, image=PopeImage, pope=pope) pp.save() - return image_path + return PopeImage def unpopable(width, height): @@ -69,8 +67,8 @@ def read_pope_from_fs(width, height, pope_image): return f.read() def read_pope(width, height, pope_image): - """ (int, int, Pope) -> bytes - get pope and scale for display + """ (int, int, PopeImage) -> bytes + get pope for display """ if redis and not SAGE_REDIS: try: @@ -80,7 +78,8 @@ def read_pope(width, height, pope_image): if not im: im = read_pope_from_fs(width, height, pope_image) redis.set(key, im) - redis.expire(key, REDIS_TTL) + + redis.expire(key, REDIS_TTL) except RedisConnectionError: im = read_pope_from_fs(width, height, pope_image)