From b6ebe7b00e2b626bef549e3c1903bf35210e16c8 Mon Sep 17 00:00:00 2001 From: Shir Bar Lev Date: Sat, 19 Sep 2020 15:01:26 +0300 Subject: [PATCH] Added forbidden words to validation error to help users fix validation errors. --- app.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 745feee7..672d1686 100644 --- a/app.py +++ b/app.py @@ -227,10 +227,14 @@ def add_post(token_payload): # If there's a blacklisted word / phrase, alert the user else: num_issues = len(blacklist_check['indexes']) + forbidden_words = ''.join([str(word['badword']) + ', ' for word in blacklist_check['indexes']]) raise ValidationError({ 'code': 400, 'description': 'Your text contains ' + str(num_issues) + ' \ - forbidden term(s). Please fix your post\'s \ + forbidden term(s). The following word(s) is/ \ + are not allowed: ' + + forbidden_words[0:-2] + + '. Please fix your post\'s \ text and try again.' }, 400) @@ -295,11 +299,15 @@ def edit_post(token_payload, post_id): # If there's a blacklisted word / phrase, alert the user else: num_issues = len(blacklist_check['indexes']) + forbidden_words = ''.join([str(word['badword']) + ', ' for word in blacklist_check['indexes']]) raise ValidationError({ 'code': 400, 'description': 'Your text contains ' + str(num_issues) + ' forbidden \ - term(s). Please fix your post\'s \ + term(s). The following word(s) is/ \ + are not allowed: ' + + forbidden_words[0:-2] + + '. Please fix your post\'s \ text and try again.' }, 400) # Otherwise, the user is allowed to edit any post, and thus text @@ -318,11 +326,15 @@ def edit_post(token_payload, post_id): # If there's a blacklisted word / phrase, alert the user else: num_issues = len(blacklist_check['indexes']) + forbidden_words = ''.join([str(word['badword']) + ', ' for word in blacklist_check['indexes']]) raise ValidationError({ 'code': 400, 'description': 'Your text contains ' + str(num_issues) + ' forbidden \ - term(s). Please fix your post\'s \ + term(s). The following word(s) is/ \ + are not allowed: ' + + forbidden_words[0:-2] + + '. Please fix your post\'s \ text and try again.' }, 400) @@ -1020,10 +1032,14 @@ def add_message(token_payload): # If there's a blacklisted word / phrase, alert the user if(blacklist_check['blacklisted'] is True): num_issues = len(blacklist_check['indexes']) + forbidden_words = ''.join([str(word['badword']) + ', ' for word in blacklist_check['indexes']]) raise ValidationError({ 'code': 400, 'description': 'Your message contains ' + str(num_issues) + ' \ - forbidden term(s). Please fix your post\'s \ + forbidden term(s). The following word(s) is/ \ + are not allowed: ' + + forbidden_words[0:-2] + + '. Please fix your post\'s \ text and try again.' }, 400)