From 3cfc01a9c84464270b220cd680f1ea084b52a3a4 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 16 Jun 2021 20:15:30 -0700 Subject: [PATCH] Get home screen working --- amanuensis/server/__init__.py | 7 ++++ amanuensis/server/auth/__init__.py | 4 +-- amanuensis/server/home/__init__.py | 28 +++------------- amanuensis/server/home/home.admin.jinja | 2 +- amanuensis/server/home/home.root.jinja | 17 +++++++--- amanuensis/server/macros.jinja | 44 +++++++++++++------------ 6 files changed, 49 insertions(+), 53 deletions(-) diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index cf97468..2f73815 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -4,6 +4,8 @@ import os from flask import Flask, g +import amanuensis.backend.lexicon +import amanuensis.backend.user from amanuensis.config import AmanuensisConfig, CommandLineConfig from amanuensis.db import DbContext import amanuensis.server.auth as auth @@ -58,6 +60,11 @@ def get_app( app.template_filter("date")(date_format) + def include_backend(): + return {"db": db, "lexiq": amanuensis.backend.lexicon, "userq": amanuensis.backend.user} + + app.context_processor(include_backend) + # Set up Flask-Login auth.get_login_manager().init_app(app) diff --git a/amanuensis/server/auth/__init__.py b/amanuensis/server/auth/__init__.py index 971ba1f..f8fc748 100644 --- a/amanuensis/server/auth/__init__.py +++ b/amanuensis/server/auth/__init__.py @@ -69,11 +69,11 @@ def login(): login_user(user, remember=remember_me) userq.update_logged_in(g.db, username) LOG.info("Logged in user {0.username} ({0.id})".format(user)) - return redirect(url_for("home.admin")) + return redirect(url_for("home.home")) @bp.get("/logout/") @login_required def logout(): logout_user() - return redirect(url_for("home.admin")) + return redirect(url_for("home.home")) diff --git a/amanuensis/server/home/__init__.py b/amanuensis/server/home/__init__.py index 162efa5..f619b3f 100644 --- a/amanuensis/server/home/__init__.py +++ b/amanuensis/server/home/__init__.py @@ -1,43 +1,23 @@ from flask import Blueprint, render_template, g -# from flask import Blueprint, render_template, redirect, url_for, current_app -# from flask_login import login_required, current_user - import amanuensis.backend.user as userq import amanuensis.backend.lexicon as lexiq -# 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.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.get("/") +def home(): + return render_template('home.root.jinja') @bp.get("/admin/") # @login_required # @admin_required def admin(): - return render_template("home.admin.jinja", db=g.db, userq=userq, lexiq=lexiq) + return render_template("home.admin.jinja", userq=userq, lexiq=lexiq) # @bp_home.route("/admin/create/", methods=['GET', 'POST']) diff --git a/amanuensis/server/home/home.admin.jinja b/amanuensis/server/home/home.admin.jinja index 025f947..20ff0f2 100644 --- a/amanuensis/server/home/home.admin.jinja +++ b/amanuensis/server/home/home.admin.jinja @@ -4,7 +4,7 @@ {% block header %}

Amanuensis - Admin Dashboard

{% endblock %} {# TODO #} -{% block sb_home %}Home{% endblock %} +{% block sb_home %}Home{% endblock %} {% block sb_create %}Create a lexicon{% endblock %} {% set template_sidebar_rows = [self.sb_home(), self.sb_create()] %} diff --git a/amanuensis/server/home/home.root.jinja b/amanuensis/server/home/home.root.jinja index 2c68487..83d7f29 100644 --- a/amanuensis/server/home/home.root.jinja +++ b/amanuensis/server/home/home.root.jinja @@ -11,10 +11,16 @@ {{ message }}
{% endfor %} +{% if current_user.is_authenticated %} +{% set joined = lexiq.get_joined(db, current_user.id)|list %} +{% else %} +{% set joined = [] %} +{% endif %} + {% if current_user.is_authenticated %}

Your games

-{% if user_lexicons %} -{% for lexicon in user_lexicons %} +{% if joined %} +{% for lexicon in joined %} {{ macros.dashboard_lexicon_item(lexicon) }} {% endfor %} {% else %} @@ -22,9 +28,10 @@ {% endif %} {% endif %} +{% set public = lexiq.get_public(db)|reject("in", joined)|list %}

Public games

-{% if public_lexicons %} -{% for lexicon in public_lexicons %} +{% if public %} +{% for lexicon in public %} {{ macros.dashboard_lexicon_item(lexicon) }} {% endfor %} {% else %} @@ -34,7 +41,7 @@ {% endblock %} {% set template_content_blocks = [self.main()] %} -{% if current_user.cfg.is_admin %} +{% if current_user.is_site_admin %} {% block admin_dash %} Admin dashboard {% endblock %} diff --git a/amanuensis/server/macros.jinja b/amanuensis/server/macros.jinja index 71e9e2a..c2e507b 100644 --- a/amanuensis/server/macros.jinja +++ b/amanuensis/server/macros.jinja @@ -3,33 +3,35 @@

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

{{ lexicon.prompt }}

{% if current_user.is_authenticated %}

- {# TODO #} - {# {% - if current_user.uid in lexicon.cfg.join.joined + {#- + Show detailed player information if the current user is a member of the lexicon or if the current user is a site admin. The filter sequence must be converted to a list because it returns a generator, which is truthy. + -#} + {%- + if lexicon.memberships|map(attribute="user_id")|select("equalto", current_user.id)|list or current_user.is_site_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 %} #} + -%} + Editor: {{ + lexicon.memberships|selectattr("is_editor")|map(attribute="user")|map(attribute="username")|join(", ") + }} / Players: {{ + lexicon.memberships|map(attribute="user")|map(attribute="username")|join(", ") + }} ({{ lexicon.memberships|count }} + {%- if lexicon.player_limit is not none -%} + /{{ lexicon.player_limit }} + {%- endif -%}) + {%- else -%} + Players: {{ lexicon.memberships|count }}{% if lexicon.player_limit is not none %} / {{ lexicon.player_limit }}{% endif -%} + {%- + if lexicon.public and lexicon.joinable + %} / Join game + {%- endif -%} + {%- endif -%}

{% endif %}