Something fixed
parent
56d039fedc
commit
0b334a52e2
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -6,6 +6,6 @@ from papiezator import views
|
|||
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>d+)$', views.grand_conclave, name='grand_conclave'),
|
||||
url(r'^(?P<width>\d+)/(?P<height>\d+)/(?P<pope_id>\d+)/$', views.conclave, name='grand_conclave'),
|
||||
)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue