pgsql is more strict, increase the hackiness up to 11
parent
c7ce3f528e
commit
6d32fa902e
|
@ -1,42 +1,51 @@
|
|||
from papiezator.models import Pope, PerfectPope, decimal_places
|
||||
from papiezator.exceptions import ENOPopeException
|
||||
from PIL import Image
|
||||
from decimal import Decimal
|
||||
PREFIX = "popes"
|
||||
|
||||
|
||||
|
||||
from io import BytesIO # FIXME:
|
||||
|
||||
def select_best_pope(aspect_ratio):
|
||||
pp = PerfectPope.objects.filter(aspect_ratio=aspect_ratio)
|
||||
if pp:
|
||||
return pp[0].pope
|
||||
lte = Pope.objects.filter(aspect_ratio__lte=aspect_ratio).order_by('-aspect_ratio')
|
||||
gte = Pope.objects.filter(aspect_ratio__gte=aspect_ratio).order_by('aspect_ratio')
|
||||
lte = Pope.objects.filter(aspect_ratio__lte=aspect_ratio).order_by('-aspect_ratio')[0:1]
|
||||
gte = Pope.objects.filter(aspect_ratio__gte=aspect_ratio).order_by('aspect_ratio')[0:1]
|
||||
|
||||
if not gte and not lte:
|
||||
raise ENOPopeException
|
||||
elif not gte:
|
||||
return_pope = lte[0]
|
||||
elif not lte:
|
||||
return_pope = gte[0]
|
||||
else:
|
||||
if gte and lte:
|
||||
r_lte = abs(aspect_ratio - lte[0].aspect_ratio) # FIXME:
|
||||
r_gte = abs(aspect_ratio - gte[0].aspect_ratio)
|
||||
|
||||
if r_lte >= r_gte:
|
||||
return_pope = _select_pope(aspect_ratio, gte[0])
|
||||
return_pope = habemus_papam(aspect_ratio, gte[0])
|
||||
else:
|
||||
return_pope = _select_pope(aspect_ratio, lte[0])
|
||||
return_pope = habemus_papam(aspect_ratio, lte[0])
|
||||
|
||||
elif gte:
|
||||
return_pope = gte[0]
|
||||
elif lte:
|
||||
return_pope = lte[0]
|
||||
|
||||
else:
|
||||
raise ENOPopeException
|
||||
|
||||
return return_pope
|
||||
|
||||
|
||||
def _select_pope(aspect_ratio, pope):
|
||||
def habemus_papam(aspect_ratio, pope):
|
||||
pp = PerfectPope(aspect_ratio=aspect_ratio, pope=pope)
|
||||
pp.save() # FIXME:
|
||||
return pope
|
||||
|
||||
|
||||
|
||||
def read_pope(width, height, pope):
|
||||
im = Image.open(pope.path)
|
||||
im = im.resize((width, height))
|
||||
f = BytesIO() # FIXME: ceriously.
|
||||
im.save(f, "jpeg")
|
||||
f.seek(0)
|
||||
return f.read()
|
||||
|
||||
|
||||
class PopeMaster:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
|
|
@ -1,31 +1,26 @@
|
|||
# Create your views here.
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
from PIL import Image
|
||||
from io import BytesIO # FIXME:
|
||||
from papiezator.pope_utils import select_best_pope
|
||||
from papiezator.pope_utils import select_best_pope, read_pope
|
||||
from papiezator.exceptions import ENOPopeException
|
||||
from papiezator.models import decimal_places
|
||||
|
||||
|
||||
def index(request):
|
||||
|
||||
return render(request, "papiezator/index.html")
|
||||
|
||||
def conclave(request, width, height):
|
||||
width, height = int(width), int(height)
|
||||
if width > 0 and height > 0:
|
||||
aspect_ratio = round(width/height, decimal_places)
|
||||
try:
|
||||
pope = select_best_pope(aspect_ratio)
|
||||
except ENOPopeException:
|
||||
return HttpResponse("czarny dym")
|
||||
|
||||
im = Image.open(pope.path)
|
||||
im = im.resize((width, height))
|
||||
f = BytesIO() # FIXME: ceriously.
|
||||
im.save(f, "jpeg")
|
||||
f.seek(0)
|
||||
return HttpResponse(f.read(), mimetype="image/jpeg")
|
||||
else:
|
||||
if width == 0 or height == 0:
|
||||
return HttpResponse("8====D~~~~")
|
||||
|
||||
aspect_ratio = round(width/height, decimal_places)
|
||||
try:
|
||||
pope = select_best_pope(aspect_ratio)
|
||||
except ENOPopeException:
|
||||
return HttpResponse("czarny dym")
|
||||
|
||||
return HttpResponse(
|
||||
read_pope(width, height, pope),
|
||||
mimetype="image/jpeg"
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue