Unverified Commit 10ee3a85 authored by CapsLock's avatar CapsLock

[feature] test if picture is really a picture, picture ajax load

Signed-off-by: 's avatarCapsLock <faimaison@legeox.net>
parent 60c3c849
......@@ -3,6 +3,7 @@ import bottle_pgsql
import bottle
import datetime
from urllib.parse import urlparse
from PIL import Image
import os
domain_name = 'coin.pan'
......@@ -76,16 +77,21 @@ def orig():
@app.route('/')
@app.route('/', method='POST')
def default(db):
# if row:
# return template('showitem', page=row)
# #return HTTPError(404, "Entity not found")
# return 'not found'
status = ''
if 'status' in request.params:
status = request.params['status']
domain_names = []
pictures = []
if get_host_name() == '' or request.forms.get('searchTerms') is not None or 'q' in request.params:
if get_host_name() == '' \
or request.forms.get('searchTerms') is not None \
or 'q' in request.params:
if get_host_name() != '':
redirect('%s?q=%s' % (get_root_url(), request.forms.get('searchTerms')))
redirect(u'{0:s}?q={1:s}'.format(get_root_url(),
request.forms.get('searchTerms')))
if request.forms.get('searchTerms') is not None or 'q' in request.params:
if request.forms.get('searchTerms') is not None:
squery = request.forms.get('searchTerms')
......@@ -127,12 +133,14 @@ def default(db):
display_new_links=get_host_name() != '',
scheme=get_scheme(),
aliases=aliases,
site_name=site_name)
site_name=site_name,
status=status)
# upload new pictures
@app.route('/new', method='POST')
def do_upload(db):
status = ''
domain = request.forms.get('domain')
description = request.forms.get('description')
upload = request.files.get('upload')
......@@ -141,39 +149,81 @@ def do_upload(db):
if ext not in ('.png', '.jpg', '.jpeg', '.gif'):
return 'File extension not allowed.'
destination_filename = os.path.join('uploads', '%s%s' %
(datetime.datetime.now().
strftime('%Y%m%d%H%M%s%f'),
ext))
db.execute(
"INSERT INTO pictures(filename, description, created_on) values (%s, %s, %s)",
(destination_filename, description, datetime.datetime.now()))
dest_folder = 'uploads'
dest_thumb_folder = os.path.join(dest_folder, 'thumbs')
temp_folder = os.path.join(dest_folder, 'temp')
temp_thumb_folder = os.path.join(temp_folder, 'thumbs')
if (not os.path.exists(dest_folder)
or not os.path.isdir(dest_folder)):
os.makedirs(dest_folder)
if (not os.path.exists(dest_thumb_folder)
or not os.path.isdir(dest_thumb_folder)):
os.makedirs(dest_thumb_folder)
if (not os.path.exists(temp_folder)
or not os.path.isdir(temp_folder)):
os.makedirs(temp_folder)
if (not os.path.exists(temp_thumb_folder)
or not os.path.isdir(temp_thumb_folder)):
os.makedirs(temp_thumb_folder)
image_name = '%s%s' % (datetime.datetime.now().strftime('%Y%m%d%H%M%s%f'), ext)
db.execute("select lastval()")
picture_id = db.fetchone()['lastval']
image_path = os.path.join(dest_folder, image_name)
thumb_path = os.path.join(dest_thumb_folder, image_name)
db.execute("select * from domain_names where name=%s", (domain, ))
domain_entity = db.fetchone()
temp_image = os.path.join(temp_folder, image_name)
temp_thumb = os.path.join(temp_thumb_folder, image_name)
domain_id = None
upload.save(temp_image)
try:
img = Image.open(temp_image)
img.thumbnail((400, 400), Image.ANTIALIAS)
img.save(temp_thumb)
except IOError as e:
status = 'errorConv'
print('io: %s' % e)
except Exception as e:
status = 'uErrorConf'
print(e)
if os.path.exists(temp_thumb):
status = 'OK'
os.rename(temp_image, image_path)
os.rename(temp_thumb, thumb_path)
if not domain_entity:
db.execute(
"insert into domain_names (name, created_on) values (%s, %s)",
(domain, datetime.datetime.now()))
"INSERT INTO pictures(filename, description, created_on) values (%s, %s, %s)",
(image_name, description, datetime.datetime.now()))
db.execute("select lastval()")
domain_id = db.fetchone()['lastval']
else:
domain_id = domain_entity['id']
picture_id = db.fetchone()['lastval']
db.execute("select * from domain_names where name=%s", (domain, ))
domain_entity = db.fetchone()
db.connection.commit()
db.execute(
"INSERT INTO pictures_domain_names(picture_id, domain_name_id, created_on) values (%s, %s, %s)",
(picture_id, domain_id, datetime.datetime.now()))
domain_id = None
if not domain_entity:
db.execute(
"insert into domain_names (name, created_on) values (%s, %s)",
(domain, datetime.datetime.now()))
db.execute("select lastval()")
domain_id = db.fetchone()['lastval']
else:
domain_id = domain_entity['id']
db.connection.commit()
db.execute(
"INSERT INTO pictures_domain_names(picture_id, domain_name_id, created_on) values (%s, %s, %s)",
(picture_id, domain_id, datetime.datetime.now()))
save_path = os.path.join(PROJECT_PATH, destination_filename)
upload.save(save_path) # appends upload.filename automatically
return redirect('/')
return redirect('/?status=%s' % status)
@app.route('/newAlias', method='POST')
......
......@@ -12,6 +12,10 @@
text-align: center;
}
.alignCenter {
text-align: center;
}
.alignRight {
text-align: right;
}
......
$('.linkModalPicture').click(function(){
$('#'+$(this).data('pic-id')).prop('src', $(this).data('pic-url'));
});
This diff is collapsed.
......@@ -3,6 +3,25 @@
<hr />
</div>
% if status != '':
<div class="row">
% if status == 'OK':
<div class="alert-box success">
Picture added !
</div>
% elif status == 'errorConv':
<div class="alert-box alert">
Error while processing your upload
</div>
% elif status == 'uErrorConv':
<div class="alert-box alert">
Error while processing your upload, please contact us
</div>
% end
</div>
% end
% item_count = 0
% tag_closed = True
......@@ -41,15 +60,15 @@
<div class="panel pictures">
<div id="modal-{{p['id']}}" class="reveal-modal" data-reveal>
<h2>{{p['description']}}</h2>
<p style="text-align: center;">
<img src="{{p['filename']}}" alt="{{p['description']}}"/>
<p id="img-placeholder-{{p['id']}}" class="alignCenter">
<img id="pic-{{p['id']}}" alt="{{p['description']}}"/><!-- src="uploads/{{p['filename']}}"-->
</p>
<a class="close-reveal-modal">&#215;</a>
</div>
<p>
<a href="#" data-reveal-id="modal-{{p['id']}}">
<img src="{{p['filename']}}" alt="{{p['description']}}"/>
<a href="#" class="linkModalPicture" data-reveal-id="modal-{{p['id']}}" data-pic-url="uploads/{{p['filename']}}" data-pic-placeholder="img-placeholder-{{p['id']}}" data-pic-id="pic-{{p['id']}}">
<img src="uploads/thumbs/{{p['filename']}}" alt="{{p['description']}}"/>
</a>
</p>
</div>
......@@ -96,4 +115,5 @@
%if not tag_closed:
</div>
%end
%end
\ No newline at end of file
%end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment