From 7c5965dcd8a2e37b6519de01b74d450a95654b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dzikie=20dro=C5=BCd=C5=BCe?= Date: Wed, 8 Jan 2014 05:50:03 +0100 Subject: [PATCH] Become a programmer, they said. It'll be fun, they said. --- src/papiezator/admin.py | 2 - src/papiezator/exceptions.py | 5 - src/papiezator/models.py | 3 + src/papiezator/pope_utils.py | 110 +++++++++--------- .../templates/papiezator/index.html | 38 +++--- src/papiezator/tests.py | 1 + src/papiezator/views.py | 9 +- 7 files changed, 82 insertions(+), 86 deletions(-) delete mode 100644 src/papiezator/exceptions.py diff --git a/src/papiezator/admin.py b/src/papiezator/admin.py index 9f1fd42..7e9b0b0 100644 --- a/src/papiezator/admin.py +++ b/src/papiezator/admin.py @@ -1,8 +1,6 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- from django.contrib import admin - - from papiezator.models import Pope, PopeImage, PerfectPope admin.site.register(PopeImage) diff --git a/src/papiezator/exceptions.py b/src/papiezator/exceptions.py deleted file mode 100644 index 050230d..0000000 --- a/src/papiezator/exceptions.py +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python -# -*- encoding: utf-8 -*- - -class ENOPopeException(Exception): pass - diff --git a/src/papiezator/models.py b/src/papiezator/models.py index 4db6c6f..384438b 100644 --- a/src/papiezator/models.py +++ b/src/papiezator/models.py @@ -2,6 +2,7 @@ from django.db import models DECIMAL_PLACES = 3 + class PopeImage(models.Model): path = models.CharField(max_length=200, primary_key=True) width = models.IntegerField() @@ -12,6 +13,7 @@ class PopeImage(models.Model): def __str__(self): return self.path.split('/')[-1] + class Pope(models.Model): id = models.IntegerField(primary_key=True) name = models.CharField(max_length=200) @@ -19,6 +21,7 @@ class Pope(models.Model): def __str__(self): return self.name + class PerfectPope(models.Model): aspect_ratio = models.FloatField(primary_key=True) image = models.ForeignKey('PopeImage') diff --git a/src/papiezator/pope_utils.py b/src/papiezator/pope_utils.py index afc7215..d1b8ab5 100644 --- a/src/papiezator/pope_utils.py +++ b/src/papiezator/pope_utils.py @@ -11,40 +11,44 @@ try: except ImportError: redis = None + +def habemus_papam(func): + """Your friendly, pointless decorator ;3 + + """ + + def inner(aspect_ratio, pope): + pp = PerfectPope.objects.filter(aspect_ratio=aspect_ratio, pope=pope) + if pp: return pp[0].image + + PopeImage = func(aspect_ratio, pope) + pp = PerfectPope(aspect_ratio=aspect_ratio, image=PopeImage, pope=pope) + pp.save() + return PopeImage + + return inner + + +@habemus_papam def select_best_pope(aspect_ratio, pope): """ (float, Pope) -> PopeImage """ - pp = PerfectPope.objects.filter(aspect_ratio=aspect_ratio, pope=pope) - if pp: - return pp[0].image # 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] if gte and lte: - - p = min([lte[0], gte[0]], - key= lambda x: abs(aspect_ratio - x.aspect_ratio) + p = min( + [lte[0], gte[0]], + key=lambda x: abs(aspect_ratio - x.aspect_ratio) ) - return_PopeImage = p + return p - # if there are no popes above or below this ratio - elif gte: - return_PopeImage = gte[0] - elif lte: - return_PopeImage = lte[0] - # or even if there are no popes at all :c - else: - return None - - return habemus_papam(aspect_ratio, PopeImage=return_PopeImage, pope=pope) - - -def habemus_papam(aspect_ratio, PopeImage, pope): - pp = PerfectPope(aspect_ratio=aspect_ratio, image=PopeImage, pope=pope) - pp.save() - return PopeImage + # 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 def unpopable(width, height): @@ -55,48 +59,47 @@ def unpopable(width, height): return False +def redis_me(func,): + def inner(*args, **kwargs): + if not redis or SAGE_REDIS: + return func(*args, **kwargs) + + try: + image = redis.get(args) + if not image: + image = func(*args, **kwargs) + redis.set(args, image) # FIXME: perhaps some better key value? + redis.expire(args, REDIS_TTL) + return image + except RedisConnectionError: + return func(*args, **kwargs) + + return inner -def read_pope_from_fs(width, height, pope_image): - im = Image.open(pope_image.path) - im = im.resize((width, height)) - f = BytesIO() # FIXME: ceriously. - im.save(f, "jpeg") - f.seek(0) - return f.read() - +@redis_me def read_pope(width, height, pope_image): """ (int, int, PopeImage) -> bytes get pope for display """ - if redis and not SAGE_REDIS: - try: - - key = (width,height, pope_image) - im = redis.get(key) - if not im: - im = read_pope_from_fs(width, height, pope_image) - redis.set(key, im) - - redis.expire(key, REDIS_TTL) - - except RedisConnectionError: - im = read_pope_from_fs(width, height, pope_image) - else: #TODO: join those two - im = read_pope_from_fs(width, height, pope_image) - - - - return im - - + im = Image.open(pope_image.path) + im = im.resize((width, height)) + f = BytesIO() # FIXME: ceriously. + im.save(f, "jpeg") + f.seek(0) + return f.read() def parse_pope(path): im = Image.open(path) width, height = im.size aspect_ratio = round(width/height, DECIMAL_PLACES) - return PopeImage(path=path, width=width, height=height, aspect_ratio=aspect_ratio) + return PopeImage( + path=path, + width=width, + height=height, + aspect_ratio=aspect_ratio + ) def pope_or_death(p, **kwargs): @@ -105,4 +108,3 @@ def pope_or_death(p, **kwargs): except ObjectDoesNotExist: x = None return x - diff --git a/src/papiezator/templates/papiezator/index.html b/src/papiezator/templates/papiezator/index.html index fc88bc0..bae6c2b 100644 --- a/src/papiezator/templates/papiezator/index.html +++ b/src/papiezator/templates/papiezator/index.html @@ -5,26 +5,24 @@ - + + + + + + + + - - - - - - - - - Fork me on $%#@^%@^% @@ -41,7 +39,7 @@
- beta feature: BXVI! + beta feature: BXVI!

How do i popes?

diff --git a/src/papiezator/tests.py b/src/papiezator/tests.py index 501deb7..477c2c5 100644 --- a/src/papiezator/tests.py +++ b/src/papiezator/tests.py @@ -7,6 +7,7 @@ Replace this with more appropriate tests for your application. from django.test import TestCase +# TODO: kek. class SimpleTest(TestCase): def test_basic_addition(self): diff --git a/src/papiezator/views.py b/src/papiezator/views.py index dad7e60..230e1fa 100644 --- a/src/papiezator/views.py +++ b/src/papiezator/views.py @@ -15,20 +15,19 @@ def index(request): } return render(request, "papiezator/index.html", c) + def conclave(request, width, height, pope_id=0): width, height = int(width), int(height) if not unpopable(width, height): - pope = pope_or_death(Pope, id=pope_id) if pope: - aspect_ratio = round(width/height, DECIMAL_PLACES) + aspect_ratio = round(width/height, DECIMAL_PLACES) pope_image = select_best_pope(aspect_ratio, pope) if pope_image: return HttpResponse( - read_pope(width, height, pope_image), - mimetype="image/jpeg" + read_pope(width, height, pope_image), + content_type="image/jpeg" ) return HttpResponse(":c") -