def toot(request): if request.method == 'GET': form = PostForm() return render(request, 'main/post.html', { 'form': form, 'fullbrutalism': fullbrutalism_p(request) }) elif request.method == 'POST': form = PostForm(request.POST, request.FILES) if form.is_valid(): mastodon = get_mastodon(request) # create media objects media_objects = [] for index in range(1, 5): if 'media_file_' + str(index) in request.FILES: media_objects.append( mastodon.media_post( request.FILES['media_file_' + str(index)].temporary_file_path(), description=request.POST.get( 'media_text_' + str(index), None))) mastodon.status_post( status=form.cleaned_data['status'], visibility=form.cleaned_data['visibility'], spoiler_text=form.cleaned_data['spoiler_text'], media_ids=media_objects) return redirect(home) else: return render(request, 'main/post.html', { 'form': form, 'fullbrutalism': fullbrutalism_p(request) }) else: return redirect(toot)
def reply(request, id): if request.method == 'GET': mastodon = get_mastodon(request) toot = mastodon.status(id) context = mastodon.status_context(id) initial_text = '@' + toot.account.acct + " " for mention in toot.mentions: initial_text += ('@' + mention.acct + " ") form = PostForm({ 'status': initial_text, 'visibility': toot.visibility, 'spoiler_text': toot.spoiler_text }) return render( request, 'main/reply.html', { 'context': context, 'toot': toot, 'form': form, 'reply': True, 'fullbrutalism': fullbrutalism_p(request) }) elif request.method == 'POST': form = PostForm(request.POST, request.FILES) if form.is_valid(): # create media objects mastodon = get_mastodon(request) # create media objects media_objects = [] for index in range(1, 5): if 'media_file_' + str(index) in request.FILES: media_objects.append( mastodon.media_post( request.FILES['media_file_' + str(index)].temporary_file_path(), description=request.POST.get( 'media_text_' + str(index), None))) mastodon.status_post( status=form.cleaned_data['status'], visibility=form.cleaned_data['visibility'], spoiler_text=form.cleaned_data['spoiler_text'], in_reply_to_id=id) return redirect(thread, id) else: return render( request, 'main/reply.html', { 'context': context, 'toot': toot, 'form': form, 'reply': True, 'fullbrutalism': fullbrutalism_p(request) }) else: return redirect(reply, id)
def toot(request, mention=None): if request.method == 'GET': if mention: if not mention.startswith('@'): mention = '@' + mention form = PostForm( initial={ 'visibility': request.session['user'].source.privacy, 'status': mention + '\n' }) else: form = PostForm( initial={'visibility': request.session['user'].source.privacy}) return render(request, 'main/post.html', { 'form': form, 'fullbrutalism': fullbrutalism_p(request) }) elif request.method == 'POST': form = PostForm(request.POST, request.FILES) if form.is_valid(): mastodon = get_mastodon(request) # create media objects media_objects = [] for index in range(1, 5): if 'media_file_' + str(index) in request.FILES: media_objects.append( mastodon.media_post( request.FILES['media_file_' + str(index)].temporary_file_path(), description=request.POST.get( 'media_text_' + str(index), 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) return redirect(home) else: return render(request, 'main/post.html', { 'form': form, 'fullbrutalism': fullbrutalism_p(request) }) else: return redirect(toot)
def timeline(request, timeline='home', timeline_name='Home'): try: mastodon = get_mastodon(request) except NotLoggedInException: return redirect(login) data = mastodon.timeline(timeline) form = PostForm() return render( request, 'main/timeline.html', { 'toots': data, 'form': form, 'timeline': timeline_name, 'fullbrutalism': fullbrutalism_p(request) })
def timeline(request, timeline='home', timeline_name='Home', max_id=None, since_id=None): account, mastodon = get_usercontext(request) data = mastodon.timeline(timeline, limit=40, max_id=max_id, since_id=since_id) form = PostForm( initial={'visibility': request.session['user'].source.privacy}) try: prev = data[0]._pagination_prev if len(mastodon.timeline(since_id=prev['since_id'])) == 0: prev = None else: prev['since_id'] = data[0].id except (IndexError, AttributeError): prev = None try: next = data[-1]._pagination_next next['max_id'] = data[-1].id except (IndexError, AttributeError): next = None notifications = _notes_count(account, mastodon) # This filtering has to be done *after* getting next/prev links if account.preferences.filter_replies: data = [x for x in data if not x.in_reply_to_id] if account.preferences.filter_boosts: data = [x for x in data if not x.reblog] return render( request, 'main/%s_timeline.html' % timeline, { 'toots': data, 'form': form, 'timeline': timeline, 'timeline_name': timeline_name, 'own_acct': request.session['user'], 'preferences': account.preferences, 'notifications': notifications, 'prev': prev, 'next': next })
def timeline(request, timeline='home', timeline_name='Home', max_id=None, since_id=None): try: mastodon = get_mastodon(request) except NotLoggedInException: return redirect(about) data = mastodon.timeline(timeline, limit=100, max_id=max_id, since_id=since_id) form = PostForm( initial={'visibility': request.session['user'].source.privacy}) try: prev = data[0]._pagination_prev if len(mastodon.timeline(since_id=prev['since_id'])) == 0: prev = None except IndexError: prev = None try: next = data[-1]._pagination_next except IndexError: next = None return render( request, 'main/%s_timeline.html' % timeline, { 'toots': data, 'form': form, 'timeline': timeline, 'timeline_name': timeline_name, 'own_username': request.session['user'].acct, 'fullbrutalism': fullbrutalism_p(request), 'prev': prev, 'next': next })
def reply(request, id): if request.method == 'GET': account, mastodon = get_usercontext(request) toot = mastodon.status(id) context = mastodon.status_context(id) notifications = _notes_count(account, mastodon) if toot.account.acct != request.session['user'].acct: initial_text = '@' + toot.account.acct + " " else: initial_text = "" for mention in [ x for x in toot.mentions if x.acct != request.session['user'].acct and x.acct != toot.account.acct ]: initial_text += ('@' + mention.acct + " ") form = PostForm( initial={ 'status': initial_text, 'visibility': toot.visibility, 'spoiler_text': toot.spoiler_text }) return render( request, 'main/reply.html', { 'context': context, 'toot': toot, 'form': form, 'reply': True, 'own_acct': request.session['user'], 'notifications': notifications, 'preferences': account.preferences }) elif request.method == 'POST': form = PostForm(request.POST, request.FILES) account, mastodon = get_usercontext(request) toot = mastodon.status(id) context = mastodon.status_context(id) notifications = _notes_count(account, mastodon) if form.is_valid(): # create media objects media_objects = [] for index in range(1, 5): if 'media_file_' + str(index) in request.FILES: media_objects.append( mastodon.media_post( request.FILES['media_file_' + str(index)].temporary_file_path(), description=request.POST.get( 'media_text_' + str(index), None))) try: 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, in_reply_to_id=id, content_type="text/markdown") except TypeError: mastodon.status_post( status=form.cleaned_data['status'], visibility=form.cleaned_data['visibility'], spoiler_text=form.cleaned_data['spoiler_text'], media_ids=media_objects, in_reply_to_id=id) except MastodonAPIError as error: form.add_error( "", "%s (%s used)" % (error.args[-1], len(form.cleaned_data['status']) + len(form.cleaned_data['spoiler_text']))) return render( request, 'main/reply.html', { 'context': context, 'toot': toot, 'form': form, 'reply': True, 'own_acct': request.session['user'], 'notifications': notifications, 'preferences': account.preferences }) return HttpResponseRedirect( reverse('thread', args=[id]) + "#toot-" + str(id)) else: return render( request, 'main/reply.html', { 'context': context, 'toot': toot, 'form': form, 'reply': True, 'own_acct': request.session['user'], 'preferences': account.preferences }) else: return HttpResponseRedirect( reverse('reply', args=[id]) + "#toot-" + str(id))
def redraft(request, id): if request.method == 'GET': account, mastodon = get_usercontext(request) toot = mastodon.status(id) toot_content = get_text(toot.content) toot_content = re.sub("(^\n)|(\n$)", '', re.sub("\n\n", "\n", toot_content)) form = PostForm({ 'status': toot_content, 'visibility': toot.visibility, 'spoiler_text': toot.spoiler_text, 'media_text_1': safe_get_attachment(toot, 0).description, 'media_text_2': safe_get_attachment(toot, 1).description, 'media_text_3': safe_get_attachment(toot, 2).description, 'media_text_4': safe_get_attachment(toot, 3).description, }) return render( request, 'main/redraft.html', { 'toot': toot, 'form': form, 'redraft': True, 'own_acct': request.session['user'], 'preferences': account.preferences }) elif request.method == 'POST': form = PostForm(request.POST, request.FILES) account, mastodon = get_usercontext(request) toot = mastodon.status(id) if form.is_valid(): media_objects = [] for index in range(1, 5): if 'media_file_' + str(index) in request.FILES: media_objects.append( mastodon.media_post( request.FILES['media_file_' + str(index)].temporary_file_path(), description=request.POST.get( 'media_text_' + str(index), None))) if form.cleaned_data['visibility'] == '': form.cleaned_data['visibility'] = request.session[ 'user'].source.privacy try: 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, in_reply_to_id=toot.in_reply_to_id, content_type='text/markdown') except TypeError: mastodon.status_post( status=form.cleaned_data['status'], visibility=form.cleaned_data['visibility'], spoiler_text=form.cleaned_data['spoiler_text'], media_ids=media_objects, in_reply_to_id=toot.in_reply_to_id) mastodon.status_delete(id) except MastodonAPIError as error: form.add_error( "", "%s (%s used)" % (error.args[-1], len(form.cleaned_data['status']) + len(form.cleaned_data['spoiler_text']))) return render( request, 'main/redraft.html', { 'toot': toot, 'form': form, 'redraft': True, 'own_acct': request.session['user'], 'preferences': account.preferences }) return redirect(home) else: return render( request, 'main/redraft.html', { 'toot': toot, 'form': form, 'redraft': True, 'own_acct': request.session['user'], 'preferences': account.preferences }) else: return redirect(redraft, id)
def toot(request, mention=None): account, mastodon = get_usercontext(request) if request.method == 'GET': if mention: if not mention.startswith('@'): mention = '@' + mention form = PostForm( initial={ 'visibility': request.session['user'].source.privacy, 'status': mention + ' ' }) else: form = PostForm( initial={'visibility': request.session['user'].source.privacy}) if request.GET.get('ic-request'): return render( request, 'intercooler/post.html', { 'form': form, 'own_acct': request.session['user'], 'preferences': account.preferences }) else: return render( request, 'main/post.html', { 'form': form, 'own_acct': request.session['user'], 'preferences': account.preferences }) elif request.method == 'POST': form = PostForm(request.POST, request.FILES) if form.is_valid(): # create media objects media_objects = [] for index in range(1, 5): if 'media_file_' + str(index) in request.FILES: media_objects.append( mastodon.media_post( request.FILES['media_file_' + str(index)].temporary_file_path(), description=request.POST.get( 'media_text_' + str(index), None))) if form.cleaned_data['visibility'] == '': form.cleaned_data['visibility'] = request.session[ 'user'].source.privacy try: 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, content_type='text/markdown') except TypeError: 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 (%s used)" % (error.args[-1], len(form.cleaned_data['status']) + len(form.cleaned_data['spoiler_text']))) 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', { 'form': form, 'own_acct': request.session['user'], 'preferences': account.preferences }) else: return redirect(toot)