Does anyone read this? I'll be at the coffee shop accross the street.

master
daz 2014-01-07 01:05:20 +01:00
parent d2c5048b3f
commit 1aec098d6d
84 changed files with 60 additions and 19 deletions

View File

@ -164,3 +164,6 @@ try:
from papiez_ipsum.settings_local import *
except ImportError:
pass
PAPIEZATOR_REDIS_TTL = 60*60*12

View File

@ -0,0 +1,6 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from django.conf import settings
REDIS_TTL = getattr(settings, "PAPIEZATOR_REDIS_TTL", 60*60*12)

View File

@ -1,7 +1,14 @@
from papiezator.models import Pope, PopeImage, PerfectPope, DECIMAL_PLACES
from papiezator.models import PopeImage, PerfectPope, DECIMAL_PLACES
from PIL import Image
from io import BytesIO # FIXME:
from io import BytesIO
from django.core.exceptions import ObjectDoesNotExist
from papiezator.papiezator_settings import REDIS_TTL
try:
from redis import Redis
from redis.exceptions import ConnectionError as RedisConnectionError
redis = Redis()
except ImportError:
redis = None
def select_best_pope(aspect_ratio, pope):
""" (float, pope) -> PopeImage
@ -10,54 +17,76 @@ def select_best_pope(aspect_ratio, pope):
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:
r_lte = abs(aspect_ratio - lte[0].aspect_ratio) # FIXME:
r_gte = abs(aspect_ratio - gte[0].aspect_ratio)
lte, gte = lte[0], gte[0]
if r_lte >= r_gte:
return_pope = habemus_papam(aspect_ratio, gte[0], pope)
lte_distance = abs(aspect_ratio - lte.aspect_ratio)
gte_distance = abs(aspect_ratio - gte.aspect_ratio)
if lte_distance >= gte_distance:
return_image_path = gte
else:
return_pope = habemus_papam(aspect_ratio, lte[0], pope)
return_image_path = lte
# if there are no popes above or below this ratio
elif gte:
return_pope = gte[0]
return_image_path = gte[0]
elif lte:
return_pope = lte[0]
return_image_path = lte[0]
# or even if there are no popes at all :c
else:
return None
return return_pope
return habemus_papam(aspect_ratio, image_path=return_image_path, pope=pope)
def habemus_papam(aspect_ratio, image, pope):
pp = PerfectPope(aspect_ratio=aspect_ratio, image=image, pope=pope)
pp.save() # FIXME:
return image
def habemus_papam(aspect_ratio, image_path, pope):
pp = PerfectPope(aspect_ratio=aspect_ratio, image=image_path, pope=pope)
pp.save()
return image_path
def unpopable(width, height):
if width == 0 or height == 0:
return True
if (width+height) > 9000:
if (width+height) >= 9001:
return True
return False
def read_pope(width, height, pope):
""" (int, int, Pope) -> bytes
"""
im = Image.open(pope.path)
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()
def read_pope(width, height, pope_image):
""" (int, int, Pope) -> bytes
get pope and scale for display
"""
if redis:
try:
im = redis.get((width,height))
if not im:
im = read_pope_from_fs(width, height, pope_image)
redis.set((width,height), im)
redis.expire((width,height), REDIS_TTL)
except RedisConnectionError:
im = read_pope_from_fs(width, height, pope_image)
return im
def parse_pope(path):

View File

Before

Width:  |  Height:  |  Size: 58 B

After

Width:  |  Height:  |  Size: 58 B

View File

Before

Width:  |  Height:  |  Size: 75 B

After

Width:  |  Height:  |  Size: 75 B

View File

Before

Width:  |  Height:  |  Size: 199 B

After

Width:  |  Height:  |  Size: 199 B

View File

Before

Width:  |  Height:  |  Size: 212 B

After

Width:  |  Height:  |  Size: 212 B

View File

Before

Width:  |  Height:  |  Size: 843 B

After

Width:  |  Height:  |  Size: 843 B

View File

Before

Width:  |  Height:  |  Size: 844 B

After

Width:  |  Height:  |  Size: 844 B

View File

Before

Width:  |  Height:  |  Size: 45 B

After

Width:  |  Height:  |  Size: 45 B

View File

Before

Width:  |  Height:  |  Size: 711 B

After

Width:  |  Height:  |  Size: 711 B

View File

Before

Width:  |  Height:  |  Size: 506 B

After

Width:  |  Height:  |  Size: 506 B

View File

Before

Width:  |  Height:  |  Size: 176 B

After

Width:  |  Height:  |  Size: 176 B

View File

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 130 B

View File

Before

Width:  |  Height:  |  Size: 299 B

After

Width:  |  Height:  |  Size: 299 B

View File

Before

Width:  |  Height:  |  Size: 119 B

After

Width:  |  Height:  |  Size: 119 B

View File

Before

Width:  |  Height:  |  Size: 145 B

After

Width:  |  Height:  |  Size: 145 B

View File

Before

Width:  |  Height:  |  Size: 192 B

After

Width:  |  Height:  |  Size: 192 B

View File

Before

Width:  |  Height:  |  Size: 119 B

After

Width:  |  Height:  |  Size: 119 B

View File

Before

Width:  |  Height:  |  Size: 390 B

After

Width:  |  Height:  |  Size: 390 B

View File

Before

Width:  |  Height:  |  Size: 181 B

After

Width:  |  Height:  |  Size: 181 B

View File

Before

Width:  |  Height:  |  Size: 319 B

After

Width:  |  Height:  |  Size: 319 B

View File

Before

Width:  |  Height:  |  Size: 667 B

After

Width:  |  Height:  |  Size: 667 B

View File

Before

Width:  |  Height:  |  Size: 341 B

After

Width:  |  Height:  |  Size: 341 B

View File

Before

Width:  |  Height:  |  Size: 477 B

After

Width:  |  Height:  |  Size: 477 B

View File

Before

Width:  |  Height:  |  Size: 781 B

After

Width:  |  Height:  |  Size: 781 B

View File

Before

Width:  |  Height:  |  Size: 447 B

After

Width:  |  Height:  |  Size: 447 B

View File

Before

Width:  |  Height:  |  Size: 623 B

After

Width:  |  Height:  |  Size: 623 B

View File

Before

Width:  |  Height:  |  Size: 102 B

After

Width:  |  Height:  |  Size: 102 B

View File

Before

Width:  |  Height:  |  Size: 116 B

After

Width:  |  Height:  |  Size: 116 B

View File

Before

Width:  |  Height:  |  Size: 186 B

After

Width:  |  Height:  |  Size: 186 B

View File

Before

Width:  |  Height:  |  Size: 265 B

After

Width:  |  Height:  |  Size: 265 B

View File

Before

Width:  |  Height:  |  Size: 273 B

After

Width:  |  Height:  |  Size: 273 B

View File

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

Before

Width:  |  Height:  |  Size: 552 B

After

Width:  |  Height:  |  Size: 552 B

View File

Before

Width:  |  Height:  |  Size: 369 B

After

Width:  |  Height:  |  Size: 369 B

View File

Before

Width:  |  Height:  |  Size: 197 B

After

Width:  |  Height:  |  Size: 197 B

View File

Before

Width:  |  Height:  |  Size: 203 B

After

Width:  |  Height:  |  Size: 203 B

View File

Before

Width:  |  Height:  |  Size: 198 B

After

Width:  |  Height:  |  Size: 198 B

View File

Before

Width:  |  Height:  |  Size: 200 B

After

Width:  |  Height:  |  Size: 200 B

View File

Before

Width:  |  Height:  |  Size: 932 B

After

Width:  |  Height:  |  Size: 932 B

View File

Before

Width:  |  Height:  |  Size: 336 B

After

Width:  |  Height:  |  Size: 336 B

View File

Before

Width:  |  Height:  |  Size: 351 B

After

Width:  |  Height:  |  Size: 351 B

View File

Before

Width:  |  Height:  |  Size: 354 B

After

Width:  |  Height:  |  Size: 354 B

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -1,5 +1,6 @@
<!DOCTYPE html>
<html>
{% load staticfiles %}
<head>
<meta charset="utf-8">
@ -26,6 +27,8 @@
</style>
<body>
<a href="https://github.com/you"><img style="position: absolute; top: 0; left: 0; border: 0;" src="{% static 'img/forkme.png' %}" alt="Fork me on GitHub"></a>
<div class="container">
<div class="header">