forked from AFPy/afpy.org
Fixing path with images and timestamp
This commit is contained in:
parent
c651cd4248
commit
8b05470cd4
51
afpy.py
51
afpy.py
|
@ -2,7 +2,6 @@ import email
|
||||||
import locale
|
import locale
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import docutils.core
|
import docutils.core
|
||||||
import docutils.writers.html5_polyglot
|
import docutils.writers.html5_polyglot
|
||||||
|
@ -59,12 +58,11 @@ def index():
|
||||||
for post in data.get_posts(data.POST_ACTUALITIES, end=4):
|
for post in data.get_posts(data.POST_ACTUALITIES, end=4):
|
||||||
timestamp = post[data.TIMESTAMP]
|
timestamp = post[data.TIMESTAMP]
|
||||||
posts[timestamp] = post
|
posts[timestamp] = post
|
||||||
if (post[data.DIR] / 'post.jpg').is_file():
|
|
||||||
posts[timestamp]['image'] = '/'.join(
|
|
||||||
(data.POST_ACTUALITIES, data.STATE_PUBLISHED, timestamp)
|
|
||||||
)
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'index.html', body_id='index', name=data.POST_ACTUALITIES, posts=posts
|
'index.html',
|
||||||
|
body_id='index',
|
||||||
|
name=data.POST_ACTUALITIES,
|
||||||
|
posts=posts
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -90,7 +88,10 @@ def rest(name):
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
abort(404)
|
abort(404)
|
||||||
return render_template(
|
return render_template(
|
||||||
'rst.html', body_id=name, html=parts['body'], title=parts['title']
|
'rst.html',
|
||||||
|
body_id=name,
|
||||||
|
html=parts['body'],
|
||||||
|
title=parts['title']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -128,7 +129,11 @@ def edit_post_admin(name, timestamp):
|
||||||
if not post:
|
if not post:
|
||||||
abort(404)
|
abort(404)
|
||||||
return render_template(
|
return render_template(
|
||||||
'edit_post.html', body_id='edit-post', post=post, name=name, admin=True
|
'edit_post.html',
|
||||||
|
body_id='edit-post',
|
||||||
|
post=post,
|
||||||
|
name=name,
|
||||||
|
admin=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -153,7 +158,10 @@ def save_post(name, token=None):
|
||||||
edit_post_url = url_for(
|
edit_post_url = url_for(
|
||||||
'edit_post', name=name, token=signer.dumps(post['_timestamp'])
|
'edit_post', name=name, token=signer.dumps(post['_timestamp'])
|
||||||
)
|
)
|
||||||
return render_template('confirmation.html', edit_post_url=edit_post_url)
|
return render_template(
|
||||||
|
'confirmation.html',
|
||||||
|
edit_post_url=edit_post_url
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/admin/post/edit/<name>/<timestamp>', methods=['post'])
|
@app.route('/admin/post/edit/<name>/<timestamp>', methods=['post'])
|
||||||
|
@ -185,10 +193,6 @@ def posts(name, page=1):
|
||||||
):
|
):
|
||||||
timestamp = post[data.TIMESTAMP]
|
timestamp = post[data.TIMESTAMP]
|
||||||
posts[timestamp] = post
|
posts[timestamp] = post
|
||||||
if (Path(str(timestamp)) / 'post.jpg').is_file():
|
|
||||||
posts[timestamp]['image'] = '/'.join(
|
|
||||||
(name, data.STATE_PUBLISHED, timestamp)
|
|
||||||
)
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'posts.html',
|
'posts.html',
|
||||||
body_id=name,
|
body_id=name,
|
||||||
|
@ -226,21 +230,24 @@ def post(name, timestamp):
|
||||||
post = data.get_post(name, timestamp, data.STATE_PUBLISHED)
|
post = data.get_post(name, timestamp, data.STATE_PUBLISHED)
|
||||||
if not post:
|
if not post:
|
||||||
abort(404)
|
abort(404)
|
||||||
if (post[data.DIR] / 'post.jpg').is_file():
|
return render_template(
|
||||||
post['image'] = '/'.join((name, data.STATE_PUBLISHED, timestamp))
|
'post.html',
|
||||||
return render_template('post.html', body_id='post', post=post, name=name)
|
body_id='post',
|
||||||
|
post=post,
|
||||||
|
name=name
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/post_image/<path:path>/post.jpg')
|
@app.route('/post_image/<path:path>')
|
||||||
def post_image(path):
|
def post_image(path):
|
||||||
if path.count('/') != 2:
|
if path.count('/') != 3:
|
||||||
abort(404)
|
abort(404)
|
||||||
name, status, timestamp = path.split('/')
|
category, state, timestamp, name = path.split('/')
|
||||||
if name not in data.POSTS:
|
if category not in data.POSTS:
|
||||||
abort(404)
|
abort(404)
|
||||||
if status not in data.STATES:
|
if state not in data.STATES:
|
||||||
abort(404)
|
abort(404)
|
||||||
return send_from_directory(data.root / path, 'post.jpg')
|
return send_from_directory(data.root, path)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/feed/<name>/rss.xml')
|
@app.route('/feed/<name>/rss.xml')
|
||||||
|
|
|
@ -16,6 +16,7 @@ ACTION_PUBLISH = 'publish'
|
||||||
ACTION_UNPUBLISH = 'unpublish'
|
ACTION_UNPUBLISH = 'unpublish'
|
||||||
ACTIONS = {ACTION_PUBLISH: "Publier", ACTION_UNPUBLISH: "Dépublier"}
|
ACTIONS = {ACTION_PUBLISH: "Publier", ACTION_UNPUBLISH: "Dépublier"}
|
||||||
|
|
||||||
|
IMAGE = '_image'
|
||||||
TIMESTAMP = '_timestamp'
|
TIMESTAMP = '_timestamp'
|
||||||
STATE = '_state'
|
STATE = '_state'
|
||||||
PATH = '_path'
|
PATH = '_path'
|
||||||
|
@ -23,6 +24,7 @@ DIR = '_dir'
|
||||||
|
|
||||||
BASE_DIR = 'posts'
|
BASE_DIR = 'posts'
|
||||||
BASE_FILE = 'post.xml'
|
BASE_FILE = 'post.xml'
|
||||||
|
BASE_IMAGE = 'post.jpg'
|
||||||
|
|
||||||
|
|
||||||
class DataException(Exception):
|
class DataException(Exception):
|
||||||
|
@ -75,7 +77,12 @@ def get_post(category, timestamp, states=None):
|
||||||
return None
|
return None
|
||||||
tree = ElementTree.parse(path)
|
tree = ElementTree.parse(path)
|
||||||
post = {item.tag: (item.text or '').strip() for item in tree.iter()}
|
post = {item.tag: (item.text or '').strip() for item in tree.iter()}
|
||||||
post[TIMESTAMP] = int(timestamp)
|
|
||||||
|
# Calculated fields
|
||||||
|
image = post.get('image') or BASE_IMAGE
|
||||||
|
if (dir / image).is_file():
|
||||||
|
post[IMAGE] = '/'.join((category, state, timestamp, image))
|
||||||
|
post[TIMESTAMP] = timestamp
|
||||||
post[STATE] = state
|
post[STATE] = state
|
||||||
post[DIR] = dir
|
post[DIR] = dir
|
||||||
post[PATH] = path
|
post[PATH] = path
|
||||||
|
|
|
@ -27,8 +27,8 @@
|
||||||
<time pubdate datetime="{{ post.published }}">
|
<time pubdate datetime="{{ post.published }}">
|
||||||
{{ post.published | parse_iso_datetime('%x') }}
|
{{ post.published | parse_iso_datetime('%x') }}
|
||||||
</time>
|
</time>
|
||||||
{% if post.image %}
|
{% if post._image %}
|
||||||
<img src="{{ url_for('post_image', path=post.image) }}" alt="{{ post.title }}" />
|
<img src="{{ url_for('post_image', path=post._image) }}" alt="{{ post.title }}" />
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{{ post.summary | safe }}
|
{{ post.summary | safe }}
|
||||||
<p><a href="{{ url_for('post', name=name, timestamp=timestamp) }}">Lire la suite…</a></p>
|
<p><a href="{{ url_for('post', name=name, timestamp=timestamp) }}">Lire la suite…</a></p>
|
||||||
|
|
|
@ -14,8 +14,8 @@
|
||||||
{{ post.summary | safe if post.summary }}
|
{{ post.summary | safe if post.summary }}
|
||||||
</em>
|
</em>
|
||||||
</p>
|
</p>
|
||||||
{% if post.image %}
|
{% if post._image %}
|
||||||
<img src="{{ url_for('post_image', path=post.image) }}" alt="{{ post.title }}" />
|
<img src="{{ url_for('post_image', path=post._image) }}" alt="{{ post.title }}" />
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{{ post.content | safe }}
|
{{ post.content | safe }}
|
||||||
</article>
|
</article>
|
||||||
|
|
|
@ -15,8 +15,8 @@
|
||||||
<time pubdate datetime="{{ post.published }}">
|
<time pubdate datetime="{{ post.published }}">
|
||||||
{{ post.published | parse_iso_datetime('%x') }}
|
{{ post.published | parse_iso_datetime('%x') }}
|
||||||
</time>
|
</time>
|
||||||
{% if post.image %}
|
{% if post._image %}
|
||||||
<img src="{{ url_for('post_image', path=post.image) }}" alt="{{ post.title }}" />
|
<img src="{{ url_for('post_image', path=post._image) }}" alt="{{ post.title }}" />
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{{ post.summary | safe if post.summary }}
|
{{ post.summary | safe if post.summary }}
|
||||||
<p><a href="{{ url_for('post', name=name, timestamp=timestamp) }}">Lire la suite…</a></p>
|
<p><a href="{{ url_for('post', name=name, timestamp=timestamp) }}">Lire la suite…</a></p>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user