Implement more constraint analysis

This commit is contained in:
Tim Van Baak 2020-04-29 21:58:25 -07:00
parent 4e13cdeff6
commit 214136485c
2 changed files with 26 additions and 15 deletions

View File

@ -66,18 +66,28 @@ def get_draft(
def title_constraint_analysis(
lexicon: LexiconModel,
player: UserModel,
title: str) -> Tuple[List[str], List[str]]:
title: str) -> Tuple[List[str], List[str], List[str]]:
"""
Checks article constraints for the lexicon against a proposed
draft title.
"""
warnings = []
errors = []
infos: list = []
warnings: list = []
errors: list = []
with lexicon.ctx.read('info') as info:
# No title
# E: No title
if not title:
errors.append('Missing title')
return warnings, errors # No point in further analysis
return infos, warnings, errors # No point in further analysis
# I: This article is new
if title not in info:
infos.append('New article')
# I: This article is a phantom
elif info[title].character is None:
infos.append('Phantom article')
# E: This article has already been written and addendums are disabled
elif not lexicon.cfg.article.addendum.allowed:
errors.append('Article already exists')
# The article does not sort under the player's assigned index
pass
# The article's title is new, but its index is full
@ -97,7 +107,7 @@ def title_constraint_analysis(
# The article's title matches a character's name
pass # warning
return warnings, errors
return infos, warnings, errors
def content_constraint_analysis(
@ -109,13 +119,14 @@ def content_constraint_analysis(
Checks article constraints for the lexicon against the content of
a draft
"""
infos = []
warnings = []
errors = []
infos: list = []
warnings: list = []
errors: list = []
character = lexicon.cfg.character.get(cid)
content_analysis: ConstraintAnalysis = (
parsed.render(ConstraintAnalysis(lexicon)))
with lexicon.ctx.read('info') as info:
# I: Word count
infos.append(f'Word count: {content_analysis.word_count}')
# Self-citation when forbidden
pass
@ -130,20 +141,20 @@ def content_constraint_analysis(
# Too many total citations
# Not enough characters' articles cited
# Too many characters' articles cited
# Exceeded hard word limit
# E: Exceeded hard word limit
if (lexicon.cfg.article.word_limit.hard is not None
and content_analysis.word_count > lexicon.cfg.article.word_limit.hard):
errors.append('Exceeded maximum word count '
f'({lexicon.cfg.article.word_limit.hard})')
# Exceeded soft word limit
# W: Exceeded soft word limit
elif (lexicon.cfg.article.word_limit.soft is not None
and content_analysis.word_count > lexicon.cfg.article.word_limit.soft):
warnings.append('Exceeded suggested maximum word count '
f'({lexicon.cfg.article.word_limit.soft})')
# Missing signature
# W: Missing signature
if content_analysis.signatures < 1:
warnings.append('Missing signature')
# Multiple signatures
# W: Multiple signatures
if content_analysis.signatures > 1:
warnings.append('Multiple signatures')
# Signature altered from default

View File

@ -110,7 +110,7 @@ def update_draft(lexicon: LexiconModel, article_json):
# HTML parsing
preview = parsed.render(PreviewHtmlRenderer(lexicon))
# Constraint analysis
title_warnings, title_errors = title_constraint_analysis(
title_infos, title_warnings, title_errors = title_constraint_analysis(
lexicon, current_user, title)
content_infos, content_warnings, content_errors = content_constraint_analysis(
lexicon, current_user, article.character, parsed)
@ -133,7 +133,7 @@ def update_draft(lexicon: LexiconModel, article_json):
},
'rendered': preview.contents,
'citations': preview.citations,
'info': content_infos,
'info': title_infos + content_infos,
'warning': title_warnings + content_warnings,
'error': title_errors + content_errors,
}