Merge pull request #40 from numahell/9-upload-images

Upload images
This commit is contained in:
Guillaume Ayoub 2018-10-05 17:07:14 +02:00 committed by GitHub
commit d8589223fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 17 deletions

View File

@ -151,7 +151,8 @@ def save_post(name, token=None):
timestamp = None
try:
post = data.save_post(
name, timestamp=timestamp, admin=False, form=request.form
name, timestamp=timestamp, admin=False,
form=request.form, files=request.files
)
except data.DataException as e:
abort(e.http_code)
@ -170,10 +171,13 @@ def save_post_admin(name, timestamp):
abort(404)
try:
data.save_post(
name, timestamp=timestamp, admin=True, form=request.form
name, timestamp=timestamp, admin=True,
form=request.form, files=request.files
)
except data.DataException as e:
abort(e.http_code)
if 'delete_image' in request.form:
return redirect(request.url)
return redirect(url_for('admin', name=name))

View File

@ -1,7 +1,9 @@
import email
import time
import os
from pathlib import Path
from xml.etree import ElementTree
from werkzeug.utils import secure_filename
POST_ACTUALITIES = 'actualites'
@ -14,7 +16,12 @@ STATES = {STATE_WAITING: "En attente", STATE_PUBLISHED: "Publié"}
ACTION_PUBLISH = 'publish'
ACTION_UNPUBLISH = 'unpublish'
ACTIONS = {ACTION_PUBLISH: "Publier", ACTION_UNPUBLISH: "Dépublier"}
ACTION_DELETE_IMAGE = 'delete_image'
ACTIONS = {
ACTION_PUBLISH: "Publier",
ACTION_UNPUBLISH: "Dépublier",
ACTION_DELETE_IMAGE: "Supprimer l'image"
}
IMAGE = '_image'
TIMESTAMP = '_timestamp'
@ -89,7 +96,7 @@ def get_post(category, timestamp, states=None):
return post
def save_post(category, timestamp, admin, form):
def save_post(category, timestamp, admin, form, files):
if timestamp is None:
status = STATE_WAITING
timestamp = str(int(time.time()))
@ -104,11 +111,28 @@ def save_post(category, timestamp, admin, form):
post = get_path(category, status, timestamp, BASE_FILE, create_dir=True)
tree = ElementTree.Element('entry')
for key, value in form.items():
if key.startswith('_'):
continue
element = ElementTree.SubElement(tree, key)
element.text = value
if '_image_path' in form:
image_path = root / form['_image_path']
if ACTION_DELETE_IMAGE in form and image_path.exists:
image_path.unlink()
else:
element = ElementTree.SubElement(tree, 'image')
element.text = image_path.name
if 'image' in files:
post_image = files['image']
filename = secure_filename(post_image.filename)
post_image.save(str(post.parent / filename))
element = ElementTree.SubElement(tree, 'image')
element.text = filename
element = ElementTree.SubElement(tree, STATE_PUBLISHED)
element.text = email.utils.formatdate(
int(timestamp) if timestamp else time.time()

View File

@ -37,7 +37,7 @@ label
textarea
height: 5em
input[type="submit"]
.button
background: $action
border: 0
color: $text
@ -234,6 +234,10 @@ dd
time
display: block
article
img
max-width: 100%
#actualites main, #emplois main, #index-news
box-sizing: border-box
display: flex
@ -247,9 +251,6 @@ time
padding: 2em
word-wrap: break-word
img
max-width: 100%
a
color: $action-secondary
font-size: .8em

View File

@ -28,7 +28,7 @@ label {
textarea {
height: 5em; }
input[type="submit"] {
.button {
background: #2e5cfd;
border: 0;
color: #eaeaea;
@ -39,7 +39,7 @@ input[type="submit"] {
text-transform: uppercase;
transition: background 250ms;
width: auto; }
input[type="submit"]:hover {
.button:hover {
background: #4770fd; }
.nicEdit-panelContain, .nicEdit-pane {
@ -208,6 +208,9 @@ dd {
time {
display: block; }
article img {
max-width: 100%; }
#actualites main, #emplois main, #index-news {
box-sizing: border-box;
display: flex;
@ -219,8 +222,6 @@ time {
flex: 1 50%;
padding: 2em;
word-wrap: break-word; }
#actualites main article img, #emplois main article img, #index-news article img {
max-width: 100%; }
#actualites main article a, #emplois main article a, #index-news article a {
color: #ffcd05;
font-size: .8em;

View File

@ -23,13 +23,21 @@
{% block main %}
<article>
<form method="post">
<form method="post" enctype="multipart/form-data">
<label>Titre
<input name="title" value="{{ post.title }}" />
</label>
<label>Description
<textarea name="summary">{{ post.summary }}</textarea>
</label>
<label>Image
{% if post._image %}
<img alt="" src="{{ url_for('post_image', path=post._image) }}" />
<input name="_image_path" id="_image_path" value="{{ post._image }}" type="hidden"/>
<input type="submit" name="delete_image" value="Supprimer l'image" class="button" />
{% endif %}
<input name="image" id="image" type="file" value="{{ post.image }}" />
</label>
<label>Contenu de l'article
<textarea name="content" id="content">{{ post.content }}</textarea>
</label>
@ -50,12 +58,12 @@
<label>Adresse e-mail
<input name="email" type="email" value="{{ post.email }}" />
</label>
<input type="submit" value="Enregistrer" />
<input type="submit" value="Enregistrer" class="button" />
{% if admin %}
{% if post.state == 'waiting' %}
<input type="submit" name="publish" value="Publier" />
<input type="submit" name="publish" value="Publier" class="button" />
{% else %}
<input type="submit" name="unpublish" value="Dépublier" />
<input type="submit" name="unpublish" value="Dépublier" class="button" />
{% endif %}
{% endif %}
</form>

View File

@ -16,7 +16,7 @@
Il est possible de soutenir le développement de l'AFPy en cotisant ou en effectuant un don.
</p>
<form action="{{ url_for('pages', name='adhesions') }}">
<input type="submit" value="S'inscrire" />
<input type="submit" value="S'inscrire" class="button" />
</form>
<h2>Actualités</h2>

View File

@ -32,3 +32,10 @@ def test_404():
assert response.status_code == 404
response = app.test_client().get('/feed/unknown')
assert response.status_code == 404
def test_read_posts():
response = app.test_client().get('/posts/actualites')
assert response.status_code == 200
response = app.test_client().get('/posts/emplois')
assert response.status_code == 200