diff --git a/securedrop/journalist_app/admin.py b/securedrop/journalist_app/admin.py index 18397f277b3..1e799435181 100644 --- a/securedrop/journalist_app/admin.py +++ b/securedrop/journalist_app/admin.py @@ -15,7 +15,7 @@ from db import db from html import escape from models import (InstanceConfig, Journalist, InvalidUsernameException, - FirstOrLastNameError, PasswordError) + FirstOrLastNameError, PasswordError, Submission) from journalist_app.decorators import admin_required from journalist_app.utils import (commit_account_changes, set_diceware_password, validate_hotp_secret, revoke_token) @@ -36,9 +36,15 @@ def index() -> str: @view.route('/config', methods=('GET', 'POST')) @admin_required def manage_config() -> Union[str, werkzeug.Response]: - # The UI prompt ("prevent") is the opposite of the setting ("allow"): + # The UI document upload prompt ("prevent") is the opposite of the setting ("allow") + if InstanceConfig.get_default().initial_message_min_len > 0: + prevent_short_messages = True + else: + prevent_short_messages = False + submission_preferences_form = SubmissionPreferencesForm( prevent_document_uploads=not InstanceConfig.get_default().allow_document_uploads, + prevent_short_messages=prevent_short_messages, min_message_length=InstanceConfig.get_default().initial_message_min_len, reject_codename_messages=InstanceConfig.get_default().reject_message_with_codename ) @@ -70,6 +76,7 @@ def manage_config() -> Union[str, werkzeug.Response]: return render_template("config.html", submission_preferences_form=submission_preferences_form, organization_name_form=organization_name_form, + max_len=Submission.MAX_MESSAGE_LEN, logo_form=logo_form) @view.route('/update-submission-preferences', methods=['POST']) @@ -80,16 +87,19 @@ def update_submission_preferences() -> Optional[werkzeug.Response]: # The UI prompt ("prevent") is the opposite of the setting ("allow"): allow_uploads = not form.prevent_document_uploads.data - try: - msg_length = form.min_message_length.data - - if isinstance(msg_length, int): - int_length = msg_length - elif isinstance(msg_length, str): - int_length = int(msg_length) - else: + if form.prevent_short_messages.data: + try: + msg_length = form.min_message_length.data + + if isinstance(msg_length, int): + int_length = msg_length + elif isinstance(msg_length, str): + int_length = int(msg_length) + else: + int_length = 0 + except ValueError: int_length = 0 - except ValueError: + else: int_length = 0 reject_codenames = form.reject_codename_messages.data diff --git a/securedrop/journalist_app/forms.py b/securedrop/journalist_app/forms.py index bc94e30ff7d..2af00474ecc 100644 --- a/securedrop/journalist_app/forms.py +++ b/securedrop/journalist_app/forms.py @@ -131,10 +131,16 @@ class SubmissionPreferencesForm(FlaskForm): 'prevent_document_uploads', false_values=('false', 'False', '') ) - min_message_length = IntegerField('min_message_length', validators=[check_message_length], + prevent_short_messages = BooleanField( + 'prevent_short_messages', + false_values=('false', 'False', '') + ) + min_message_length = IntegerField('min_message_length', + validators=[ + RequiredIf('prevent_short_messages'), + check_message_length], render_kw={ - 'aria-describedby': 'message-length-notes', - 'class': 'short_int_field'}) + 'aria-describedby': 'message-length-notes'}) reject_codename_messages = BooleanField( 'reject_codename_messages', false_values=('false', 'False', '') diff --git a/securedrop/journalist_templates/config.html b/securedrop/journalist_templates/config.html index 9473d7e4446..641795cd9cc 100644 --- a/securedrop/journalist_templates/config.html +++ b/securedrop/journalist_templates/config.html @@ -55,6 +55,7 @@