Add name helper to index model to aid identification

This commit is contained in:
Tim Van Baak 2021-09-20 14:19:45 -07:00
parent b94a2224c0
commit a91be8bc87
4 changed files with 18 additions and 22 deletions

View File

@ -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 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 matched to a an extant index is created. Matched indices are updated with
the input logical and display orders and capacity. 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)) 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: for extant_index in extant_indices:
match = None match = None
for new_index in indices: for new_index in indices:
is_match = ( if extant_index.name == new_index.name:
extant_index.index_type == new_index.index_type
and extant_index.pattern == new_index.pattern
)
if is_match:
match = new_index match = new_index
break break
if match: if match:
@ -111,14 +110,9 @@ def update(db: DbContext, lexicon_id: int, indices: Sequence[ArticleIndex]) -> N
for new_index in indices: for new_index in indices:
match = None match = None
for extant_index in extant_indices: for extant_index in extant_indices:
is_match = ( if extant_index.name == new_index.name:
extant_index.index_type == new_index.index_type
and extant_index.pattern == new_index.pattern
)
if is_match:
match = extant_index match = extant_index
break break
if not match: if not match:
new_index.lexicon_id = lexicon_id
db.session.add(new_index) db.session.add(new_index)
db.session.commit() db.session.commit()

View File

@ -505,6 +505,10 @@ class ArticleIndex(ModelBase):
lexicon = relationship("Lexicon", back_populates="indices") lexicon = relationship("Lexicon", back_populates="indices")
index_rules = relationship("ArticleIndexRule", back_populates="index") index_rules = relationship("ArticleIndexRule", back_populates="index")
@property
def name(self):
return f"{self.index_type}:{self.pattern}"
class ArticleIndexRule(ModelBase): class ArticleIndexRule(ModelBase):
""" """

View File

@ -161,7 +161,14 @@ def index_post(lexicon_name):
if form.validate(): if form.validate():
# Valid data, strip out all indices with the blank type # Valid data, strip out all indices with the blank type
indices = [ 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 for index_def in form.indices.entries
if index_def.index_type.data if index_def.index_type.data
] ]

View File

@ -80,15 +80,6 @@ class IndexDefinitionForm(FlaskForm):
if form.index_type.data and not field.data: if form.index_type.data and not field.data:
raise ValidationError("Pattern must be defined") 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): class IndexSchemaForm(FlaskForm):
"""/lexicon/<name>/settings/index/""" """/lexicon/<name>/settings/index/"""