Last time I said it works? I was kidding. Try this.

master
daz 2013-12-22 14:31:17 +01:00
parent cd3a72f8c7
commit 56d039fedc
6 changed files with 105 additions and 42 deletions

View File

@ -3,7 +3,7 @@ from django.db import models
decimal_places = 3
class Pope(models.Model):
path = models.CharField(max_length=200)
path = models.CharField(max_length=200, primary_key=True)
width = models.IntegerField()
height = models.IntegerField()
aspect_ratio = models.FloatField()
@ -13,7 +13,7 @@ class Pope(models.Model):
return self.path.split('/')[-1]
class PopeVersion(models.Model):
pope_id = models.AutoField(primary_key=True)
pope_id = models.CharField(max_length=8, primary_key=True)
pope_name = models.CharField(max_length=200)
def __str__(self):

View File

@ -1,5 +1,4 @@
from papiezator.models import Pope, PerfectPope, decimal_places
from papiezator.exceptions import ENOPopeException
from PIL import Image
from io import BytesIO # FIXME:
@ -26,9 +25,8 @@ def select_best_pope(aspect_ratio):
return_pope = gte[0]
elif lte:
return_pope = lte[0]
else:
raise ENOPopeException
return None
return return_pope
@ -39,6 +37,14 @@ def habemus_papam(aspect_ratio, pope):
return pope
def unpopable(width, height):
if width == 0 or height == 0:
return True
if (width+height) > 9000:
return True
return False
def read_pope(width, height, pope):
""" (int, int, Pope) -> bytes
@ -52,12 +58,11 @@ def read_pope(width, height, pope):
return f.read()
class PopeMaster:
def __init__(self):
pass
def parse_pope(self, pope):
im = Image.open(pope)
width, height = im.size
aspect_ratio = round(width/height, decimal_places)
return Pope(path=pope, width=width, height=height, aspect_ratio=aspect_ratio)
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)

View File

@ -10,8 +10,8 @@
<script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap-theme.css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
<style>
@ -35,6 +35,13 @@
<div class="jumbotron">
Jan Paweł II wypełniał małe strony
</div>
<h3>How do i popes?</h3>
<p> simply append width/height of the pope you need to the url.</p>
<pre>
<a href="{{ absolute_url }}">{{ absolute_url }}</a></pre>
<div class="row">
<div class="col-xs-5">
<h2>Get your popes</h2>

View File

@ -5,7 +5,7 @@ from papiezator import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^dupa', 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'),
)

View File

@ -1,28 +1,39 @@
# Create your views here.
from django.shortcuts import render
from django.http import HttpResponse
from papiezator.pope_utils import select_best_pope, read_pope
from papiezator.exceptions import ENOPopeException
from django.core.urlresolvers import reverse
from papiezator.pope_utils import select_best_pope, read_pope, unpopable
from papiezator.models import decimal_places
def index(request):
return render(request, "papiezator/index.html")
c = {
"absolute_url": request.build_absolute_uri(reverse("papiezator:conclave", args=[200,300])),
"absolute_url_exp": request.build_absolute_uri("width/height/")
}
return render(request, "papiezator/index.html", c)
def conclave(request, width, height):
width, height = int(width), int(height)
if width == 0 or height == 0:
return HttpResponse("8====D~~~~")
if (width+height) > 9000:
return HttpResponse("ITS OVER NEIN THOUSAND!!!11")
if not unpopable(width, height):
aspect_ratio = round(width/height, decimal_places)
try:
aspect_ratio = round(width/height, decimal_places)
pope = select_best_pope(aspect_ratio)
except ENOPopeException:
return HttpResponse("czarny dym")
return HttpResponse(
read_pope(width, height, pope),
mimetype="image/jpeg"
)
if pope:
return HttpResponse(
read_pope(width, height, pope),
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):
aspect_ratio = round(width/height, decimal_places)
pope = pope_or_death(pope_id=pope_id)
pope = select_best_pope(aspect_ratio)

View File

@ -2,30 +2,70 @@
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 PopeMaster
from papiezator.pope_utils import parse_pope
from django.core.exceptions import ObjectDoesNotExist
from os import path
from optparse import OptionParser
import ast
PREFIX = "popes"
pm = PopeMaster()
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"])
if pope:
return pope
else:
pope = PopeVersion(meta["id"], meta["name"])
pope.save()
return pope
def add_pope_entry(pope, path):
if pope_or_death(Pope, path=path):
return False
pope_image = parse_pope(path)
pope_image.pope_version = pope
pope_image.save()
return pope_image
def main():
existing_popes = []
parser = OptionParser()
options, args = parser.parse_args()
with open('popes/JP2_list') as file:
jp2 = PopeVersion(0, "John Paul II")
jp2.save()
for line in file:
pope = line.rstrip()
p = pm.parse_pope(path.join(PREFIX, pope))
p.pope_version = jp2
p.save()
print(p)
pope_lists = glob("popes/*_list")
for pl in pope_lists:
pope = pl.split('_')[0]
if os.path.exists(pope+"_meta"):
with open(pope+"_meta") as file:
meta = ast.literal_eval(file.read())
if meta["id"] in existing_popes:
raise "jp2gmd"
existing_popes.append(meta["id"])
pope = init_pope(meta)
with open(pl) as file:
for line in file:
line = line.rstrip()
line = path.join("popes", line)
add_pope_entry(pope, line)
if __name__ == '__main__':