From 5c244855ebb59e985f8f7e849cc79209473626c7 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 20 Jan 2020 07:52:41 -0800 Subject: [PATCH] Add lexicon listings to home page --- amanuensis/lexicon/__init__.py | 10 +++++++++- amanuensis/lexicon/manage.py | 2 +- amanuensis/resources/lexicon.json | 1 + amanuensis/resources/page.css | 22 ++++++++++++++++++++++ amanuensis/server/__init__.py | 4 ++++ amanuensis/server/lexicon.py | 22 ++++++++++++++++++++++ amanuensis/templates/home/home.html | 23 ++++++++++++++++++++++- amanuensis/user.py | 9 +++++++++ 8 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 amanuensis/server/lexicon.py diff --git a/amanuensis/lexicon/__init__.py b/amanuensis/lexicon/__init__.py index 30eed8d..4e8fdf6 100644 --- a/amanuensis/lexicon/__init__.py +++ b/amanuensis/lexicon/__init__.py @@ -46,4 +46,12 @@ class LexiconModel(): def log(self, message): now = int(time.time()) with config.json_rw(self.config_path) as j: - j['log'].append([now, message]) \ No newline at end of file + j['log'].append([now, message]) + + def status(self): + if self.turn.current is None: + return "unstarted" + elif self.turn.current > lex.turn.max: + return "completed" + else: + return "ongoing" \ No newline at end of file diff --git a/amanuensis/lexicon/manage.py b/amanuensis/lexicon/manage.py index 00c9ad8..31b21d1 100644 --- a/amanuensis/lexicon/manage.py +++ b/amanuensis/lexicon/manage.py @@ -92,7 +92,7 @@ def get_all_lexicons(): lids = list(index.values()) # Load all of the lexicons - lexes = map(lambda id: lexicon.LexiconModel.by(lid=id), lids) + lexes = list(map(lambda id: lexicon.LexiconModel.by(lid=id), lids)) return lexes diff --git a/amanuensis/resources/lexicon.json b/amanuensis/resources/lexicon.json index b1910f8..fb76eb5 100644 --- a/amanuensis/resources/lexicon.json +++ b/amanuensis/resources/lexicon.json @@ -1,6 +1,7 @@ { "lid": null, "name": null, + "title": null, "editor": null, "prompt": null, "time": { diff --git a/amanuensis/resources/page.css b/amanuensis/resources/page.css index d5f5fdb..cf180b6 100644 --- a/amanuensis/resources/page.css +++ b/amanuensis/resources/page.css @@ -97,6 +97,28 @@ textarea#configText { width: 100%; box-sizing: border-box; } +div.dashboard-lexicon-item { + margin: 0 10px; + padding: 0 10px; + border-left: 3px solid black; +} +div.dashboard-lexicon-unstarted { + border-left-color: blue; +} +div.dashboard-lexicon-ongoing { + border-left-color: goldenrod; +} +div.dashboard-lexicon-completed { + border-left-color: green; +} +div.dashboard-lexicon-item span.dashboard-lexicon-item-title { + font-size: 1.5em; + font-weight: bold; +} +div.dashboard-lexicon-item p { + margin-block-start: 0.5em; + margin-block-end: 0.5em; +} @media only screen and (max-width: 816px) { div#wrapper { padding: 5px; diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index 026cd47..aad1413 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -6,6 +6,7 @@ from flask_login import LoginManager import config from server.auth import get_bp as get_auth_bp from server.home import get_bp as get_home_bp +from server.lexicon import get_bp as get_lex_bp # Flask app init static_root = os.path.abspath(config.get("static_root")) @@ -22,3 +23,6 @@ app.register_blueprint(auth_bp) home_bp = get_home_bp() app.register_blueprint(home_bp) + +lex_bp = get_lex_bp() +app.register_blueprint(lex_bp) \ No newline at end of file diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py new file mode 100644 index 0000000..9b6d75d --- /dev/null +++ b/amanuensis/server/lexicon.py @@ -0,0 +1,22 @@ +import json + +from flask import Blueprint, render_template, url_for, redirect +from flask_login import login_required, current_user +# from flask_wtf import FlaskForm +# from wtforms import TextAreaField, SubmitField, StringField + +import config +import user +import lexicon + + +def get_bp(): + """Create a blueprint for lexicon pages""" + bp = Blueprint('lexicon', __name__, url_prefix='/lexicon/') + + @bp.route('/session/', methods=['GET']) + @login_required + def session(name): + return "Lexicon " + str(name) + + return bp diff --git a/amanuensis/templates/home/home.html b/amanuensis/templates/home/home.html index 7969784..4ab4083 100644 --- a/amanuensis/templates/home/home.html +++ b/amanuensis/templates/home/home.html @@ -12,6 +12,27 @@ {% endif %} {% block main %} -

Home Page

+ +

Open games

+ +{% set lexicons = current_user.lexicons_in() %} +

Your games

+{% if lexicons %} +{% for lexicon in lexicons %} + +
+

+ + Lexicon {{ lexicon.name }} + + [{{ lexicon.status().capitalize() }}] +

+

{{ lexicon.prompt }}

+
+ +{% endfor %} +{% else %} +

You haven't joined a game yet.

+{% endif %} {% endblock %} {% set template_content_blocks = [self.main()] %} \ No newline at end of file diff --git a/amanuensis/user.py b/amanuensis/user.py index 24dbebd..55d2a86 100644 --- a/amanuensis/user.py +++ b/amanuensis/user.py @@ -9,6 +9,7 @@ from werkzeug.security import generate_password_hash, check_password_hash from errors import InternalMisuseError, MissingConfigError, IndexMismatchError import config import resources +import lexicon.manage class UserModel(UserMixin): @staticmethod @@ -56,6 +57,14 @@ class UserModel(UserMixin): with config.json_ro(self.config_path) as j: return check_password_hash(j['password'], pw) + def lexicons_in(self): + return [ + lex + for lex in lexicon.manage.get_all_lexicons() + if self.id in lex.join.joined + ] + + def valid_username(username): return re.match(r"^[A-Za-z0-9-_]{3,}$", username) is not None