deleted images and image_edit templates
This commit is contained in:
BIN
tcs_site.sqlite
BIN
tcs_site.sqlite
Binary file not shown.
@@ -1,57 +0,0 @@
|
|||||||
{% extends "layout.jinja2" %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
<div class="row">
|
|
||||||
<div class="card mb-0">
|
|
||||||
<div class="card-body">
|
|
||||||
|
|
||||||
{% if message %}
|
|
||||||
<div class="alert alert-danger">{{ message }}</div>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
<form action="{{request.route_url('image_edit', filename=filename)}}" method="post" class="form needs-validation">
|
|
||||||
|
|
||||||
<div class="mb-3">
|
|
||||||
<label class="form-label required-field" for="new_name">Nouveau nom</label>
|
|
||||||
<input type="text" name="new_name" class="form-control" required value="{{ filename }}">
|
|
||||||
</div>
|
|
||||||
<br>
|
|
||||||
<div class="form-group">
|
|
||||||
<a class="btn btn-light" href="{{ request.route_url('images') }}"><span class="ti ti-chevron-left fs-4"></span> Retour</a>
|
|
||||||
<button class="btn btn-primary" type="submit" name="form.submitted">
|
|
||||||
<span class="ti ti-check fs-4"></span> Renommer</button>
|
|
||||||
<button class="btn btn-danger" type="button" data-bs-toggle="modal" data-bs-target="#confirmDelete">
|
|
||||||
<span class="ti ti-x fs-4"></span> Supprimer</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<div class="alert alert-primary text-center mt-3">
|
|
||||||
<img src="{{ file_url }}" alt="" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Modal : Confirmation SUPRESSION -->
|
|
||||||
<div id="confirmDelete" class="modal" role="dialog">
|
|
||||||
<div class="modal-dialog">
|
|
||||||
<div class="modal-content">
|
|
||||||
<div class="modal-header">
|
|
||||||
<h4 class="modal-title">Supprimer une IMAGES</h4>
|
|
||||||
</div>
|
|
||||||
<div class="modal-body">
|
|
||||||
<!-- The form is placed inside the body of modal -->
|
|
||||||
<p>Etes-vous certain(e) de vouloir supprimer cette image :<br>
|
|
||||||
<b>{{ filename }}</b> ?</p>
|
|
||||||
</div>
|
|
||||||
<div class="modal-footer">
|
|
||||||
<form id="confirmForm" method="post">
|
|
||||||
<button type="submit" class="btn btn-danger" name="form.deleted">Supprimer</button>
|
|
||||||
<button type="button" class="btn btn-success" data-bs-dismiss="modal">Fermer</button>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{% endblock %}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
{% extends "layout.jinja2" %}
|
|
||||||
{% block content %}
|
|
||||||
|
|
||||||
<div class="card mb-0">
|
|
||||||
<div class="card-body">
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<button class="btn btn-success" type="button" data-bs-toggle="modal" data-bs-target="#confirmAdd">
|
|
||||||
<span class="ti ti-plus"></span> Nouvelle image</button>
|
|
||||||
|
|
||||||
</p>
|
|
||||||
<br />
|
|
||||||
<table id="images_list" class="table table-striped table-bordered table-sm">
|
|
||||||
{% for entry in images_list %}
|
|
||||||
<tr>
|
|
||||||
<td><a href="{{request.route_url('image_edit', filename=entry[0])}}">{{ entry[0] }}</a></td>
|
|
||||||
<td class="text-right">{{ entry[1] }}</td>
|
|
||||||
</tr>
|
|
||||||
{% endfor %}
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<!-- Modal : Confirmation AJOUT -->
|
|
||||||
<div id="confirmAdd" class="modal">
|
|
||||||
<div class="modal-dialog">
|
|
||||||
<div class="modal-content">
|
|
||||||
<div class="modal-header">
|
|
||||||
<h4 class="modal-title">Ajouter une image</h4>
|
|
||||||
</div>
|
|
||||||
<div class="modal-body">
|
|
||||||
<p>
|
|
||||||
- Seuls les documents au format <b>PNG ou JPG</b> seront acceptés.<br>
|
|
||||||
- La taille de chaque document ne doit <b>pas dépasser 4 Mo</b>.
|
|
||||||
</p>
|
|
||||||
<form id="upload_doc-form" action="{{ request.route_url('images') }}" method="post"
|
|
||||||
accept-charset="utf-8" enctype="multipart/form-data">
|
|
||||||
<div class="pb-3">
|
|
||||||
<label class="form-label" for="uploadfile">Veuillez séléctionner un fichier :</label>
|
|
||||||
<input class="form-control" 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="ti ti-upload fs-6"></i> Télécharger</button>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
<div class="modal-footer">
|
|
||||||
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">Fermer</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{% endblock %}
|
|
||||||
@@ -13,12 +13,6 @@
|
|||||||
<span class="hide-menu">Topics / Tags</span>
|
<span class="hide-menu">Topics / Tags</span>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="sidebar-item mb-3">
|
|
||||||
<a class="sidebar-link" href=" {{request.route_url('images')}} " aria-expanded="false">
|
|
||||||
<span><i class="ti ti-photo fs-6"></i></span>
|
|
||||||
<span class="hide-menu">Images</span>
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
<li class="sidebar-item mb-3">
|
<li class="sidebar-item mb-3">
|
||||||
<a class="sidebar-link" href=" {{request.route_url('topic', topic='_admin')}} " aria-expanded="false">
|
<a class="sidebar-link" href=" {{request.route_url('topic', topic='_admin')}} " aria-expanded="false">
|
||||||
<span><i class="ti ti-files fs-6"></i></span>
|
<span><i class="ti ti-files fs-6"></i></span>
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ from pyramid_mailer.message import Message, Attachment
|
|||||||
from ..models.users import *
|
from ..models.users import *
|
||||||
from ..models.entries import *
|
from ..models.entries import *
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from PIL import Image
|
|
||||||
from urllib import request, parse
|
from urllib import request, parse
|
||||||
from passlib.apps import custom_app_context as blogger_pwd_context
|
from passlib.apps import custom_app_context as blogger_pwd_context
|
||||||
|
|
||||||
@@ -352,114 +351,3 @@ def tag_edit(request):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@view_config(route_name='images', renderer='../templates/images.jinja2')
|
|
||||||
def images(request):
|
|
||||||
|
|
||||||
folder_path = request.registry.settings['images_dir']
|
|
||||||
|
|
||||||
images_list = []
|
|
||||||
# lire tous les fichiers du dossier
|
|
||||||
for f in os.scandir(folder_path):
|
|
||||||
file = []
|
|
||||||
file_url = request.static_url('tcs_site:static/img/') + f.name
|
|
||||||
file_path = folder_path + f.name
|
|
||||||
file.append(f.name)
|
|
||||||
file.append('%s Ko' % round(os.path.getsize(file_path) / 1024))
|
|
||||||
file.append(file_url)
|
|
||||||
images_list.append(file)
|
|
||||||
|
|
||||||
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 = os.path.join(folder_path, input_name)
|
|
||||||
# supprimer le fichier s'il existe déjà
|
|
||||||
if os.path.exists(temp_file):
|
|
||||||
os.remove(temp_file)
|
|
||||||
|
|
||||||
# copie le fichier upload dans temp_file
|
|
||||||
input_file.seek(0)
|
|
||||||
with open(temp_file, 'wb') as output_file:
|
|
||||||
shutil.copyfileobj(input_file, output_file)
|
|
||||||
|
|
||||||
# controler la taille du fichier < 4 Mo
|
|
||||||
filesize = round(os.path.getsize(temp_file) / 1024)
|
|
||||||
if filesize > 4096:
|
|
||||||
os.remove(temp_file)
|
|
||||||
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)
|
|
||||||
|
|
||||||
request.session.flash('%s : Ce fichier est téléchargé avec succès.' % input_name, 'success')
|
|
||||||
return HTTPFound(location=request.route_url('images'))
|
|
||||||
|
|
||||||
return {
|
|
||||||
'page_title': "Images dans /static/img",
|
|
||||||
'images_list': images_list,
|
|
||||||
}
|
|
||||||
|
|
||||||
@view_config(route_name='image_edit', renderer='../templates/image_edit.jinja2')
|
|
||||||
def image_edit(request):
|
|
||||||
filename = request.matchdict['filename']
|
|
||||||
|
|
||||||
message = ''
|
|
||||||
folder_path = request.registry.settings['images_dir']
|
|
||||||
|
|
||||||
if 'form.submitted' in request.params:
|
|
||||||
new_name = request.params["new_name"].lower()
|
|
||||||
ext = new_name[-3:]
|
|
||||||
if ext == "jpg" or ext == "png":
|
|
||||||
os.rename(folder_path + filename, folder_path + new_name)
|
|
||||||
return HTTPFound(location=request.route_url('images'))
|
|
||||||
else:
|
|
||||||
message = "Nom d'image invalide !"
|
|
||||||
|
|
||||||
if 'form.deleted' in request.params:
|
|
||||||
os.remove(folder_path + filename)
|
|
||||||
request.session.flash("Le fichier a été supprimé avec succès.", 'success')
|
|
||||||
return HTTPFound(location=request.route_url('images'))
|
|
||||||
|
|
||||||
return {
|
|
||||||
'page_title': "Modifier : " + filename,
|
|
||||||
'message': message,
|
|
||||||
'filename': filename,
|
|
||||||
'file_url': request.static_url('tcs_site: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)
|
|
||||||
else:
|
|
||||||
img_anti = img_org
|
|
||||||
# create a new file name for saving the result
|
|
||||||
img_anti.save(image_file)
|
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user