Something fixed

master
daz 2013-12-23 19:53:20 +01:00
parent 56d039fedc
commit 0b334a52e2
6 changed files with 60 additions and 53 deletions

View File

@ -3,8 +3,8 @@
from django.contrib import admin from django.contrib import admin
from papiezator.models import Pope, PopeVersion, PerfectPope from papiezator.models import Pope, PopeImage, PerfectPope
admin.site.register(PopeImage)
admin.site.register(Pope) admin.site.register(Pope)
admin.site.register(PopeVersion)
admin.site.register(PerfectPope) admin.site.register(PerfectPope)

View File

@ -1,26 +1,27 @@
from django.db import models from django.db import models
decimal_places = 3 DECIMAL_PLACES = 3
class Pope(models.Model): class PopeImage(models.Model):
path = models.CharField(max_length=200, primary_key=True) path = models.CharField(max_length=200, primary_key=True)
width = models.IntegerField() width = models.IntegerField()
height = models.IntegerField() height = models.IntegerField()
aspect_ratio = models.FloatField() aspect_ratio = models.FloatField()
pope_version = models.ForeignKey('PopeVersion') pope = models.ForeignKey('Pope')
def __str__(self): def __str__(self):
return self.path.split('/')[-1] return self.path.split('/')[-1]
class PopeVersion(models.Model): class Pope(models.Model):
pope_id = models.CharField(max_length=8, primary_key=True) id = models.IntegerField(primary_key=True)
pope_name = models.CharField(max_length=200) name = models.CharField(max_length=200)
def __str__(self): def __str__(self):
return self.pope_name return self.name
class PerfectPope(models.Model): class PerfectPope(models.Model):
aspect_ratio = models.FloatField(primary_key=True) aspect_ratio = models.FloatField(primary_key=True)
image = models.ForeignKey('PopeImage')
pope = models.ForeignKey('Pope') pope = models.ForeignKey('Pope')
def __str__(self): def __str__(self):

View File

@ -1,25 +1,26 @@
from papiezator.models import Pope, PerfectPope, decimal_places from papiezator.models import Pope, PopeImage, PerfectPope, DECIMAL_PLACES
from PIL import Image from PIL import Image
from io import BytesIO # FIXME: from io import BytesIO # FIXME:
from django.core.exceptions import ObjectDoesNotExist
def select_best_pope(aspect_ratio): def select_best_pope(aspect_ratio, pope):
""" (float) -> Pope """ (float, pope) -> PopeImage
""" """
pp = PerfectPope.objects.filter(aspect_ratio=aspect_ratio) pp = PerfectPope.objects.filter(aspect_ratio=aspect_ratio, pope=pope)
if pp: if pp:
return pp[0].pope return pp[0].image
lte = Pope.objects.filter(aspect_ratio__lte=aspect_ratio).order_by('-aspect_ratio')[0:1] lte = PopeImage.objects.filter(aspect_ratio__lte=aspect_ratio, pope=pope).order_by('-aspect_ratio')[0:1]
gte = Pope.objects.filter(aspect_ratio__gte=aspect_ratio).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: if gte and lte:
r_lte = abs(aspect_ratio - lte[0].aspect_ratio) # FIXME: r_lte = abs(aspect_ratio - lte[0].aspect_ratio) # FIXME:
r_gte = abs(aspect_ratio - gte[0].aspect_ratio) r_gte = abs(aspect_ratio - gte[0].aspect_ratio)
if r_lte >= r_gte: if r_lte >= r_gte:
return_pope = habemus_papam(aspect_ratio, gte[0]) return_pope = habemus_papam(aspect_ratio, gte[0], pope)
else: else:
return_pope = habemus_papam(aspect_ratio, lte[0]) return_pope = habemus_papam(aspect_ratio, lte[0], pope)
elif gte: elif gte:
return_pope = gte[0] return_pope = gte[0]
@ -31,10 +32,10 @@ def select_best_pope(aspect_ratio):
return return_pope return return_pope
def habemus_papam(aspect_ratio, pope): def habemus_papam(aspect_ratio, image, pope):
pp = PerfectPope(aspect_ratio=aspect_ratio, pope=pope) pp = PerfectPope(aspect_ratio=aspect_ratio, image=image, pope=pope)
pp.save() # FIXME: pp.save() # FIXME:
return pope return image
def unpopable(width, height): def unpopable(width, height):
@ -62,7 +63,14 @@ def read_pope(width, height, pope):
def parse_pope(path): def parse_pope(path):
im = Image.open(path) im = Image.open(path)
width, height = im.size width, height = im.size
aspect_ratio = round(width/height, decimal_places) aspect_ratio = round(width/height, DECIMAL_PLACES)
return Pope(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):
try:
x = p.objects.get(**kwargs)
except ObjectDoesNotExist:
x = None
return x

