By works, I meant 'doesnt work'. Works now..
parent
8b7cc5fad1
commit
53313383a5
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue