Implement title constraints

This commit is contained in:
Tim Van Baak 2020-04-30 22:44:52 -07:00
parent 70c2241e91
commit b936a850db
2 changed files with 47 additions and 19 deletions

View File

@ -67,6 +67,7 @@ def get_draft(
def title_constraint_analysis( def title_constraint_analysis(
lexicon: LexiconModel, lexicon: LexiconModel,
player: UserModel, player: UserModel,
cid: str,
title: str) -> Tuple[List[str], List[str], List[str]]: title: str) -> Tuple[List[str], List[str], List[str]]:
""" """
Checks article constraints for the lexicon against a proposed Checks article constraints for the lexicon against a proposed
@ -86,30 +87,57 @@ def title_constraint_analysis(
# I: This article is a phantom # I: This article is a phantom
elif info[title].character is None: elif info[title].character is None:
infos.append('Phantom article') infos.append('Phantom article')
# I: This article is an addendum
elif lexicon.cfg.article.addendum.allowed:
infos.append('Addendum article')
# E: And this player is already at the addendum limit
pass # TODO
# E: This article has already been written and addendums are disabled # E: This article has already been written and addendums are disabled
elif not lexicon.cfg.article.addendum.allowed: else:
errors.append('Article already exists') errors.append('Article already exists')
# I: This article's index # I: This article's index
index = get_index_for_title(lexicon, title) index = get_index_for_title(lexicon, title)
infos.append(f'Article index: {index}') infos.append(f'Article index: {index}')
# The article does not sort under the player's assigned index # E: The article does not sort under the player's assigned index
pass turn = lexicon.cfg.turn.current
assignments = lexicon.cfg.turn.assignment.get(turn, [])
fits = None
for char_id, index_pattern in assignments:
if char_id == cid and fits is None:
fits = False
if char_id == cid and index_pattern == index:
fits = True
if fits is not None and not fits:
errors.append('Article is not under your assigned index')
# The article's title is new, but its index is full # The article's title is new, but its index is full
pass pass # TODO
# The article's title is a phantom, but the player has cited it before # E: The article's title is a phantom, but the player has cited it before
info if title in info and not info[title].character:
# Another player is writing an article with this title for atitle, ainfo in info.items():
pass # warning if ainfo.character == cid and title in ainfo.citations:
# Another player has an approved article with this title errors.append(f'Cited by your article: {atitle}')
pass break
# An article with this title was already written and addendums are # W: Another player is writing an article with this title
# disabled draft_ctx = lexicon.ctx.draft
pass drafts = []
# An article with this title was already written and this player has for draft_fn in draft_ctx.ls():
# reached the maximum number of addendum articles with draft_ctx.read(draft_fn) as draft:
pass drafts.append(draft)
# The article's title matches a character's name if len([
pass # warning d for d in drafts
if not d.status.ready and d.title == title]) > 1:
warnings.append('Another player is writing an article with '
'this title')
# E: Another player has an approved article with this title
if len([
d for d in drafts
if d.status.approved and d.title == title]) > 1:
errors.append('Another player has already written this article')
# W: The article's title matches a character's name
for char in lexicon.cfg.character.values():
if len(char.name) > 10 and title == char.name:
warnings.append(f'"{title}" is the name of a character. '
'Are you sure you want to do that?')
return infos, warnings, errors return infos, warnings, errors

View File

@ -111,7 +111,7 @@ def update_draft(lexicon: LexiconModel, article_json):
preview = parsed.render(PreviewHtmlRenderer(lexicon)) preview = parsed.render(PreviewHtmlRenderer(lexicon))
# Constraint analysis # Constraint analysis
title_infos, title_warnings, title_errors = title_constraint_analysis( title_infos, title_warnings, title_errors = title_constraint_analysis(
lexicon, current_user, title) lexicon, current_user, article.character, title)
content_infos, content_warnings, content_errors = content_constraint_analysis( content_infos, content_warnings, content_errors = content_constraint_analysis(
lexicon, current_user, article.character, parsed) lexicon, current_user, article.character, parsed)
if any(title_errors) or any(content_errors): if any(title_errors) or any(content_errors):