diff --git a/amanuensis/backend/index.py b/amanuensis/backend/index.py index bfd259c..fc988cc 100644 --- a/amanuensis/backend/index.py +++ b/amanuensis/backend/index.py @@ -3,7 +3,9 @@ Index query interface """ import re -from typing import Optional +from typing import Optional, Sequence + +from sqlalchemy import select from amanuensis.db import DbContext, ArticleIndex, IndexType from amanuensis.errors import ArgumentError, BackendArgumentTypeError @@ -72,3 +74,42 @@ def create( db.session.add(new_index) db.session.commit() return new_index + + +def get_for_lexicon(db: DbContext, lexicon_id: int) -> Sequence[ArticleIndex]: + """Returns all index rules for a lexicon.""" + return db(select(ArticleIndex).where(ArticleIndex.lexicon_id == lexicon_id)).scalars() + +def update(db: DbContext, lexicon_id: int, indices: Sequence[ArticleIndex]) -> None: + """ + Update the indices for a lexicon. Indices are matched by type and pattern. + An extant index not matched to an input is deleted, and an input index not + matched to a an extant index is created. Matched indexes are updated with + the input logical and display orders and capacity. + """ + extant_indices: Sequence[ArticleIndex] = list(get_for_lexicon(db, lexicon_id)) + s = lambda i: f"{i.index_type}:{i.pattern}" + for extant_index in extant_indices: + match = None + for new_index in indices: + is_match = extant_index.index_type == new_index.index_type and extant_index.pattern == new_index.pattern + if is_match: + match = new_index + break + if match: + extant_index.logical_order = new_index.logical_order + extant_index.display_order = new_index.display_order + extant_index.capacity = new_index.capacity + else: + db.session.delete(extant_index) + for new_index in indices: + match = None + for extant_index in extant_indices: + is_match = extant_index.index_type == new_index.index_type and extant_index.pattern == new_index.pattern + if is_match: + match = extant_index + break + if not match: + new_index.lexicon_id = lexicon_id + db.session.add(new_index) + db.session.commit() diff --git a/amanuensis/db/models.py b/amanuensis/db/models.py index c9461a9..e972e31 100644 --- a/amanuensis/db/models.py +++ b/amanuensis/db/models.py @@ -473,6 +473,7 @@ class ArticleIndex(ModelBase): """ __tablename__ = "article_index" + __table_args__ = (UniqueConstraint("lexicon_id", "index_type", "pattern"),) ############## # Index info # diff --git a/amanuensis/resources/page.css b/amanuensis/resources/page.css index c798e74..976f2f8 100644 --- a/amanuensis/resources/page.css +++ b/amanuensis/resources/page.css @@ -46,11 +46,8 @@ div#sidebar { img#logo { max-width: 200px; } -table { - table-layout: fixed; - width: 100%; -} div#sidebar table { + width: 100%; border-collapse: collapse; } div.citeblock table td:first-child + td a { @@ -118,9 +115,6 @@ input.fullwidth { width: 100%; box-sizing: border-box; } -input.smallnumber { - width: 4em; -} form#session-settings p { line-height: 1.8em; } @@ -207,6 +201,20 @@ ul.unordered-tabs li a[href]:hover { background-color: var(--button-hover); border-color: var(--button-hover); } +#index-definition-help { + margin-block-start: 1em; + margin-block-end: 1em; +} +#index-definition-table td:nth-child(2) { + width: 100%; +} +#index-definition-table td:nth-child(2) *:only-child { + box-sizing: border-box; + width: 100%; +} +#index-definition-table td input[type=number] { + width: 4em; +} @media only screen and (max-width: 816px) { div#wrapper { padding: 5px; diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index bb0e0b4..c8a9827 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -75,6 +75,7 @@ def get_app( "userq": userq, "memq": memq, "charq": charq, + "indq": indq, "current_lexicon": current_lexicon, "current_membership": current_membership } diff --git a/amanuensis/server/lexicon/settings/__init__.py b/amanuensis/server/lexicon/settings/__init__.py index 3a161a9..fbf13b2 100644 --- a/amanuensis/server/lexicon/settings/__init__.py +++ b/amanuensis/server/lexicon/settings/__init__.py @@ -1,3 +1,5 @@ +from typing import Sequence + from flask import Blueprint, render_template, url_for, g, flash, redirect from amanuensis.backend import * @@ -10,7 +12,7 @@ from amanuensis.server.helpers import ( current_lexicon, ) -from .forms import PlayerSettingsForm, SetupSettingsForm +from .forms import PlayerSettingsForm, SetupSettingsForm, IndexSchemaForm bp = Blueprint("settings", __name__, url_prefix="/settings", template_folder=".") @@ -118,15 +120,56 @@ def setup(lexicon_name): ) -@bp.get("/progress/") +@bp.get("/index/") @lexicon_param @editor_required -def progress(lexicon_name): +def index(lexicon_name): + # Get the current indices + indices: Sequence[ArticleIndex] = indq.get_for_lexicon(g.db, current_lexicon.id) + index_data = [ + { + "index_type": str(index.index_type), + "pattern": index.pattern, + "logical_order": index.logical_order, + "display_order": index.display_order, + "capacity": index.capacity, + } + for index in indices + ] + # Add a blank index to allow for adding rules + index_data.append({ + "index_type": "", + "pattern": None, + "logical_order": None, + "display_order": None, + "capacity": None, + }) + form = IndexSchemaForm(indices=index_data) return render_template( - "settings.jinja", lexicon_name=lexicon_name, page_name=progress.__name__ + "settings.jinja", lexicon_name=lexicon_name, page_name=index.__name__, form=form ) +@bp.post("/index/") +@lexicon_param +@editor_required +def index_post(lexicon_name): + # Initialize the form + form = IndexSchemaForm() + if form.validate(): + # Valid data, strip out all indexes with the blank type + indices = [ + index_def.to_model() + for index_def in form.indices.entries + if index_def.index_type.data + ] + indq.update(g.db, current_lexicon.id, indices) + return redirect(url_for("lexicon.settings.index", lexicon_name=lexicon_name)) + else: + # Invalid data + return render_template("settings.jinja", lexicon_name=lexicon_name, page_name=index.__name__, form=form) + + @bp.get("/publish/") @lexicon_param @editor_required diff --git a/amanuensis/server/lexicon/settings/forms.py b/amanuensis/server/lexicon/settings/forms.py index 612a7af..6b0f10c 100644 --- a/amanuensis/server/lexicon/settings/forms.py +++ b/amanuensis/server/lexicon/settings/forms.py @@ -1,15 +1,20 @@ from flask_wtf import FlaskForm from wtforms import ( BooleanField, + FieldList, + FormField, IntegerField, PasswordField, + SelectField, StringField, SubmitField, TextAreaField, ) -from wtforms.validators import Optional, DataRequired +from wtforms.validators import Optional, DataRequired, ValidationError from wtforms.widgets.html5 import NumberInput +from amanuensis.db import ArticleIndex, IndexType + class PlayerSettingsForm(FlaskForm): """/lexicon//settings/player/""" @@ -43,3 +48,41 @@ class SetupSettingsForm(FlaskForm): validators=[Optional()], ) submit = SubmitField("Submit") + + +def parse_index_type(type_str): + if not type_str: + return None + return getattr(IndexType, type_str) + + +class IndexDefinitionForm(FlaskForm): + """/lexicon//settings/index/""" + + TYPE_CHOICES = ([("", "")] + [(str(t), str(t).lower()) for t in IndexType]) + + index_type = SelectField(choices=TYPE_CHOICES, coerce=parse_index_type) + pattern = StringField() + logical_order = IntegerField(widget=NumberInput(min=-99, max=99), validators=[Optional()]) + display_order = IntegerField(widget=NumberInput(min=-99, max=99), validators=[Optional()]) + capacity = IntegerField(widget=NumberInput(min=0, max=99), validators=[Optional()]) + + def validate_pattern(form, field): + if form.index_type.data and not field.data: + raise ValidationError("Pattern must be defined") + + def to_model(self): + return ArticleIndex( + index_type=self.index_type.data, + pattern=self.pattern.data, + logical_order=self.logical_order.data, + display_order=self.display_order.data, + capacity=self.capacity.data, + ) + + +class IndexSchemaForm(FlaskForm): + """/lexicon//settings/index/""" + + indices = FieldList(FormField(IndexDefinitionForm)) + submit = SubmitField("Submit") diff --git a/amanuensis/server/lexicon/settings/settings.jinja b/amanuensis/server/lexicon/settings/settings.jinja index 3c4de9e..1e4eecd 100644 --- a/amanuensis/server/lexicon/settings/settings.jinja +++ b/amanuensis/server/lexicon/settings/settings.jinja @@ -21,9 +21,9 @@ {% block main %} {% if current_membership.is_editor %}
    -
  • {{ settings_page_link("player", "Player") }}
  • +
  • {{ settings_page_link("player", "Player Settings") }}
  • {{ settings_page_link("setup", "Game Setup") }}
  • -
  • {{ settings_page_link("progress", "Game Progress") }}
  • +
  • {{ settings_page_link("index", "Article Indexes") }}
  • {{ settings_page_link("publish", "Turn Publishing") }}
  • {{ settings_page_link("article", "Article Requirements") }}
