Make media posting work, with limitations.

Doesn't work with replies (for reasons I don't 100% understand yet).

Only one media field set in the form, though the rest could be added.
This commit is contained in:
Jason McBrayer 2018-05-01 14:44:56 -04:00
parent f3d34733fe
commit 1004b88779
3 changed files with 45 additions and 4 deletions

View File

@ -127,3 +127,7 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# Sanitizer settings
SANITIZER_ALLOWED_TAGS = ['a', 'p', 'img', 'br', 'i', 'strong']
SANITIZER_ALLOWED_ATTRIBUTES = ['href', 'src']
# File upload settings.
# Important: media will not work if you change this.
FILE_UPLOAD_HANDLERS = ["django.core.files.uploadhandler.TemporaryFileUploadHandler"]

View File

@ -1,9 +1,9 @@
{% load widget_tweaks %}
{% if reply %}
<form method="post" action="{% url "reply" toot.id %}">
<form method="post" action="{% url "reply" toot.id %}" enctype="multipart/form-data">
{% else %}
<form method="post" action="{% url "toot" %}">
<form method="post" action="{% url "toot" %}" enctype="multipart/form-data">
{% endif %}
{% csrf_token %}
@ -32,6 +32,21 @@
</div>
</div>
</div>
<div class="field">
<label class="label"> {{ form.media_file_1.label }}</label>
<div class="content">
{% render_field form.media_file_1 %}
</div>
<div class="control">
{% render_field form.media_text_1 class+="input" placeholder="Describe attachment" %}
</div>
</div>
<div>
{{ form.errors }}
</div>
<div class="field">
<div class="control">
<input type="submit" class="button is-primary"

View File

@ -5,6 +5,7 @@ from brutaldon.forms import LoginForm, SettingsForm, PostForm
from brutaldon.models import Client, Account
from mastodon import Mastodon
from urllib import parse
from django.core.files.uploadhandler import TemporaryFileUploadHandler
class NotLoggedInException(Exception):
pass
@ -165,11 +166,22 @@ def toot(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'])
spoiler_text=form.cleaned_data['spoiler_text'],
media_ids=media_objects)
return redirect(home)
else:
return render(request, 'main/post.html',
@ -198,6 +210,16 @@ def reply(request, id):
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'],