diff --git a/amanuensis/db/models.py b/amanuensis/db/models.py index 471e05a..0b1642f 100644 --- a/amanuensis/db/models.py +++ b/amanuensis/db/models.py @@ -13,7 +13,6 @@ from sqlalchemy import ( ForeignKey, Integer, String, - Table, Text, text, TypeDecorator, @@ -234,6 +233,14 @@ class Lexicon(ModelBase): content_rules = relationship("ArticleContentRule", back_populates="lexicon") posts = relationship("Post", back_populates="lexicon") + ####################### + # Derived information # + ####################### + + @property + def full_title(self: "Lexicon") -> str: + return self.title if self.title else f"Lexicon {self.name}" + class Membership(ModelBase): """ diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index 0231d89..e144471 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -5,6 +5,7 @@ from flask import Flask, g from amanuensis.config import AmanuensisConfig, CommandLineConfig from amanuensis.db import DbContext +import amanuensis.server.home def get_app( @@ -34,11 +35,13 @@ def get_app( # Make the database connection available to requests via g def db_setup(): g.db = db + app.before_request(db_setup) # Tear down the session on request teardown def db_teardown(response_or_exc): db.session.remove() + app.teardown_appcontext(db_teardown) # Configure jinja options @@ -48,10 +51,11 @@ def get_app( # TODO # Register blueprints - # TODO + app.register_blueprint(amanuensis.server.home.bp) def test(): return "Hello, world!" + app.route("/")(test) return app diff --git a/amanuensis/server/home/__init__.py b/amanuensis/server/home/__init__.py index 68477ca..162efa5 100644 --- a/amanuensis/server/home/__init__.py +++ b/amanuensis/server/home/__init__.py @@ -1,64 +1,64 @@ -from flask import Blueprint, render_template, redirect, url_for, current_app -from flask_login import login_required, current_user +from flask import Blueprint, render_template, g -from amanuensis.config import RootConfigDirectoryContext -from amanuensis.lexicon import create_lexicon, load_all_lexicons -from amanuensis.models import UserModel, ModelFactory -from amanuensis.server.helpers import admin_required +# from flask import Blueprint, render_template, redirect, url_for, current_app +# from flask_login import login_required, current_user -from .forms import LexiconCreateForm +import amanuensis.backend.user as userq +import amanuensis.backend.lexicon as lexiq -bp_home = Blueprint('home', __name__, - url_prefix='/home', - template_folder='.') +# from amanuensis.config import RootConfigDirectoryContext +# from amanuensis.lexicon import create_lexicon, load_all_lexicons +# from amanuensis.models import UserModel, ModelFactory +# from amanuensis.server.helpers import admin_required + +# from .forms import LexiconCreateForm + +bp = Blueprint("home", __name__, url_prefix="/home", template_folder=".") -@bp_home.route('/', methods=['GET']) -def home(): - root: RootConfigDirectoryContext = current_app.config['root'] - user: UserModel = current_user - user_lexicons = [] - public_lexicons = [] - for lexicon in load_all_lexicons(root): - if user.uid in lexicon.cfg.join.joined: - user_lexicons.append(lexicon) - elif lexicon.cfg.join.public: - public_lexicons.append(lexicon) - return render_template( - 'home.root.jinja', - user_lexicons=user_lexicons, - public_lexicons=public_lexicons) +# @bp.get("/") +# def home(): +# Show lexicons that are visible to the current user +# return "TODO" +# user_lexicons = [] +# public_lexicons = [] +# for lexicon in load_all_lexicons(root): +# if user.uid in lexicon.cfg.join.joined: +# user_lexicons.append(lexicon) +# elif lexicon.cfg.join.public: +# public_lexicons.append(lexicon) +# return render_template( +# 'home.root.jinja', +# user_lexicons=user_lexicons, +# public_lexicons=public_lexicons) -@bp_home.route('/admin/', methods=['GET']) -@login_required -@admin_required +@bp.get("/admin/") +# @login_required +# @admin_required def admin(): - root: RootConfigDirectoryContext = current_app.config['root'] - users = [] - lexicons = list(load_all_lexicons(root)) - return render_template('home.admin.jinja', users=users, lexicons=lexicons) + return render_template("home.admin.jinja", db=g.db, userq=userq, lexiq=lexiq) -@bp_home.route("/admin/create/", methods=['GET', 'POST']) -@login_required -@admin_required -def admin_create(): - form = LexiconCreateForm() +# @bp_home.route("/admin/create/", methods=['GET', 'POST']) +# @login_required +# @admin_required +# def admin_create(): +# form = LexiconCreateForm() - if not form.validate_on_submit(): - # GET or POST with invalid form data - return render_template('home.create.jinja', form=form) +# if not form.validate_on_submit(): +# # GET or POST with invalid form data +# return render_template('home.create.jinja', form=form) - # POST with valid data - root: RootConfigDirectoryContext = current_app.config['root'] - model_factory: ModelFactory = current_app.config['model_factory'] - lexicon_name = form.lexiconName.data - editor_name = form.editorName.data - prompt = form.promptText.data - # Editor's existence was checked by form validators - editor = model_factory.user(editor_name) - lexicon = create_lexicon(root, lexicon_name, editor) - with lexicon.ctx.edit_config() as cfg: - cfg.prompt = prompt - return redirect(url_for('session.session', name=lexicon_name)) +# # POST with valid data +# root: RootConfigDirectoryContext = current_app.config['root'] +# model_factory: ModelFactory = current_app.config['model_factory'] +# lexicon_name = form.lexiconName.data +# editor_name = form.editorName.data +# prompt = form.promptText.data +# # Editor's existence was checked by form validators +# editor = model_factory.user(editor_name) +# lexicon = create_lexicon(root, lexicon_name, editor) +# with lexicon.ctx.edit_config() as cfg: +# cfg.prompt = prompt +# return redirect(url_for('session.session', name=lexicon_name)) diff --git a/amanuensis/server/home/home.admin.jinja b/amanuensis/server/home/home.admin.jinja index 2b8ecff..854f4a7 100644 --- a/amanuensis/server/home/home.admin.jinja +++ b/amanuensis/server/home/home.admin.jinja @@ -3,17 +3,18 @@ {% block title %}Admin | Amanuensis{% endblock %} {% block header %}

Amanuensis - Admin Dashboard

{% endblock %} -{% block sb_home %}Home{% endblock %} -{% block sb_create %}Create a lexicon{% endblock %} +{# TODO #} +{% block sb_home %}Home{% endblock %} +{% block sb_create %}Create a lexicon{% endblock %} {% set template_sidebar_rows = [self.sb_home(), self.sb_create()] %} {% block main %}

Users:

-{% for user in users %} +{% for user in userq.get_all_users(db) %} {{ macros.dashboard_user_item(user) }} {% endfor %}

Lexicons:

-{% for lexicon in lexicons %} +{% for lexicon in lexiq.get_all_lexicons(db) %} {{ macros.dashboard_lexicon_item(lexicon) }} {% endfor %} {% endblock %} diff --git a/amanuensis/server/macros.jinja b/amanuensis/server/macros.jinja index 9cbf0b0..7909f7b 100644 --- a/amanuensis/server/macros.jinja +++ b/amanuensis/server/macros.jinja @@ -1,45 +1,47 @@ {% macro dashboard_lexicon_item(lexicon) %} -
-

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

-

{{ lexicon.cfg.prompt }}

- {% if current_user.is_authenticated %} -

- {% - if current_user.uid in lexicon.cfg.join.joined - or current_user.cfg.is_admin - %} - Editor: {{ lexicon.cfg.editor|user_attr('username') }} / - Players: - {% for uid in lexicon.cfg.join.joined %} - {{ uid|user_attr('username') }}{% if not loop.last %}, {% endif %} - {% endfor %} - ({{ lexicon.cfg.join.joined|count }}/{{ lexicon.cfg.join.max_players }}) - {% else %} - Players: {{ lexicon.cfg.join.joined|count }}/{{ lexicon.cfg.join.max_players }} - {% if lexicon.cfg.join.public and lexicon.cfg.join.open %} - / - Join game - - {% endif %} - {% endif %} -

- {% endif %} +{% set status = "completed" if lexicon.completed else "ongoing" if lexicon.started else "unstarted" %} +
+

+ + + {{ lexicon.full_title }} + + [{{ lexicon.status.capitalize() }}] +

+

{{ lexicon.prompt }}

+ {# {% if current_user.is_authenticated %} #} +

+ {# TODO #} + {# {% + if current_user.uid in lexicon.cfg.join.joined + or current_user.cfg.is_admin + %} #} + Editor: {#{ lexicon.cfg.editor|user_attr('username') }#} / + Players: + {# {% for uid in lexicon.cfg.join.joined %} #} + {# {{ uid|user_attr('username') }}{% if not loop.last %}, {% endif %} #} + {# {% endfor %} #} + {# ({{ lexicon.cfg.join.joined|count }}/{{ lexicon.cfg.join.max_players }}) #} + {# {% else %} #} + {# Players: {{ lexicon.cfg.join.joined|count }}/{{ lexicon.cfg.join.max_players }} #} + {# {% if lexicon.cfg.join.public and lexicon.cfg.join.open %} #} + {# / #} + {# Join game #} + {# #} + {# {% endif %} #} + {# {% endif %} #} +

+ {# {% endif %} #}
{% endmacro %} {% macro dashboard_user_item(user) %}
-

- {{ user.cfg.username }} - {% if user.cfg.username != user.cfg.displayname %} / {{ user.cfg.displayname }}{% endif %} - ({{user.uid}}) -

-

Last activity: {{ user.cfg.last_activity|asdate }} — Last login: {{ user.cfg.last_login|asdate }}

+

+ {{ user.username }} + {% if user.username != user.display_name %} / {{ user.display_name }}{% endif %} + (id #{{user.id}}) +

+

Last activity: {{ user.last_activity }} — Last login: {{ user.last_login }}

{% endmacro %} \ No newline at end of file diff --git a/amanuensis/server/page.jinja b/amanuensis/server/page.jinja index f58fac8..9e1e362 100644 --- a/amanuensis/server/page.jinja +++ b/amanuensis/server/page.jinja @@ -1,33 +1,34 @@ - - - {% block title %}{% endblock %} - - + + + {% block title %}{% endblock %} + + -
- - {% block sidebar %}{% endblock %} -
- {% if not template_content_blocks %}{% set template_content_blocks = [] %}{% endif %} - {% if not content_blocks %}{% set content_blocks = [] %}{% endif %} - {% for content_block in template_content_blocks + content_blocks %}
- {{ content_block|safe }}
- {% endfor %} -
-
+
+ + {% block sidebar %}{% endblock %} +
+ {% if not template_content_blocks %}{% set template_content_blocks = [] %}{% endif %} + {% if not content_blocks %}{% set content_blocks = [] %}{% endif %} + {% for content_block in template_content_blocks + content_blocks %}
+ {{ content_block|safe }}
+ {% endfor %} +
+
diff --git a/amanuensis/server/page_2col.jinja b/amanuensis/server/page_2col.jinja index df107aa..dac4d32 100644 --- a/amanuensis/server/page_2col.jinja +++ b/amanuensis/server/page_2col.jinja @@ -1,12 +1,12 @@ {% extends "page.jinja" %} {% block sidebar %} {% endblock %} {% block content_class %}content-2col{% endblock %} \ No newline at end of file