Compare commits

...

1 Commits

Author SHA1 Message Date
Nikolai 5b3e5663b7 added form to lexicon creation page 2021-09-23 19:53:27 -07:00
5 changed files with 60 additions and 30 deletions

View File

@ -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)

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=".")
@ -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

View File

@ -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')

View File

@ -3,9 +3,8 @@
{% block title %}Admin | Amanuensis{% endblock %}
{% block header %}<h2>Amanuensis - Admin Dashboard</h2>{% endblock %}
{# TODO #}
{% 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()] %}
{% block main %}

View File

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