diff --git a/amanuensis/db/models.py b/amanuensis/db/models.py index e951309..d410714 100644 --- a/amanuensis/db/models.py +++ b/amanuensis/db/models.py @@ -181,7 +181,7 @@ class Lexicon(ModelBase): ################################ # Whether players can join the game - joinable = Column(Boolean, nullable=False, default=False) + joinable = Column(Boolean, nullable=False, default=True) # Whether the game is listed on public pages public = Column(Boolean, nullable=False, default=False) diff --git a/amanuensis/server/home/__init__.py b/amanuensis/server/home/__init__.py index 6103e88..2b17d7d 100644 --- a/amanuensis/server/home/__init__.py +++ b/amanuensis/server/home/__init__.py @@ -1,8 +1,12 @@ -from flask import Blueprint, render_template, g +from operator import le -from amanuensis.backend import userq, lexiq +from sqlalchemy.sql.expression import true +from amanuensis.db.models import Lexicon +from flask import Blueprint, render_template, g, url_for, redirect -# from .forms import LexiconCreateForm +from amanuensis.backend import userq, lexiq, memq + +from .forms import LexiconCreateForm bp = Blueprint("home", __name__, url_prefix="/home", template_folder=".") @@ -18,6 +22,34 @@ def home(): def admin(): return render_template("home.admin.jinja", userq=userq, lexiq=lexiq) +@bp.get("/admin/create/") +def create(): + form = LexiconCreateForm() + return render_template("home.create.jinja", form=form) + +@bp.post("/admin/create/") +def create_post(): + form = LexiconCreateForm() + if form.validate(): + name = form.lexicon.data + editor = userq.try_from_username(g.db, form.editor.data) + prompt = form.prompt.data + + assert editor is not None + + lexicon = lexiq.create(g.db, name, None, prompt) + new_membership = memq.create(g.db, editor.id, lexicon.id, is_editor=True) + + lexicon.joinable = False + g.db.session.commit() + + return redirect(url_for("home.admin")) + + else: + return render_template("home.create.jinja", form=form) + + + # @bp_home.route("/admin/create/", methods=['GET', 'POST']) # @login_required diff --git a/amanuensis/server/home/forms.py b/amanuensis/server/home/forms.py index 6187ba8..df57777 100644 --- a/amanuensis/server/home/forms.py +++ b/amanuensis/server/home/forms.py @@ -2,16 +2,13 @@ from flask_wtf import FlaskForm from wtforms import StringField, SubmitField, TextAreaField from wtforms.validators import DataRequired -# from amanuensis.server.forms import User, Lexicon - - -# class LexiconCreateForm(FlaskForm): -# """/admin/create/""" -# lexiconName = StringField( -# 'Lexicon name', -# validators=[DataRequired(), Lexicon(should_exist=False)]) -# editorName = StringField( -# 'Username of editor', -# validators=[DataRequired(), User(should_exist=True)]) -# promptText = TextAreaField('Prompt') -# submit = SubmitField('Create') +class LexiconCreateForm(FlaskForm): + """/admin/create/""" + lexicon = StringField( + 'Lexicon name', + validators=[DataRequired()]) + editor = StringField( + 'Username of editor', + validators=[DataRequired()]) + prompt = TextAreaField('Prompt') + submit = SubmitField('Create') diff --git a/amanuensis/server/home/home.admin.jinja b/amanuensis/server/home/home.admin.jinja index 20ff0f2..8d3132d 100644 --- a/amanuensis/server/home/home.admin.jinja +++ b/amanuensis/server/home/home.admin.jinja @@ -3,9 +3,8 @@ {% block title %}Admin | Amanuensis{% endblock %} {% block header %}

Amanuensis - Admin Dashboard

{% endblock %} -{# TODO #} {% block sb_home %}Home{% endblock %} -{% block sb_create %}Create a lexicon{% endblock %} +{% block sb_create %}Create a lexicon{% endblock %} {% set template_sidebar_rows = [self.sb_home(), self.sb_create()] %} {% block main %} diff --git a/amanuensis/server/home/home.create.jinja b/amanuensis/server/home/home.create.jinja index ec504d3..55b72dd 100644 --- a/amanuensis/server/home/home.create.jinja +++ b/amanuensis/server/home/home.create.jinja @@ -8,22 +8,24 @@ {% set template_sidebar_rows = [self.sb_home(), self.sb_admin()] %} {% block main %} +
- {{ form.hidden_tag() }} -

{{ form.lexiconName.label }}
{{ form.lexiconName(size=32) }} - {% for error in form.lexiconName.errors %} -
{{ error }} - {% endfor %}

+ {{ form.hidden_tag() }} +

{{ form.lexicon.label }}
{{ form.lexicon(size=32) }} + {% for error in form.lexicon.errors %} +
{{ error }} + {% endfor %}

-

{{ form.editorName.label }}
{{ form.editorName(size=32) }} - {% for error in form.editorName.errors %} -
{{ error }} - {% endfor %}

+

{{ form.editor.label }}
{{ form.editor(size=32) }} + {% for error in form.editor.errors %} +
{{ error }} + {% endfor %}

-

{{ form.promptText.label }}
{{ form.promptText(class_="fullwidth") }}

+

{{ form.prompt.label }}
{{ form.prompt(class_="fullwidth") }}

-

{{ form.submit() }}

+

{{ form.submit() }}

+ {% for message in get_flashed_messages() %} {{ message }}
{% endfor %}