@@ -31,6 +31,7 @@ {% if page_name == "player" %}

Player Settings

+

These settings are specific to you as a player in this lexicon.

{{ form.hidden_tag() }}

@@ -83,8 +84,49 @@ {% endfor %} {% endif %} -{% if page_name == "progress" %} -

Game Progress

+{% if page_name == "index" %} +

Article Indexes

+
+ Index definition help +

An index is a rule that matches the title of a lexicon article based on its index type and pattern. A char index matches a title if the first letter of the title (excluding "A", "An", and "The") is one of the letters in the pattern. A range index has a pattern denoting a range of letters, such as "A-F", and matches a title if the first letter of the title is in the range. A prefix index matches any title that begins with the pattern. An etc index always matches a title.

+

When a title is to be sorted under an index, indices are checked in order, sorted first by descending order of logical priority, and then by alphabetical order of index pattern. The title is sorted under the first index that matches it.

+

On the contents page, indices and the articles under them are displayed sorted instead by display order and then alphabetically by pattern.

+

The capacity of an index is the number of articles that may exist under that index. If an index is at capacity, no new articles may be written or created via phantom citation in that index.

+

To add an index, fill in the type and pattern in the blank row and save your changes. To remove an index, set the type to blank. Note: If you change the type or pattern of an index, all index assignments will be reset. Avoid changing index definitions during gameplay.

+
+ + {{ form.hidden_tag() }} + + + + + + + + + {% for index_form in form.indices %} + {{ index_form.hidden_tag() }} + + + + + + + + {% for field in index_form %} + {% for error in field.errors %} + + + + {% endfor %} + {% endfor %} + {% endfor %} +
TypePatternDisp OrLog OrCap
{{ index_form.index_type() }}{{ index_form.pattern() }}{{ index_form.logical_order() }}{{ index_form.display_order() }}{{ index_form.capacity() }}
{{ error }}
+

{{ form.submit() }}

+
+ {% for message in get_flashed_messages() %} + {{ message }}
+ {% endfor %} {% endif %} {% if page_name == "publish" %}