diff --git a/afpy.py b/afpy.py
index 1caa267..1d51507 100644
--- a/afpy.py
+++ b/afpy.py
@@ -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))
diff --git a/data_xml.py b/data_xml.py
index d8360f3..e7c8d09 100644
--- a/data_xml.py
+++ b/data_xml.py
@@ -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()
diff --git a/sass/style.sass b/sass/style.sass
index 2c499c0..e785a32 100644
--- a/sass/style.sass
+++ b/sass/style.sass
@@ -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
diff --git a/static/css/style.sass.css b/static/css/style.sass.css
index 030f233..4d3813a 100644
--- a/static/css/style.sass.css
+++ b/static/css/style.sass.css
@@ -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;
diff --git a/templates/edit_post.html b/templates/edit_post.html
index a2f4245..c337a94 100644
--- a/templates/edit_post.html
+++ b/templates/edit_post.html
@@ -23,13 +23,21 @@
{% block main %}