From a91be8bc87210d7a06e241217aea17af99b8410b Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 20 Sep 2021 14:19:45 -0700 Subject: [PATCH] Add name helper to index model to aid identification --- amanuensis/backend/index.py | 18 ++++++------------ amanuensis/db/models.py | 4 ++++ amanuensis/server/lexicon/settings/__init__.py | 9 ++++++++- amanuensis/server/lexicon/settings/forms.py | 9 --------- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/amanuensis/backend/index.py b/amanuensis/backend/index.py index 17a8bb5..eca5f2e 100644 --- a/amanuensis/backend/index.py +++ b/amanuensis/backend/index.py @@ -89,17 +89,16 @@ def update(db: DbContext, lexicon_id: int, indices: Sequence[ArticleIndex]) -> N An extant index not matched to an input is deleted, and an input index not matched to a an extant index is created. Matched indices are updated with the input logical and display orders and capacity. + + Note that this scheme does not allow for an existing index to have its type + or pattern updated: such an operation will always result in the deletion of + the old index and the creation of a new index. """ 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: + if extant_index.name == new_index.name: match = new_index break if match: @@ -111,14 +110,9 @@ def update(db: DbContext, lexicon_id: int, indices: Sequence[ArticleIndex]) -> N 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: + if extant_index.name == new_index.name: 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 f208463..7251591 100644 --- a/amanuensis/db/models.py +++ b/amanuensis/db/models.py @@ -505,6 +505,10 @@ class ArticleIndex(ModelBase): lexicon = relationship("Lexicon", back_populates="indices") index_rules = relationship("ArticleIndexRule", back_populates="index") + @property + def name(self): + return f"{self.index_type}:{self.pattern}" + class ArticleIndexRule(ModelBase): """ diff --git a/amanuensis/server/lexicon/settings/__init__.py b/amanuensis/server/lexicon/settings/__init__.py index f283756..2a0b258 100644 --- a/amanuensis/server/lexicon/settings/__init__.py +++ b/amanuensis/server/lexicon/settings/__init__.py @@ -161,7 +161,14 @@ def index_post(lexicon_name): if form.validate(): # Valid data, strip out all indices with the blank type indices = [ - index_def.to_model() + ArticleIndex( + lexicon_id=current_lexicon.id, + index_type=index_def.index_type.data, + pattern=index_def.pattern.data, + logical_order=index_def.logical_order.data, + display_order=index_def.display_order.data, + capacity=index_def.capacity.data + ) for index_def in form.indices.entries if index_def.index_type.data ] diff --git a/amanuensis/server/lexicon/settings/forms.py b/amanuensis/server/lexicon/settings/forms.py index 7b9209c..74faa47 100644 --- a/amanuensis/server/lexicon/settings/forms.py +++ b/amanuensis/server/lexicon/settings/forms.py @@ -80,15 +80,6 @@ class IndexDefinitionForm(FlaskForm): 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/"""