added upload image
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="password">Nouveau nom</label>
|
<label for="password">Nouveau nom</label>
|
||||||
<input type="text" name="new_name" class="form-control" value="{{ filename }}">
|
<input type="text" name="new_name" class="form-control" value="{{ file }}">
|
||||||
</div>
|
</div>
|
||||||
<br>
|
<br>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
{% extends "layout.jinja2" %}
|
{% extends "layout.jinja2" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
<p>
|
||||||
|
<button class="btn btn-success" type="button" data-toggle="modal" data-target="#confirmAdd">
|
||||||
|
<span class="glyphicon glyphicon-plus"></span> Nouvelle image</button>
|
||||||
|
|
||||||
|
</p>
|
||||||
<br />
|
<br />
|
||||||
<div class="pictures">
|
<div class="pictures">
|
||||||
{% for item in images_list %}
|
{% for item in images_list %}
|
||||||
@@ -14,6 +18,38 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal : Confirmation AJOUT -->
|
||||||
|
<div id="confirmAdd" class="modal" role="dialog">
|
||||||
|
<div class="modal-dialog">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<button type="button" class="close" data-dismiss="modal">×</button>
|
||||||
|
<h4 class="modal-title">Ajouter une image</h4>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<form id="upload_doc-form" action="{{ request.route_url('images') }}" method="post"
|
||||||
|
accept-charset="utf-8" enctype="multipart/form-data">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="uploadfile">Veuillez séléctionner un fichier :</label>
|
||||||
|
<input class="file" id="uploadfile" name="uploadfile" type="file" multiple />
|
||||||
|
</div>
|
||||||
|
<br />
|
||||||
|
<div class="form-group">
|
||||||
|
<button id="uploadButton" class="btn btn-success" type="submit" name="form.submitted">
|
||||||
|
<i class="glyphicon glyphicon-arrow-up"></i> Télécharger</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="panel-footer">
|
||||||
|
<ul>
|
||||||
|
<li>Seuls les documents au format <b>PNG ou JPG</b> seront acceptés.</li>
|
||||||
|
<li>La taille de chaque document ne doit <b>pas dépasser 4 Mo</b>.</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<script src="{{ request.static_url('cao_blogr:static/js/jquery-tjgallery.min.js')}}"></script>
|
<script src="{{ request.static_url('cao_blogr:static/js/jquery-tjgallery.min.js')}}"></script>
|
||||||
<script>
|
<script>
|
||||||
// waiting for loading page
|
// waiting for loading page
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ from ..forms import UserCreateForm, TopicForm, TagForm
|
|||||||
from ..models.user import User
|
from ..models.user import User
|
||||||
from ..models.blog_record import Topics, Tags
|
from ..models.blog_record import Topics, Tags
|
||||||
import os
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
import shutil
|
||||||
|
import magic
|
||||||
|
|
||||||
|
|
||||||
@view_config(route_name='home',
|
@view_config(route_name='home',
|
||||||
@@ -296,6 +299,7 @@ def tag_edit(request):
|
|||||||
renderer='cao_blogr:templates/images.jinja2')
|
renderer='cao_blogr:templates/images.jinja2')
|
||||||
def images(request):
|
def images(request):
|
||||||
|
|
||||||
|
message = ''
|
||||||
folder_path = request.registry.settings['images_dir']
|
folder_path = request.registry.settings['images_dir']
|
||||||
|
|
||||||
images_list = []
|
images_list = []
|
||||||
@@ -305,8 +309,46 @@ def images(request):
|
|||||||
image.append(f.name)
|
image.append(f.name)
|
||||||
images_list.append(image)
|
images_list.append(image)
|
||||||
|
|
||||||
|
if 'form.submitted' in request.params:
|
||||||
|
if request.POST['uploadfile'] != b'':
|
||||||
|
# récupère le fichier download dans le dossier /tmp
|
||||||
|
input_file = request.POST['uploadfile'].file
|
||||||
|
input_name = request.POST['uploadfile'].filename
|
||||||
|
ext_allowed = ['image/jpeg', 'image/jpg', 'image/png']
|
||||||
|
|
||||||
|
mime = magic.from_buffer(input_file.read(), mime=True)
|
||||||
|
# types de fichiers autorisés ?
|
||||||
|
if mime not in ext_allowed:
|
||||||
|
request.session.flash("ERREUR: Le format du fichier n'est pas valide. Téléchargement impossible.", 'danger')
|
||||||
|
return HTTPFound(location=request.route_url('images'))
|
||||||
|
|
||||||
|
# Finally write the data to a temporary file
|
||||||
|
temp_file_path = os.path.join(folder_path, input_name)
|
||||||
|
# supprimer le fichier s'il existe déjà
|
||||||
|
if os.path.exists(temp_file_path):
|
||||||
|
os.remove(temp_file_path)
|
||||||
|
|
||||||
|
# copie le fichier upload dans temp_file
|
||||||
|
input_file.seek(0)
|
||||||
|
with open(temp_file_path, 'wb') as output_file:
|
||||||
|
shutil.copyfileobj(input_file, output_file)
|
||||||
|
|
||||||
|
# controler la taille du fichier < 4 Mo
|
||||||
|
filesize = round(os.path.getsize(temp_file_path) / 1024)
|
||||||
|
if filesize > 4096:
|
||||||
|
os.remove(temp_file_path)
|
||||||
|
request.session.flash("ERREUR: La taille du fichier dépasse la limite autorisée. Téléchargement impossible.", 'danger')
|
||||||
|
return HTTPFound(location=request.route_url('images'))
|
||||||
|
|
||||||
|
# using the Python Image Library (PIL) to resize an image
|
||||||
|
resize_photos(temp_file_path)
|
||||||
|
|
||||||
|
request.session.flash('%s : Ce fichier est téléchargé avec succès.' % input_name, 'success')
|
||||||
|
return HTTPFound(location=request.route_url('images'))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'page_title': "Images",
|
'page_title': "Images",
|
||||||
|
'message': message,
|
||||||
'images_list': images_list,
|
'images_list': images_list,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,3 +381,27 @@ def image_edit(request):
|
|||||||
'file_url': request.static_url('cao_blogr:static/img/') + filename,
|
'file_url': request.static_url('cao_blogr:static/img/') + filename,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def resize_photos(image_file):
|
||||||
|
# using the Python Image Library (PIL) to resize an image
|
||||||
|
img_org = Image.open(image_file)
|
||||||
|
# get the size of the original image
|
||||||
|
width_org, height_org = img_org.size
|
||||||
|
original_size = max(width_org, height_org)
|
||||||
|
|
||||||
|
MAX_SIZE = 800
|
||||||
|
# width or height > than MAX_SIZE ?
|
||||||
|
if original_size >= MAX_SIZE:
|
||||||
|
if (width_org > height_org):
|
||||||
|
resized_width = MAX_SIZE
|
||||||
|
resized_height = int(round((MAX_SIZE/float(width_org))*height_org))
|
||||||
|
else:
|
||||||
|
resized_height = MAX_SIZE
|
||||||
|
resized_width = int(round((MAX_SIZE/float(height_org))*width_org))
|
||||||
|
|
||||||
|
# best down-sizing filter
|
||||||
|
img_anti = img_org.resize((resized_width, resized_height), Image.ANTIALIAS)
|
||||||
|
# create a new file name for saving the result
|
||||||
|
img_anti.save(image_file)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -24,6 +24,8 @@ requires = [
|
|||||||
'wtforms==2.2.1', # form library
|
'wtforms==2.2.1', # form library
|
||||||
'webhelpers2==2.0', # various web building related helpers
|
'webhelpers2==2.0', # various web building related helpers
|
||||||
'passlib',
|
'passlib',
|
||||||
|
'python-magic',
|
||||||
|
'Pillow == 6.1.0',
|
||||||
]
|
]
|
||||||
|
|
||||||
tests_require = [
|
tests_require = [
|
||||||
|
|||||||
Reference in New Issue
Block a user