Last time I said it works? I was kidding. Try this.
parent
cd3a72f8c7
commit
56d039fedc
|
@ -3,7 +3,7 @@ from django.db import models
|
||||||
decimal_places = 3
|
decimal_places = 3
|
||||||
|
|
||||||
class Pope(models.Model):
|
class Pope(models.Model):
|
||||||
path = models.CharField(max_length=200)
|
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()
|
||||||
|
@ -13,7 +13,7 @@ class Pope(models.Model):
|
||||||
return self.path.split('/')[-1]
|
return self.path.split('/')[-1]
|
||||||
|
|
||||||
class PopeVersion(models.Model):
|
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)
|
pope_name = models.CharField(max_length=200)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
from papiezator.models import Pope, PerfectPope, decimal_places
|
from papiezator.models import Pope, PerfectPope, decimal_places
|
||||||
from papiezator.exceptions import ENOPopeException
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from io import BytesIO # FIXME:
|
from io import BytesIO # FIXME:
|
||||||
|
|
||||||
|
@ -26,9 +25,8 @@ def select_best_pope(aspect_ratio):
|
||||||
return_pope = gte[0]
|
return_pope = gte[0]
|
||||||
elif lte:
|
elif lte:
|
||||||
return_pope = lte[0]
|
return_pope = lte[0]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ENOPopeException
|
return None
|
||||||
|
|
||||||
return return_pope
|
return return_pope
|
||||||
|
|
||||||
|
@ -39,6 +37,14 @@ def habemus_papam(aspect_ratio, pope):
|
||||||
return 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):
|
def read_pope(width, height, pope):
|
||||||
""" (int, int, Pope) -> bytes
|
""" (int, int, Pope) -> bytes
|
||||||
|
@ -52,12 +58,11 @@ def read_pope(width, height, pope):
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
|
|
||||||
class PopeMaster:
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def parse_pope(self, pope):
|
def parse_pope(path):
|
||||||
im = Image.open(pope)
|
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=pope, width=width, height=height, aspect_ratio=aspect_ratio)
|
return Pope(path=path, width=width, height=height, aspect_ratio=aspect_ratio)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,8 @@
|
||||||
<script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
|
<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="//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.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-theme.css">
|
||||||
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
|
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
@ -35,6 +35,13 @@
|
||||||
<div class="jumbotron">
|
<div class="jumbotron">
|
||||||
Jan Paweł II wypełniał małe strony
|
Jan Paweł II wypełniał małe strony
|
||||||
</div>
|
</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="row">
|
||||||
<div class="col-xs-5">
|
<div class="col-xs-5">
|
||||||
<h2>Get your popes</h2>
|
<h2>Get your popes</h2>
|
||||||
|
|
|
@ -5,7 +5,7 @@ from papiezator import views
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^$', views.index, name='index'),
|
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+)/$', views.conclave, name='conclave'),
|
||||||
|
url(r'^(?P<width>\d+)/(?P<height>\d+)/(?P<pope>d+)$', views.grand_conclave, name='grand_conclave'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,39 @@
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from papiezator.pope_utils import select_best_pope, read_pope
|
from django.core.urlresolvers import reverse
|
||||||
from papiezator.exceptions import ENOPopeException
|
from papiezator.pope_utils import select_best_pope, read_pope, unpopable
|
||||||
from papiezator.models import decimal_places
|
from papiezator.models import decimal_places
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
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):
|
def conclave(request, width, height):
|
||||||
width, height = int(width), int(height)
|
width, height = int(width), int(height)
|
||||||
if width == 0 or height == 0:
|
if not unpopable(width, height):
|
||||||
return HttpResponse("8====D~~~~")
|
|
||||||
if (width+height) > 9000:
|
|
||||||
return HttpResponse("ITS OVER NEIN THOUSAND!!!11")
|
|
||||||
|
|
||||||
aspect_ratio = round(width/height, decimal_places)
|
aspect_ratio = round(width/height, decimal_places)
|
||||||
try:
|
|
||||||
pope = select_best_pope(aspect_ratio)
|
pope = select_best_pope(aspect_ratio)
|
||||||
except ENOPopeException:
|
|
||||||
return HttpResponse("czarny dym")
|
|
||||||
|
|
||||||
return HttpResponse(
|
if pope:
|
||||||
read_pope(width, height, pope),
|
return HttpResponse(
|
||||||
mimetype="image/jpeg"
|
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)
|
||||||
|
|
|
@ -2,30 +2,70 @@
|
||||||
import os
|
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 papiezator.models import Pope, PopeVersion
|
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 os import path
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
import ast
|
||||||
|
|
||||||
PREFIX = "popes"
|
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():
|
def main():
|
||||||
|
existing_popes = []
|
||||||
parser = OptionParser()
|
parser = OptionParser()
|
||||||
options, args = parser.parse_args()
|
options, args = parser.parse_args()
|
||||||
|
|
||||||
with open('popes/JP2_list') as file:
|
pope_lists = glob("popes/*_list")
|
||||||
jp2 = PopeVersion(0, "John Paul II")
|
|
||||||
jp2.save()
|
for pl in pope_lists:
|
||||||
for line in file:
|
pope = pl.split('_')[0]
|
||||||
pope = line.rstrip()
|
|
||||||
p = pm.parse_pope(path.join(PREFIX, pope))
|
if os.path.exists(pope+"_meta"):
|
||||||
p.pope_version = jp2
|
with open(pope+"_meta") as file:
|
||||||
p.save()
|
meta = ast.literal_eval(file.read())
|
||||||
print(p)
|
|
||||||
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue