This solves it.

master
daz 2014-01-08 07:12:39 +01:00
parent 8466e30825
commit 8ee60a5135
1 changed files with 16 additions and 8 deletions

View File

@ -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):