Get editor hooked up again

This commit is contained in:
Tim Van Baak 2020-04-25 19:02:22 -07:00
parent 7f76db7af7
commit 9f785b1f4e
2 changed files with 42 additions and 56 deletions

View File

@ -12,9 +12,11 @@ from flask import (
Markup) Markup)
from flask_login import current_user from flask_login import current_user
from amanuensis.errors import MissingConfigError
from amanuensis.lexicon import ( from amanuensis.lexicon import (
get_player_characters,
get_player_drafts,
attempt_publish) attempt_publish)
from amanuensis.models import LexiconModel
from amanuensis.parser import ( from amanuensis.parser import (
parse_raw_markdown, parse_raw_markdown,
PreviewHtmlRenderer, PreviewHtmlRenderer,
@ -184,68 +186,41 @@ def review(name):
@lexicon_param @lexicon_param
@player_required @player_required
def editor(name): def editor(name):
""" lexicon: LexiconModel = g.lexicon
cases:
- neither cid nor aid: load all chars and articles
- cid: list articles just for cid
- aid:
"""
cid = request.args.get('cid')
if not cid:
# Character not specified, load all characters and articles
# and return render_template
characters = [
char for char in g.lexicon.cfg.character.values()
if char.player == current_user.uid
]
articles = [
article for article in g.lexicon.get_drafts_for_player(uid=current_user.uid)
if any([article.character == char.cid for char in characters])
]
return render_template(
'session.editor.jinja',
characters=characters,
articles=articles,
jsonfmt=jsonfmt)
character = g.lexicon.cfg.character.get(cid)
if not character:
# Character was specified, but id was invalid
flash("Character not found")
return redirect(url_for('session.session', name=name))
if character.player != current_user.uid:
# Player doesn't control this character
flash("Access forbidden")
return redirect(url_for('session.session', name=name))
aid = request.args.get('aid') aid = request.args.get('aid')
if not aid: if aid:
# Character specified but not article, load character articles # Article specfied, load editor in edit mode
# and retuen r_t article_fn = None
articles = [ for filename in lexicon.ctx.draft.ls():
article for article in g.lexicon.get_drafts_for_player(uid=current_user.uid) if filename.endswith(f'{aid}.json'):
if article.character == character.cid article_fn = filename
] break
return render_template( if not article_fn:
'session.editor.jinja',
character=character,
articles=articles,
jsonfmt=jsonfmt)
filename = f'{cid}.{aid}'
try:
with g.lexicon.ctx.draft.read(filename) as a:
article = a
except MissingConfigError:
flash("Draft not found") flash("Draft not found")
return redirect(url_for('session.session', name=name)) return redirect(url_for('session.session', name=name))
with lexicon.ctx.draft.read(article_fn) as a:
article = a
# Check that the player owns this article
character = lexicon.cfg.character.get(article.character)
if character.player != current_user.uid:
flash("Access forbidden")
return redirect(url_for('session.session', name=name))
return render_template( return render_template(
'session.editor.jinja', 'session.editor.jinja',
character=character, character=character,
article=article, article=article,
jsonfmt=jsonfmt) jsonfmt=jsonfmt)
# Article not specified, load editor in load mode
characters = list(get_player_characters(lexicon, current_user.uid))
articles = list(get_player_drafts(lexicon, current_user.uid))
print(characters)
print(articles)
return render_template(
'session.editor.jinja',
characters=characters,
articles=articles)
@bp_session.route('/editor/new', methods=['GET']) @bp_session.route('/editor/new', methods=['GET'])
@lexicon_param @lexicon_param

View File

@ -30,22 +30,29 @@
<div id="wrapper"> <div id="wrapper">
<div id="editor-left" class="column"> <div id="editor-left" class="column">
<div class="contentblock"> <div class="contentblock">
{# Thin header bar #}
<div id="editor-header"> <div id="editor-header">
{# Header always includes backlink to lexicon #}
<a href="{{ url_for('session.session', name=g.lexicon.cfg.name) }}"> <a href="{{ url_for('session.session', name=g.lexicon.cfg.name) }}">
{{ g.lexicon.title }} {{ g.lexicon.title }}
</a> </a>
{# If article is not finalized, show button to submit and retract #}
{% if article and not article.status.approved %} {% if article and not article.status.approved %}
<button id="button-submit" onclick="submitArticle()">Submit article</button> <button id="button-submit" onclick="submitArticle()">Submit article</button>
{% endif %} {% endif %}
{# Header always includes character/player info #}
<span> <span>
<b> <b>
{% if character %} {% if character %}
{{ character.name }} / {{ character.name }} /
{% endif %} {% endif %}
{{ current_user.username }} {{ current_user.cfg.username }}
</b> </b>
</span> </span>
</div> </div>
{# In load mode, `characters` is specified and `article` is #}
{# not, and the main body of the editor column contains a #}
{# list of articles that can be loaded. #}
{% for char in characters %} {% for char in characters %}
<div id="editor-charselect"> <div id="editor-charselect">
<b>{{ char.name }}</b> <b>{{ char.name }}</b>
@ -53,7 +60,9 @@
{% for article in articles %} {% for article in articles %}
{% if article.character == char.cid %} {% if article.character == char.cid %}
<li> <li>
<a href="{{ url_for('session.editor', name=g.lexicon.cfg.name, cid=char.cid, aid=article.aid) }}">{{ article.title if article.title.strip() else "Untitled" }}</a> <a href="{{ url_for('session.editor', name=g.lexicon.cfg.name, aid=article.aid) }}">
{{ article.title if article.title.strip() else "Untitled" }}
</a>
<span> <span>
{% if not article.status.ready %} {% if not article.status.ready %}
[Draft] [Draft]
@ -74,6 +83,8 @@
</ul> </ul>
</div> </div>
{% endfor %} {% endfor %}
{# In edit mode, `article` is specified and `characters` is #}
{# not, and the editor pane contains the article editor. #}
{% if article %} {% if article %}
<div id="editor-buttons"> <div id="editor-buttons">
Character literals: Character literals: