From 9f785b1f4ea91405dd4d3b2a4dd034e27aa27936 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sat, 25 Apr 2020 19:02:22 -0700 Subject: [PATCH] Get editor hooked up again --- amanuensis/server/session/__init__.py | 83 +++++++------------ .../server/session/session.editor.jinja | 15 +++- 2 files changed, 42 insertions(+), 56 deletions(-) diff --git a/amanuensis/server/session/__init__.py b/amanuensis/server/session/__init__.py index 8da40ec..60df1bd 100644 --- a/amanuensis/server/session/__init__.py +++ b/amanuensis/server/session/__init__.py @@ -12,9 +12,11 @@ from flask import ( Markup) from flask_login import current_user -from amanuensis.errors import MissingConfigError from amanuensis.lexicon import ( + get_player_characters, + get_player_drafts, attempt_publish) +from amanuensis.models import LexiconModel from amanuensis.parser import ( parse_raw_markdown, PreviewHtmlRenderer, @@ -184,67 +186,40 @@ def review(name): @lexicon_param @player_required def editor(name): - """ - 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)) - + lexicon: LexiconModel = g.lexicon aid = request.args.get('aid') - if not aid: - # Character specified but not article, load character articles - # and retuen r_t - articles = [ - article for article in g.lexicon.get_drafts_for_player(uid=current_user.uid) - if article.character == character.cid - ] + if aid: + # Article specfied, load editor in edit mode + article_fn = None + for filename in lexicon.ctx.draft.ls(): + if filename.endswith(f'{aid}.json'): + article_fn = filename + break + if not article_fn: + flash("Draft not found") + 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( 'session.editor.jinja', character=character, - articles=articles, + article=article, jsonfmt=jsonfmt) - filename = f'{cid}.{aid}' - try: - with g.lexicon.ctx.draft.read(filename) as a: - article = a - except MissingConfigError: - flash("Draft not found") - return redirect(url_for('session.session', name=name)) - + # 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', - character=character, - article=article, - jsonfmt=jsonfmt) + characters=characters, + articles=articles) @bp_session.route('/editor/new', methods=['GET']) diff --git a/amanuensis/server/session/session.editor.jinja b/amanuensis/server/session/session.editor.jinja index 07f663b..57fe23f 100644 --- a/amanuensis/server/session/session.editor.jinja +++ b/amanuensis/server/session/session.editor.jinja @@ -30,22 +30,29 @@
+ {# Thin header bar #}
+ {# Header always includes backlink to lexicon #} {{ g.lexicon.title }} + {# If article is not finalized, show button to submit and retract #} {% if article and not article.status.approved %} {% endif %} + {# Header always includes character/player info #} {% if character %} {{ character.name }} / {% endif %} - {{ current_user.username }} + {{ current_user.cfg.username }}
+ {# 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 %}
{{ char.name }} @@ -53,7 +60,9 @@ {% for article in articles %} {% if article.character == char.cid %}
  • - {{ article.title if article.title.strip() else "Untitled" }} + + {{ article.title if article.title.strip() else "Untitled" }} + {% if not article.status.ready %} [Draft] @@ -74,6 +83,8 @@
  • {% endfor %} + {# In edit mode, `article` is specified and `characters` is #} + {# not, and the editor pane contains the article editor. #} {% if article %}
    Character literals: