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 %}