From 0b334a52e23dadd5f88edc7e62d1f8f6c3e6c435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dzikie=20dro=C5=BCd=C5=BCe?= Date: Mon, 23 Dec 2013 19:53:20 +0100 Subject: [PATCH] Something fixed --- src/papiezator/admin.py | 4 ++-- src/papiezator/models.py | 15 ++++++++------- src/papiezator/pope_utils.py | 36 ++++++++++++++++++++++-------------- src/papiezator/urls.py | 2 +- src/papiezator/views.py | 33 ++++++++++++++++++--------------- src/populate_popes | 23 +++++++++-------------- 6 files changed, 60 insertions(+), 53 deletions(-) diff --git a/src/papiezator/admin.py b/src/papiezator/admin.py index 1fcdcc7..9f1fd42 100644 --- a/src/papiezator/admin.py +++ b/src/papiezator/admin.py @@ -3,8 +3,8 @@ 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(PopeVersion) admin.site.register(PerfectPope) diff --git a/src/papiezator/models.py b/src/papiezator/models.py index 1ffcfe2..4db6c6f 100644 --- a/src/papiezator/models.py +++ b/src/papiezator/models.py @@ -1,26 +1,27 @@ 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) width = models.IntegerField() height = models.IntegerField() aspect_ratio = models.FloatField() - pope_version = models.ForeignKey('PopeVersion') + pope = models.ForeignKey('Pope') def __str__(self): return self.path.split('/')[-1] -class PopeVersion(models.Model): - pope_id = models.CharField(max_length=8, primary_key=True) - pope_name = models.CharField(max_length=200) +class Pope(models.Model): + id = models.IntegerField(primary_key=True) + name = models.CharField(max_length=200) def __str__(self): - return self.pope_name + return self.name class PerfectPope(models.Model): aspect_ratio = models.FloatField(primary_key=True) + image = models.ForeignKey('PopeImage') pope = models.ForeignKey('Pope') def __str__(self): diff --git a/src/papiezator/pope_utils.py b/src/papiezator/pope_utils.py index 2bffe3c..9e72699 100644 --- a/src/papiezator/pope_utils.py +++ b/src/papiezator/pope_utils.py @@ -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 io import BytesIO # FIXME: +from django.core.exceptions import ObjectDoesNotExist -def select_best_pope(aspect_ratio): - """ (float) -> Pope +def select_best_pope(aspect_ratio, pope): + """ (float, pope) -> PopeImage """ - pp = PerfectPope.objects.filter(aspect_ratio=aspect_ratio) + pp = PerfectPope.objects.filter(aspect_ratio=aspect_ratio, pope=pope) if pp: - return pp[0].pope - lte = Pope.objects.filter(aspect_ratio__lte=aspect_ratio).order_by('-aspect_ratio')[0:1] - gte = Pope.objects.filter(aspect_ratio__gte=aspect_ratio).order_by('aspect_ratio')[0:1] + return pp[0].image + 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) if r_lte >= r_gte: - return_pope = habemus_papam(aspect_ratio, gte[0]) + return_pope = habemus_papam(aspect_ratio, gte[0], pope) else: - return_pope = habemus_papam(aspect_ratio, lte[0]) + return_pope = habemus_papam(aspect_ratio, lte[0], pope) elif gte: return_pope = gte[0] @@ -31,10 +32,10 @@ def select_best_pope(aspect_ratio): return return_pope -def habemus_papam(aspect_ratio, pope): - pp = PerfectPope(aspect_ratio=aspect_ratio, pope=pope) +def habemus_papam(aspect_ratio, image, pope): + pp = PerfectPope(aspect_ratio=aspect_ratio, image=image, pope=pope) pp.save() # FIXME: - return pope + return image def unpopable(width, height): @@ -62,7 +63,14 @@ def read_pope(width, height, pope): def parse_pope(path): im = Image.open(path) width, height = im.size - aspect_ratio = round(width/height, decimal_places) - return Pope(path=path, width=width, height=height, aspect_ratio=aspect_ratio) + aspect_ratio = round(width/height, DECIMAL_PLACES) + 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 + diff --git a/src/papiezator/urls.py b/src/papiezator/urls.py index 58a4bbf..98d43cc 100644 --- a/src/papiezator/urls.py +++ b/src/papiezator/urls.py @@ -6,6 +6,6 @@ from papiezator import views urlpatterns = patterns('', url(r'^$', views.index, name='index'), url(r'^(?P\d+)/(?P\d+)/$', views.conclave, name='conclave'), - url(r'^(?P\d+)/(?P\d+)/(?Pd+)$', views.grand_conclave, name='grand_conclave'), + url(r'^(?P\d+)/(?P\d+)/(?P\d+)/$', views.conclave, name='grand_conclave'), ) diff --git a/src/papiezator/views.py b/src/papiezator/views.py index 4c60550..c09264d 100644 --- a/src/papiezator/views.py +++ b/src/papiezator/views.py @@ -2,8 +2,8 @@ from django.shortcuts import render from django.http import HttpResponse from django.core.urlresolvers import reverse -from papiezator.pope_utils import select_best_pope, read_pope, unpopable -from papiezator.models import decimal_places +from papiezator.pope_utils import select_best_pope, read_pope, unpopable, pope_or_death +from papiezator.models import DECIMAL_PLACES, Pope def index(request): @@ -13,27 +13,30 @@ def index(request): } 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) 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: - return HttpResponse( - read_pope(width, height, pope), - mimetype="image/jpeg" - ) + 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), + mimetype="image/jpeg" + ) return HttpResponse(":c") -def grand_conclave(request, width, height, pope_id): - width, height = int(width), int(height) - if not unpopable(width, height): +#def grand_conclave(request, width, height, pope_id): + #width, height = int(width), int(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 = select_best_pope(aspect_ratio) + #pope = pope_or_death(pope_id=pope_id) + #pope = select_best_pope(aspect_ratio) diff --git a/src/populate_popes b/src/populate_popes index b7b68c6..d7de2a1 100644 --- a/src/populate_popes +++ b/src/populate_popes @@ -3,9 +3,8 @@ import os os.environ ['PYTHONPATH'] = '/home/daz/Documents/Projects/Py/django/papiez_ipsum' os.environ['DJANGO_SETTINGS_MODULE'] = 'papiez_ipsum.settings' from glob import glob -from papiezator.models import Pope, PopeVersion -from papiezator.pope_utils import parse_pope -from django.core.exceptions import ObjectDoesNotExist +from papiezator.models import PopeImage, Pope +from papiezator.pope_utils import parse_pope, pope_or_death from os import path from optparse import OptionParser 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): - pope = pope_or_death(PopeVersion, pope_id=meta["id"]) + pope = pope_or_death(Pope, id=meta["id"]) if pope: return pope else: - pope = PopeVersion(meta["id"], meta["name"]) + pope = Pope(meta["id"], meta["name"]) pope.save() return pope def add_pope_entry(pope, path): - if pope_or_death(Pope, path=path): + if pope_or_death(PopeImage, path=path): + print('-', end='') return False pope_image = parse_pope(path) - pope_image.pope_version = pope + pope_image.pope = pope pope_image.save() + print('+', end='') return pope_image def main(): @@ -50,6 +44,7 @@ def main(): for pl in pope_lists: pope = pl.split('_')[0] + print(pope) if os.path.exists(pope+"_meta"): with open(pope+"_meta") as file: