From 8fa5da1409c0e42c504c3bfe3d459ce236da1683 Mon Sep 17 00:00:00 2001 From: Jason McBrayer Date: Thu, 2 Aug 2018 11:13:50 -0400 Subject: [PATCH] Do a better job of validating post length This still doesn't correctly handle the shorter "effective lengths" for handles and URLs. And it doesn't handle different per-server toot lengths. But it does catch posts that become too long because the length of the CW is counted as part of the length of the post body, and gives a validation error rather than throwing an exception. --- brutaldon/forms.py | 29 +- brutaldon/settings.py | 5 + brutaldon/static/css/brutaldon.css | 5 + brutaldon/static/css/fullbrutalism.css | 5 + .../templates/main/post_minimal_partial.html | 9 +- brutaldon/templates/main/post_partial.html | 263 +++++++++--------- 6 files changed, 174 insertions(+), 142 deletions(-) diff --git a/brutaldon/forms.py b/brutaldon/forms.py index b3a5f93..70456e9 100644 --- a/brutaldon/forms.py +++ b/brutaldon/forms.py @@ -1,10 +1,12 @@ from django import forms +from django.conf import settings PRIVACY_CHOICES = (('public', 'Public'), ('unlisted', 'Unlisted'), ('private', 'Private'), ('direct', 'Direct')) +MAX_LENGTH = settings.TOOT_MAX_LENGTH class LoginForm(forms.Form): instance = forms.CharField(label="Instance", @@ -27,27 +29,40 @@ class SettingsForm(forms.Form): class PostForm(forms.Form): """def status_post(self, status, in_reply_to_id=None, media_ids=None, -sensitive=False, visibility=None, spoiler_text=None):""" - status = forms.CharField(label="Toot", max_length=500, widget=forms.Textarea) + sensitive=False, visibility=None, spoiler_text=None):""" + status = forms.CharField(label="Toot", max_length=MAX_LENGTH, widget=forms.Textarea) visibility = forms.ChoiceField(label="Toot visibility", choices=PRIVACY_CHOICES, required=False) - spoiler_text = forms.CharField(label="CW or Subject", max_length=500, + spoiler_text = forms.CharField(label="CW or Subject", max_length=MAX_LENGTH, required=False) media_file_1 = forms.FileField(label = "Media attachment 1", required=False) - media_text_1 = forms.CharField(label="Describe media attachment 1.", max_length=500, + media_text_1 = forms.CharField(label="Describe media attachment 1.", + max_length=MAX_LENGTH, required=False) media_file_2 = forms.FileField(label = "Media attachment 2", required=False) - media_text_2 = forms.CharField(label="Describe media attachment 2.", max_length=500, + media_text_2 = forms.CharField(label="Describe media attachment 2.", + max_length=MAX_LENGTH, required=False) media_file_3 = forms.FileField(label = "Media attachment 3", required=False) - media_text_3 = forms.CharField(label="Describe media attachment 3.", max_length=500, + media_text_3 = forms.CharField(label="Describe media attachment 3.", + max_length=MAX_LENGTH, required=False) media_file_4 = forms.FileField(label = "Media attachment 4", required=False) - media_text_4 = forms.CharField(label="Describe media attachment 4.", max_length=500, + media_text_4 = forms.CharField(label="Describe media attachment 4.", + max_length=MAX_LENGTH, required=False) media_sensitive = forms.BooleanField(label="Sensitive media?", required=False) + def clean(self): + cleaned_data = super().clean() + status = cleaned_data.get("status") + spoiler_text = cleaned_data.get("spoiler_text") + + if len(status) + len(spoiler_text) > MAX_LENGTH: + raise forms.ValidationError("Max length of toot exceeded: %(max_length)s", + code="too_long", + params={"max_length": MAX_LENGTH}) diff --git a/brutaldon/settings.py b/brutaldon/settings.py index 42f9d52..cca07fe 100644 --- a/brutaldon/settings.py +++ b/brutaldon/settings.py @@ -187,3 +187,8 @@ FILE_UPLOAD_HANDLERS = ["django.core.files.uploadhandler.TemporaryFileUploadHand # Session serialization # Important: whatever you choose has to be able to serialize DateTime, so not JSON. SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer' + +# Max length of toots +# Later this will be a user setting, but I am adding it here so that I don't +# write any magic numbers into the validation code +TOOT_MAX_LENGTH = 500 diff --git a/brutaldon/static/css/brutaldon.css b/brutaldon/static/css/brutaldon.css index 7648dfb..7157683 100644 --- a/brutaldon/static/css/brutaldon.css +++ b/brutaldon/static/css/brutaldon.css @@ -75,6 +75,11 @@ span.account-locked margin-left: -16px; } +.errorlist +{ + color: #FF0000; +} + @media screen and (max-width: 768px) { .media { display: block; diff --git a/brutaldon/static/css/fullbrutalism.css b/brutaldon/static/css/fullbrutalism.css index 85eedbc..3b80ba0 100644 --- a/brutaldon/static/css/fullbrutalism.css +++ b/brutaldon/static/css/fullbrutalism.css @@ -282,3 +282,8 @@ label { max-width: 100%; } + +.errorlist +{ + color: #FF0000; +} diff --git a/brutaldon/templates/main/post_minimal_partial.html b/brutaldon/templates/main/post_minimal_partial.html index bb8fc9c..0e14e1f 100644 --- a/brutaldon/templates/main/post_minimal_partial.html +++ b/brutaldon/templates/main/post_minimal_partial.html @@ -2,6 +2,11 @@
{% csrf_token %} +
+ {{ form.non_field_errors }} +
+
+
@@ -9,10 +14,6 @@
-
- {{ form.errors }} -
-
diff --git a/brutaldon/templates/main/post_partial.html b/brutaldon/templates/main/post_partial.html index a9790ea..5a4c25a 100644 --- a/brutaldon/templates/main/post_partial.html +++ b/brutaldon/templates/main/post_partial.html @@ -5,153 +5,154 @@ {% else %} {% endif %} - {% csrf_token %} +{% csrf_token %} -
- -
- {% render_field form.spoiler_text class+="input mousetrap" placeholder="Optional" %} +
+ {{ form.non_field_errors }} +
+
+ +
+ +
+ {% render_field form.spoiler_text class+="input mousetrap" placeholder="Optional" %} +
+
+ +
+ +
+ {% render_field form.status class+="textarea mousetrap is-primary" rows="4" %} +
+
+ +
+ +
+
+ {% render_field form.visibility class+="select"%} + + +
+
-
- -
- {% render_field form.status class+="textarea mousetrap is-primary" rows="4" %} -
-
- -
- -
-
- {% render_field form.visibility class+="select"%} - - +
+
+
-
-
- -
-
- -
-
- {% render_field form.media_text_1 class+="input mousetrap" placeholder="Describe attachment" %} -
+ +
+
+ {% render_field form.media_text_1 class+="input mousetrap" placeholder="Describe attachment" %} +
+
-
-
-