Fucking egotistical bastard. adds expandtab to vimrc
parent
6793326ec9
commit
f200eb58ea
|
@ -6,3 +6,5 @@ from django.conf import settings
|
|||
SAGE_REDIS = False
|
||||
REDIS_TTL = getattr(settings, "PAPIEZATOR_REDIS_TTL", 60*60*12)
|
||||
LOLGITHUB_URL = "https://code.hackerspace.pl/daz/papiez_ipsum/"
|
||||
POPE_SPAN = 5
|
||||
POPE_CHOICE_RANGE = 4
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
from papiezator.models import PopeImage, PerfectPope, DECIMAL_PLACES
|
||||
from papiezator.models import Pope, PopeImage, PerfectPope, DECIMAL_PLACES
|
||||
from PIL import Image
|
||||
from io import BytesIO
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from papiezator.papiezator_settings import REDIS_TTL, SAGE_REDIS
|
||||
from random import choice
|
||||
from papiezator.papiezator_settings import (
|
||||
REDIS_TTL, SAGE_REDIS, POPE_SPAN,
|
||||
POPE_CHOICE_RANGE
|
||||
)
|
||||
|
||||
try:
|
||||
from redis import Redis
|
||||
|
@ -12,6 +16,56 @@ except ImportError:
|
|||
redis = None
|
||||
|
||||
|
||||
def unpopable(width, height):
|
||||
if width == 0 or height == 0:
|
||||
return True
|
||||
if (width+height) >= 9001:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def redis_me(prefix):
|
||||
|
||||
def decorator(func):
|
||||
|
||||
def inner(*args, **kwargs):
|
||||
if not redis or SAGE_REDIS:
|
||||
return func(*args, **kwargs)
|
||||
|
||||
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.expire(key, REDIS_TTL)
|
||||
return image
|
||||
|
||||
except RedisConnectionError:
|
||||
return func(*args, **kwargs)
|
||||
|
||||
return inner
|
||||
return decorator
|
||||
|
||||
|
||||
@redis_me("get_pope")
|
||||
def get_pope(width, height, pope_id):
|
||||
width, height = int(width), int(height)
|
||||
if not unpopable(width, height):
|
||||
|
||||
width, height = int(width), int(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
|
||||
|
||||
|
@ -30,62 +84,37 @@ def habemus_papam(func):
|
|||
|
||||
|
||||
@habemus_papam
|
||||
def select_best_pope(aspect_ratio, pope):
|
||||
def select_best_pope(aspect_ratio, pope, random=True):
|
||||
""" (float, Pope) -> PopeImage
|
||||
|
||||
"""
|
||||
# popes below and above this ratio
|
||||
popes = []
|
||||
|
||||
popes.extend(
|
||||
PopeImage.objects.filter(
|
||||
aspect_ratio__lte=aspect_ratio,
|
||||
pope=pope).order_by('-aspect_ratio')[0:1]
|
||||
)
|
||||
pope=pope).order_by('-aspect_ratio')[0:POPE_SPAN]
|
||||
)
|
||||
|
||||
popes.extend(
|
||||
PopeImage.objects.filter(
|
||||
aspect_ratio__gte=aspect_ratio,
|
||||
pope=pope).order_by('aspect_ratio')[0:1]
|
||||
pope=pope).order_by('aspect_ratio')[0:POPE_SPAN]
|
||||
)
|
||||
|
||||
if popes:
|
||||
p = min(
|
||||
p = sorted(
|
||||
popes,
|
||||
key=lambda x: abs(aspect_ratio - x.aspect_ratio)
|
||||
)
|
||||
return p
|
||||
|
||||
else:
|
||||
return None
|
||||
if random: return choice(p[:POPE_CHOICE_RANGE])
|
||||
else: return p[0]
|
||||
|
||||
else: return None
|
||||
|
||||
|
||||
def unpopable(width, height):
|
||||
if width == 0 or height == 0:
|
||||
return True
|
||||
if (width+height) >= 9001:
|
||||
return True
|
||||
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
|
||||
|
||||
|
||||
@redis_me
|
||||
def read_pope(width, height, pope_image):
|
||||
""" (int, int, PopeImage) -> bytes
|
||||
get pope for display
|
||||
|
|
|
@ -41,6 +41,10 @@
|
|||
<div class="alert alert-info">
|
||||
beta feature: <a href="{% url 'papiezator:grand_conclave' 255 200 1 %}">BXVI!</a>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-info">
|
||||
beta feature: <a href="{% url 'papiezator:noir' 255 200 %}">noirpope</a>
|
||||
</div>
|
||||
<h3>How do i popes?</h3>
|
||||
|
||||
<p> simply append width/height to the url</p>
|
||||
|
|
|
@ -2,11 +2,17 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
from django.conf.urls import patterns, url
|
||||
from papiezator import views
|
||||
from papiezator.extenpopes import noir
|
||||
|
||||
urlpatterns = patterns('',
|
||||
whp = r'(?P<width>\d+)/(?P<height>\d+)/(?P<pope_id>\d+)/$'
|
||||
wh = r'(?P<width>\d+)/(?P<height>\d+)/$'
|
||||
urlpatterns = patterns(
|
||||
'',
|
||||
url(r'^$', views.index, name='index'),
|
||||
url(r'^(?P<width>\d+)/(?P<height>\d+)/$', views.conclave, name='conclave'),
|
||||
url(r'^(?P<width>\d+)/(?P<height>\d+)/(?P<pope_id>\d+)/$', views.conclave, name='grand_conclave'),
|
||||
#url(r'^(?P<width>\d+)/(?P<height>\d+)/r', views.conclave, name='random_conclave'),
|
||||
url(r'^' + wh, views.conclave, name='conclave'),
|
||||
url(r'^' + whp, views.conclave, name='grand_conclave'),
|
||||
url(r'^noirpope/' + wh, noir.view, name='noir'),
|
||||
url(r'^noirpope/' + whp, noir.view, name='noir'),
|
||||
|
||||
)
|
||||
|
||||
|
|
|
@ -3,13 +3,12 @@ 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 select_best_pope, read_pope, unpopable, pope_or_death
|
||||
from papiezator.models import DECIMAL_PLACES, Pope
|
||||
from papiezator.pope_utils import get_pope
|
||||
|
||||
|
||||
def index(request):
|
||||
c = {
|
||||
"absolute_url": request.build_absolute_uri(reverse("papiezator:conclave", args=[200,300])),
|
||||
"absolute_url": request.build_absolute_uri(reverse("papiezator:conclave", args=[200, 300])),
|
||||
"absolute_url_exp": request.build_absolute_uri("width/height/"),
|
||||
"github_url": LOLGITHUB_URL
|
||||
}
|
||||
|
@ -17,17 +16,8 @@ def index(request):
|
|||
|
||||
|
||||
def conclave(request, width, height, pope_id=0):
|
||||
width, height = int(width), int(height)
|
||||
if not unpopable(width, height):
|
||||
pope = get_pope(width, height, pope_id)
|
||||
if pope:
|
||||
return HttpResponse(pope, content_type="image/jpeg")
|
||||
|
||||
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 HttpResponse(
|
||||
read_pope(width, height, pope_image),
|
||||
content_type="image/jpeg"
|
||||
)
|
||||
return HttpResponse(":c")
|
||||
|
|
Loading…
Reference in New Issue