Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
Instead of setting a max_length on posts, catch API error on posting
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmcbrayer committed Sep 4, 2018
1 parent 0d3cc1c commit 928f0fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
20 changes: 2 additions & 18 deletions brutaldon/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

timezones = [ (tz, tz) for tz in common_timezones]

MAX_LENGTH = settings.TOOT_MAX_LENGTH

class LoginForm(forms.Form):
instance = forms.CharField(label="Instance",
max_length=256)
Expand All @@ -31,39 +29,25 @@ class Meta:
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=MAX_LENGTH, widget=forms.Textarea)
status = forms.CharField(label="Toot", widget=forms.Textarea)
visibility = forms.ChoiceField(label="Toot visibility", choices=PRIVACY_CHOICES,
required=False)
spoiler_text = forms.CharField(label="CW or Subject", max_length=MAX_LENGTH,
spoiler_text = forms.CharField(label="CW or Subject",
required=False)
media_file_1 = forms.FileField(label = "Media 1",
required=False)
media_text_1 = forms.CharField(label="Describe media 1.",
max_length=MAX_LENGTH,
required=False)
media_file_2 = forms.FileField(label = "Media 2",
required=False)
media_text_2 = forms.CharField(label="Describe media 2.",
max_length=MAX_LENGTH,
required=False)
media_file_3 = forms.FileField(label = "Media 3",
required=False)
media_text_3 = forms.CharField(label="Describe media 3.",
max_length=MAX_LENGTH,
required=False)
media_file_4 = forms.FileField(label = "Media 4",
required=False)
media_text_4 = forms.CharField(label="Describe media 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 (status and spoiler_text and 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})
17 changes: 12 additions & 5 deletions brutaldon/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.core.files.uploadhandler import TemporaryFileUploadHandler
from brutaldon.forms import LoginForm, OAuthLoginForm, PreferencesForm, PostForm
from brutaldon.models import Client, Account, Preference, Theme
from mastodon import Mastodon, AttribAccessDict, MastodonError
from mastodon import Mastodon, AttribAccessDict, MastodonError, MastodonAPIError
from urllib import parse
from pdb import set_trace
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -411,10 +411,17 @@ def toot(request, mention=None):
None)))
if form.cleaned_data['visibility'] == '':
form.cleaned_data['visibility'] = request.session['user'].source.privacy
mastodon.status_post(status=form.cleaned_data['status'],
visibility=form.cleaned_data['visibility'],
spoiler_text=form.cleaned_data['spoiler_text'],
media_ids=media_objects)
try:
mastodon.status_post(status=form.cleaned_data['status'],
visibility=form.cleaned_data['visibility'],
spoiler_text=form.cleaned_data['spoiler_text'],
media_ids=media_objects)
except MastodonAPIError as error:
form.add_error("", "%s" % error.args[-1])
return render(request, 'main/post.html',
{'form': form,
'own_acct': request.session['user'],
'preferences': account.preferences})
return redirect(home)
else:
return render(request, 'main/post.html',
Expand Down

0 comments on commit 928f0fd

Please sign in to comment.