Simplify player membership checks

This commit is contained in:
Tim Van Baak 2020-01-31 13:58:39 -08:00
parent f6947271a1
commit 81c18b5c7c
8 changed files with 39 additions and 18 deletions

View File

@ -134,7 +134,7 @@ def command_player_add(args):
from amanuensis.lexicon.manage import add_player
# Verify arguments
if args.user.id in args.lexicon.join.joined:
if args.user.in_lexicon(args.lexicon):
logger.error('"{0.username}" is already a player in "{1.name}"'.format(
args.user, args.lexicon))
return -1
@ -163,7 +163,7 @@ def command_player_remove(args):
from amanuensis.lexicon.manage import remove_player
# Verify arguments
if args.user.id not in args.lexicon.join.joined:
if not args.user.in_lexicon(args.lexicon):
logger.error('"{0.username}" is not a player in lexicon "{1.name}"'
''.format(args.user, args.lexicon))
return -1
@ -218,7 +218,7 @@ def command_char_create(args):
from amanuensis.user import UserModel
# Verify arguments
if args.user.id not in args.lexicon.join.joined:
if not args.user.in_lexicon(args.lexicon):
logger.error('"{0.username}" is not a player in lexicon "{1.name}"'
''.format(args.user, args.lexicon))
return -1

View File

@ -125,7 +125,7 @@ def get_user_lexicons(user):
return [
lexicon
for lexicon in get_all_lexicons()
if user.id in lexicon.join.joined]
if user.in_lexicon(lexicon)]
def valid_add(lex, player, password=None):

View File

@ -8,6 +8,7 @@ from amanuensis.server.auth import get_bp as get_auth_bp
from amanuensis.server.home import get_bp as get_home_bp
from amanuensis.server.helpers import register_custom_filters
from amanuensis.server.lexicon import get_bp as get_lex_bp
from amanuensis.user import AnonymousUserModel
# Flask app init
static_root = os.path.abspath(get("static_root"))
@ -23,6 +24,7 @@ register_custom_filters(app)
# Flask-Login init
login = LoginManager(app)
login.login_view = 'auth.login'
login.anonymous_user = AnonymousUserModel
# Blueprint inits
auth_bp = get_auth_bp(login)

View File

@ -59,7 +59,7 @@ def player_required(route):
"""
@wraps(route)
def player_route(*args, **kwargs):
if current_user.id not in g.lexicon.join.joined:
if not current_user.in_lexicon(g.lexicon):
flash("You must be a player to view this page")
return (redirect(url_for('lexicon.contents', name=g.lexicon.name))
if g.lexicon.join.public
@ -76,7 +76,7 @@ def player_required_if_not_public(route):
@wraps(route)
def player_route(*args, **kwargs):
if ((not g.lexicon.join.public)
and current_user.id not in g.lexicon.join.joined):
and not current_user.in_lexicon(g.lexicon)):
flash("You must be a player to view this page")
return redirect(url_for('home.home'))
return route(*args, **kwargs)

View File

@ -1,5 +1,5 @@
{% extends "page_2col.html" %}
{% import 'macros.html' as macros %}
{% import 'macros.html' as macros with context %}
{% block title %}Admin | Amanuensis{% endblock %}
{% block header %}<h2>Amanuensis - Admin Dashboard</h2>{% endblock %}

View File

@ -1,5 +1,5 @@
{% extends "page_1col.html" %}
{% import 'macros.html' as macros %}
{% import 'macros.html' as macros with context %}
{% block title %}Home | Amanuensis{% endblock %}
{% block header %}<h2>Amanuensis - Home</h2>{% endblock %}

View File

@ -1,4 +1,4 @@
{% macro dashboard_lexicon_item(lexicon, anonymize) %}
{% macro dashboard_lexicon_item(lexicon) %}
<div class="dashboard-lexicon-item dashboard-lexicon-{{ lexicon.status() }}">
<p>
<span class="dashboard-lexicon-item-title">
@ -8,19 +8,28 @@
[{{ lexicon.status().capitalize() }}]
</p>
<p><i>{{ lexicon.prompt }}</i></p>
{% if current_user.is_authenticated %}
<p>
{% if anonymize %}
Players: {{ lexicon.join.joined|count }}/{{ lexicon.join.max_players }}
{% else %}
Editor:
{{ lexicon.editor|user_attr('username') }} / Players:
{% for uid in lexicon.join.joined[:-1] %}
{{ uid|user_attr('username') }},
{%
if current_user.in_lexicon(lexicon)
or current_user.is_admin
%}
Editor: {{ lexicon.editor|user_attr('username') }} /
Players:
{% for uid in lexicon.join.joined %}
{{ uid|user_attr('username') }}{% if not loop.last %}, {% endif %}
{% endfor %}
{{ lexicon.join.joined[-1]|user_attr('username') }}
({{ lexicon.join.joined|count }}/{{ lexicon.join.max_players }})
{% else %}
Players: {{ lexicon.join.joined|count }}/{{ lexicon.join.max_players }}
{% if lexicon.join.public and lexicon.join.open %}
/ <a href="{{ url_for('lexicon.join', name=lexicon.name) }}">
Join game
</a>
{% endif %}
{% endif %}
</p>
{% endif %}
</div>
{% endmacro %}

View File

@ -3,7 +3,7 @@ import re
import time
import uuid
from flask_login import UserMixin
from flask_login import UserMixin, AnonymousUserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from amanuensis.errors import (
@ -64,6 +64,16 @@ class UserModel(UserMixin):
with json_ro(self.config_path) as j:
return check_password_hash(j['password'], pw)
def in_lexicon(self, lexicon):
return self.id in lexicon.join.joined
class AnonymousUserModel(AnonymousUserMixin):
is_admin = False
def in_lexicon(self, lexicon):
return False
def valid_username(username):
"""