From 0dd51262f2bfe2c44998aa567b8aace3887bce6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dzikie=20dro=C5=BCd=C5=BCe?= Date: Tue, 26 Nov 2013 16:11:44 +0100 Subject: [PATCH] Whee, good night. --- papiezator/pope_utils.py | 16 +++++++++------- papiezator/views.py | 9 ++++++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/papiezator/pope_utils.py b/papiezator/pope_utils.py index 4744842..bf47ea2 100644 --- a/papiezator/pope_utils.py +++ b/papiezator/pope_utils.py @@ -27,20 +27,22 @@ def select_best_pope(aspect_ratio): r_lte = abs(aspect_ratio - lte[0].aspect_ratio) r_gte = abs(aspect_ratio - gte[0].aspect_ratio) if r_lte >= r_gte: - pp = PerfectPope(aspect_ratio=aspect_ratio, pope=gte[0]).save() - return gte[0].path + return _select_pope(aspect_ratio, gte[0]) else: - pp = PerfectPope(aspect_ratio=aspect_ratio, pope=lte[0]).save() - return lte[0].path + return _select_pope(aspect_ratio, lte[0]) + + +def _select_pope(aspect_ratio, pope): + pp = PerfectPope(aspect_ratio=aspect_ratio, pope=pope) + pp.save() + return pope.path + class PopeMaster: def __init__(self): pass - def parse_popes(self): - self.parse_pope("papiez.jpg") - def parse_pope(self, pope): im = Image.open(pope) width, height = im.size diff --git a/papiezator/views.py b/papiezator/views.py index 8e59841..3dc6c83 100644 --- a/papiezator/views.py +++ b/papiezator/views.py @@ -3,7 +3,7 @@ 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, ENOPopeException def index(request): @@ -16,11 +16,14 @@ def conclave(request, width, height): width, height = int(width), int(height) if width > 0 and height > 0: aspect_ratio = width/height - pope = select_best_pope(aspect_ratio) + try: + pope = select_best_pope(aspect_ratio) + except ENOPopeException: + return("czarny dym") im = Image.open(pope) im = im.resize((width, height)) - f = BytesIO() + f = BytesIO() # FIXME: ceriously. im.save(f, "jpeg") f.seek(0) return HttpResponse(f.read(), mimetype="image/jpeg")