View File

@ -6,6 +6,6 @@ from papiezator import views
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^$', views.index, name='index'), 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+)/$', views.conclave, name='conclave'),
url(r'^(?P<width>\d+)/(?P<height>\d+)/(?P<pope>d+)$', views.grand_conclave, name='grand_conclave'), url(r'^(?P<width>\d+)/(?P<height>\d+)/(?P<pope_id>\d+)/$', views.conclave, name='grand_conclave'),
) )

View File

@ -2,8 +2,8 @@
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from papiezator.pope_utils import select_best_pope, read_pope, unpopable from papiezator.pope_utils import select_best_pope, read_pope, unpopable, pope_or_death
from papiezator.models import decimal_places from papiezator.models import DECIMAL_PLACES, Pope
def index(request): def index(request):
@ -13,27 +13,30 @@ def index(request):
} }
return render(request, "papiezator/index.html", c) return render(request, "papiezator/index.html", c)
def conclave(request, width, height): def conclave(request, width, height, pope_id=0):
width, height = int(width), int(height) width, height = int(width), int(height)
if not unpopable(width, height): if not unpopable(width, height):
aspect_ratio = round(width/height, decimal_places)
pope = select_best_pope(aspect_ratio)
pope = pope_or_death(Pope, id=pope_id)
if pope: if pope:
return HttpResponse( aspect_ratio = round(width/height, DECIMAL_PLACES)
read_pope(width, height, pope), pope_image = select_best_pope(aspect_ratio, pope)
mimetype="image/jpeg"
) if pope_image:
return HttpResponse(
read_pope(width, height, pope_image),
mimetype="image/jpeg"
)
return HttpResponse(":c") return HttpResponse(":c")
def grand_conclave(request, width, height, pope_id): #def grand_conclave(request, width, height, pope_id):
width, height = int(width), int(height) #width, height = int(width), int(height)
if not unpopable(width, height): #if not unpopable(width, height):
aspect_ratio = round(width/height, decimal_places) #aspect_ratio = round(width/height, decimal_places)
pope = pope_or_death(pope_id=pope_id) #pope = pope_or_death(pope_id=pope_id)
pope = select_best_pope(aspect_ratio) #pope = select_best_pope(aspect_ratio)

View File

@ -3,9 +3,8 @@ import os
os.environ ['PYTHONPATH'] = '/home/daz/Documents/Projects/Py/django/papiez_ipsum' os.environ ['PYTHONPATH'] = '/home/daz/Documents/Projects/Py/django/papiez_ipsum'
os.environ['DJANGO_SETTINGS_MODULE'] = 'papiez_ipsum.settings' os.environ['DJANGO_SETTINGS_MODULE'] = 'papiez_ipsum.settings'
from glob import glob from glob import glob
from papiezator.models import Pope, PopeVersion from papiezator.models import PopeImage, Pope
from papiezator.pope_utils import parse_pope from papiezator.pope_utils import parse_pope, pope_or_death
from django.core.exceptions import ObjectDoesNotExist
from os import path from os import path
from optparse import OptionParser from optparse import OptionParser
import ast import ast
@ -15,30 +14,25 @@ PREFIX = "popes"
def pope_or_death(p, **kwargs):
try:
x = p.objects.get(**kwargs)
except ObjectDoesNotExist:
x = None
return x
def init_pope(meta): def init_pope(meta):
pope = pope_or_death(PopeVersion, pope_id=meta["id"]) pope = pope_or_death(Pope, id=meta["id"])
if pope: if pope:
return pope return pope
else: else:
pope = PopeVersion(meta["id"], meta["name"]) pope = Pope(meta["id"], meta["name"])
pope.save() pope.save()
return pope return pope
def add_pope_entry(pope, path): def add_pope_entry(pope, path):
if pope_or_death(Pope, path=path): if pope_or_death(PopeImage, path=path):
print('-', end='')
return False return False
pope_image = parse_pope(path) pope_image = parse_pope(path)
pope_image.pope_version = pope pope_image.pope = pope
pope_image.save() pope_image.save()
print('+', end='')
return pope_image return pope_image
def main(): def main():
@ -50,6 +44,7 @@ def main():
for pl in pope_lists: for pl in pope_lists:
pope = pl.split('_')[0] pope = pl.split('_')[0]
print(pope)
if os.path.exists(pope+"_meta"): if os.path.exists(pope+"_meta"):
with open(pope+"_meta") as file: with open(pope+"_meta") as file: