From 8ee60a51352302cb722f4b61f576d45787b10841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dzikie=20dro=C5=BCd=C5=BCe?= Date: Wed, 8 Jan 2014 07:12:39 +0100 Subject: [PATCH] This solves it. --- src/papiezator/pope_utils.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/papiezator/pope_utils.py b/src/papiezator/pope_utils.py index f03f572..740b509 100644 --- a/src/papiezator/pope_utils.py +++ b/src/papiezator/pope_utils.py @@ -35,20 +35,28 @@ def select_best_pope(aspect_ratio, pope): """ # popes below and above this ratio - lte = PopeImage.objects.filter(aspect_ratio__lte=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] + popes = [] + popes.extend( + PopeImage.objects.filter( + aspect_ratio__lte=aspect_ratio, + pope=pope).order_by('-aspect_ratio')[0:1] + ) - if gte and lte: + popes.extend( + PopeImage.objects.filter( + aspect_ratio__gte=aspect_ratio, + pope=pope).order_by('aspect_ratio')[0:1] + ) + + if popes: p = min( - [lte[0], gte[0]], + popes, key=lambda x: abs(aspect_ratio - x.aspect_ratio) ) return p - # if there are no popes above or below this ratio # do i want to get rid of this? - elif gte: return gte[0] - elif lte: return lte[0] - else: return None + else: + return None def unpopable(width, height):