Arrrrgggg

master
daz 2014-01-09 21:49:38 +01:00
parent b7bc11b99b
commit 0069206be0
3 changed files with 39 additions and 31 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
from papiezator.pope_utils import get_pope, redis_me from papiezator.pope_utils import get_pope, redis_me, http_return_image
from django.http import HttpResponse from django.http import HttpResponse
from io import BytesIO from io import BytesIO
from PIL import Image from PIL import Image
@ -22,12 +22,8 @@ def get_noir_pope(width, height, pope_id):
return pope.read() return pope.read()
@http_return_image
def view(request, width, height, pope_id=0): def view(request, width, height, pope_id=0):
width, height = int(width), int(height) width, height = int(width), int(height)
pope = get_noir_pope(width, height, pope_id) pope = get_noir_pope(width, height, pope_id)
if pope: return pope
return HttpResponse(
pope,
content_type="image/jpeg"
)
return HttpResponse(':c')

View File

@ -3,6 +3,7 @@ from PIL import Image
from io import BytesIO from io import BytesIO
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from random import choice from random import choice
from django.http import HttpResponse
from papiezator.papiezator_settings import ( from papiezator.papiezator_settings import (
REDIS_TTL, SAGE_REDIS, POPE_SPAN, REDIS_TTL, SAGE_REDIS, POPE_SPAN,
POPE_CHOICE_RANGE POPE_CHOICE_RANGE
@ -24,6 +25,7 @@ def unpopable(width, height):
return False return False
#======== your friendly decorators :3 ======================================
def redis_me(prefix): def redis_me(prefix):
def decorator(func): def decorator(func):
@ -32,12 +34,13 @@ def redis_me(prefix):
if not redis or SAGE_REDIS: if not redis or SAGE_REDIS:
return func(*args, **kwargs) return func(*args, **kwargs)
# FIXME: perhaps some better key value?
key = (prefix, args) key = (prefix, args)
try: try:
image = redis.get(key) image = redis.get(key)
if not image: if not image:
image = func(*args, **kwargs) image = func(*args, **kwargs)
redis.set(key, image) # FIXME: perhaps some better key value? redis.set(key, image)
redis.expire(key, REDIS_TTL) redis.expire(key, REDIS_TTL)
return image return image
@ -48,22 +51,6 @@ def redis_me(prefix):
return decorator return decorator
@redis_me("get_pope")
def get_pope(width, height, pope_id):
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)
pope_image = select_best_pope(aspect_ratio, pope)
if pope_image:
return read_pope(width, height, pope_image)
return None
def habemus_papam(func): def habemus_papam(func):
"""Your friendly, pointless decorator ;3 """Your friendly, pointless decorator ;3
@ -81,6 +68,35 @@ def habemus_papam(func):
return inner return inner
def http_return_image(func):
def inner(*args, **kwargs):
image = func(*args, **kwargs)
if image:
return HttpResponse(image, content_type="image/jpeg")
else:
return HttpResponse("Jan Paweł II spadał z rowerka")
return inner
# =============================================================================
@redis_me("get_pope")
def get_pope(width, height, pope_id):
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)
pope_image = select_best_pope(aspect_ratio, pope)
if pope_image:
return read_pope(width, height, pope_image)
return None
@habemus_papam @habemus_papam
def select_best_pope(aspect_ratio, pope, random=True): def select_best_pope(aspect_ratio, pope, random=True):
""" (float, Pope) -> PopeImage """ (float, Pope) -> PopeImage

View File

@ -1,9 +1,8 @@
# Create your views here. # Create your views here.
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from papiezator.papiezator_settings import LOLGITHUB_URL from papiezator.papiezator_settings import LOLGITHUB_URL
from papiezator.pope_utils import get_pope from papiezator.pope_utils import get_pope, http_return_image
def index(request): def index(request):
@ -15,9 +14,6 @@ def index(request):
return render(request, "papiezator/index.html", c) return render(request, "papiezator/index.html", c)
@http_return_image
def conclave(request, width, height, pope_id=0): def conclave(request, width, height, pope_id=0):
pope = get_pope(width, height, pope_id) return get_pope(width, height, pope_id)
if pope:
return HttpResponse(pope, content_type="image/jpeg")
return HttpResponse(":c")