added form to lexicon creation page

This commit is contained in:
Nikolai 2021-09-23 19:53:27 -07:00
parent 0d022af335
commit 5b3e5663b7
5 changed files with 60 additions and 30 deletions

View File

@ -181,7 +181,7 @@ class Lexicon(ModelBase):
################################ ################################
# Whether players can join the game # 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 # Whether the game is listed on public pages
public = Column(Boolean, nullable=False, default=False) public = Column(Boolean, nullable=False, default=False)

View File

@ -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=".") bp = Blueprint("home", __name__, url_prefix="/home", template_folder=".")
@ -18,6 +22,34 @@ def home():
def admin(): def admin():
return render_template("home.admin.jinja", userq=userq, lexiq=lexiq) 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']) # @bp_home.route("/admin/create/", methods=['GET', 'POST'])
# @login_required # @login_required

View File

@ -2,16 +2,13 @@ from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField from wtforms import StringField, SubmitField, TextAreaField
from wtforms.validators import DataRequired from wtforms.validators import DataRequired
# from amanuensis.server.forms import User, Lexicon class LexiconCreateForm(FlaskForm):
"""/admin/create/"""
lexicon = StringField(
# class LexiconCreateForm(FlaskForm): 'Lexicon name',
# """/admin/create/""" validators=[DataRequired()])
# lexiconName = StringField( editor = StringField(
# 'Lexicon name', 'Username of editor',
# validators=[DataRequired(), Lexicon(should_exist=False)]) validators=[DataRequired()])
# editorName = StringField( prompt = TextAreaField('Prompt')
# 'Username of editor', submit = SubmitField('Create')
# validators=[DataRequired(), User(should_exist=True)])
# promptText = TextAreaField('Prompt')
# submit = SubmitField('Create')

View File

@ -3,9 +3,8 @@
{% block title %}Admin | Amanuensis{% endblock %} {% block title %}Admin | Amanuensis{% endblock %}
{% block header %}<h2>Amanuensis - Admin Dashboard</h2>{% endblock %} {% block header %}<h2>Amanuensis - Admin Dashboard</h2>{% endblock %}
{# TODO #}
{% block sb_home %}<a href="{{ url_for('home.home') }}">Home</a>{% endblock %} {% block sb_home %}<a href="{{ url_for('home.home') }}">Home</a>{% endblock %}
{% block sb_create %}<a href="#{#{ url_for('home.admin_create') }#}">Create a lexicon</a>{% endblock %} {% block sb_create %}<a href="{{ url_for('home.create') }}">Create a lexicon</a>{% endblock %}
{% set template_sidebar_rows = [self.sb_home(), self.sb_create()] %} {% set template_sidebar_rows = [self.sb_home(), self.sb_create()] %}
{% block main %} {% block main %}

View File

@ -8,22 +8,24 @@
{% set template_sidebar_rows = [self.sb_home(), self.sb_admin()] %} {% set template_sidebar_rows = [self.sb_home(), self.sb_admin()] %}
{% block main %} {% block main %}
<form action="" method="post" novalidate> <form action="" method="post" novalidate>
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
<p>{{ form.lexiconName.label }}<br>{{ form.lexiconName(size=32) }} <p>{{ form.lexicon.label }}<br>{{ form.lexicon(size=32) }}
{% for error in form.lexiconName.errors %} {% for error in form.lexicon.errors %}
<br><span style="color: #ff0000">{{ error }}</span> <br><span style="color: #ff0000">{{ error }}</span>
{% endfor %}</p> {% endfor %}</p>
<p>{{ form.editorName.label }}<br>{{ form.editorName(size=32) }} <p>{{ form.editor.label }}<br>{{ form.editor(size=32) }}
{% for error in form.editorName.errors %} {% for error in form.editor.errors %}
<br><span style="color: #ff0000">{{ error }}</span> <br><span style="color: #ff0000">{{ error }}</span>
{% endfor %}</p> {% endfor %}</p>
<p>{{ form.promptText.label }}<br>{{ form.promptText(class_="fullwidth") }}</p> <p>{{ form.prompt.label }}<br>{{ form.prompt(class_="fullwidth") }}</p>
<p>{{ form.submit() }}</p> <p>{{ form.submit() }}</p>
</form> </form>
{% for message in get_flashed_messages() %} {% for message in get_flashed_messages() %}
<span style="color: #ff0000">{{ message }}</span><br> <span style="color: #ff0000">{{ message }}</span><br>
{% endfor %} {% endfor %}