Arrrrgggg
parent
b7bc11b99b
commit
0069206be0
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- 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 io import BytesIO
|
||||
from PIL import Image
|
||||
|
@ -22,12 +22,8 @@ def get_noir_pope(width, height, pope_id):
|
|||
return pope.read()
|
||||
|
||||
|
||||
@http_return_image
|
||||
def view(request, width, height, pope_id=0):
|
||||
width, height = int(width), int(height)
|
||||
pope = get_noir_pope(width, height, pope_id)
|
||||
if pope:
|
||||
return HttpResponse(
|
||||
pope,
|
||||
content_type="image/jpeg"
|
||||
)
|
||||
return HttpResponse(':c')
|
||||
return pope
|
||||
|
|
|
@ -3,6 +3,7 @@ from PIL import Image
|
|||
from io import BytesIO
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from random import choice
|
||||
from django.http import HttpResponse
|
||||
from papiezator.papiezator_settings import (
|
||||
REDIS_TTL, SAGE_REDIS, POPE_SPAN,
|
||||
POPE_CHOICE_RANGE
|
||||
|
@ -24,6 +25,7 @@ def unpopable(width, height):
|
|||
return False
|
||||
|
||||
|
||||
#======== your friendly decorators :3 ======================================
|
||||
def redis_me(prefix):
|
||||
|
||||
def decorator(func):
|
||||
|
@ -32,12 +34,13 @@ def redis_me(prefix):
|
|||
if not redis or SAGE_REDIS:
|
||||
return func(*args, **kwargs)
|
||||
|
||||
# FIXME: perhaps some better key value?
|
||||
key = (prefix, args)
|
||||
try:
|
||||
image = redis.get(key)
|
||||
if not image:
|
||||
image = func(*args, **kwargs)
|
||||
redis.set(key, image) # FIXME: perhaps some better key value?
|
||||
redis.set(key, image)
|
||||
redis.expire(key, REDIS_TTL)
|
||||
return image
|
||||
|
||||
|
@ -48,22 +51,6 @@ def redis_me(prefix):
|
|||
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):
|
||||
"""Your friendly, pointless decorator ;3
|
||||
|
||||
|
@ -81,6 +68,35 @@ def habemus_papam(func):
|
|||
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
|
||||
def select_best_pope(aspect_ratio, pope, random=True):
|
||||
""" (float, Pope) -> PopeImage
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
# Create your views here.
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
from django.core.urlresolvers import reverse
|
||||
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):
|
||||
|
@ -15,9 +14,6 @@ def index(request):
|
|||
return render(request, "papiezator/index.html", c)
|
||||
|
||||
|
||||
@http_return_image
|
||||
def conclave(request, width, height, pope_id=0):
|
||||
pope = get_pope(width, height, pope_id)
|
||||
if pope:
|
||||
return HttpResponse(pope, content_type="image/jpeg")
|
||||
|
||||
return HttpResponse(":c")
|
||||
return get_pope(width, height, pope_id)
|
||||
|
|
Loading…
Reference in New Issue