Some fixes on SAS upload form

This commit is contained in:
Skia 2017-01-05 10:05:13 +01:00
parent 72685618a6
commit a90a553939
3 changed files with 34 additions and 10 deletions

View File

@ -80,7 +80,7 @@
{% if edit_mode %}
</form>
{% endif %}
<form action="" method="post" enctype="multipart/form-data">
<form id="upload_form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p() }}
<p><input type="submit" value="{% trans %}Upload{% endtrans %}" /></p>
@ -91,20 +91,20 @@
{% block script %}
{{ super() }}
<script>
$("form").submit(function (event) {
$("form#upload_form").submit(function (event) {
var formData = new FormData($(this)[0]);
if(formData.get('album_name') === '' && formData.get('images').name === '')
if(!formData.get('album_name') && !formData.get('images').name)
return false;
if(formData.get('images').name === '') {
if(!formData.get('images').name) {
return true;
}
event.preventDefault();
var errorlist;
if((errorlist = this.querySelector('ul.errorlist.nonfield')) === null) {
if((errorlist = this.querySelector('#upload_form ul.errorlist.nonfield')) === null) {
errorlist = document.createElement('ul');
errorlist.classList.add('errorlist', 'nonfield');
this.insertBefore(errorlist, this.firstElementChild);
@ -124,12 +124,13 @@ $("form").submit(function (event) {
var dataHolder;
if(formData.get('album_name') !== '') {
if(formData.get('album_name')) {
dataHolder = new FormData();
dataHolder.set('csrfmiddlewaretoken', '{{ csrf_token }}');
dataHolder.set('album_name', formData.get('album_name'));
$.ajax({
method: 'POST',
url: "{{ url('sas:album_upload', album_id=object.id) }}",
data: dataHolder,
processData: false,
contentType: false,
@ -157,10 +158,12 @@ $("form").submit(function (event) {
$.ajax({
method: 'POST',
url: "{{ url('sas:album_upload', album_id=object.id) }}",
data: dataHolder,
processData: false,
contentType: false,
})
.fail(onSuccess.bind(undefined, image))
.done(onSuccess.bind(undefined, image))
.always(next.bind(undefined, image));
}
@ -179,8 +182,8 @@ $("form").submit(function (event) {
}
function onSuccess(image, data, status, jqXHR) {
if ($(data).find('.errorlist.nonfield')[0])
var errors = Array.from($(data).find('.errorlist.nonfield')[0].children);
if ($(data.responseText).find('.errorlist.nonfield')[0])
var errors = Array.from($(data.responseText).find('.errorlist.nonfield')[0].children);
else
var errors = []
while(errors.length > 0)

View File

@ -6,6 +6,7 @@ urlpatterns = [
url(r'^$', SASMainView.as_view(), name='main'),
url(r'^moderation$', ModerationView.as_view(), name='moderation'),
url(r'^album/(?P<album_id>[0-9]+)$', AlbumView.as_view(), name='album'),
url(r'^album/(?P<album_id>[0-9]+)/upload$', AlbumUploadView.as_view(), name='album_upload'),
url(r'^album/(?P<album_id>[0-9]+)/edit$', AlbumEditView.as_view(), name='album_edit'),
url(r'^picture/(?P<picture_id>[0-9]+)$', PictureView.as_view(), name='picture'),
url(r'^picture/(?P<picture_id>[0-9]+)/edit$', PictureEditView.as_view(), name='picture_edit'),

View File

@ -1,4 +1,5 @@
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse_lazy, reverse
from django.views.generic import ListView, DetailView, RedirectView, TemplateView
from django.views.generic.edit import UpdateView, CreateView, DeleteView, ProcessFormView, FormMixin, FormView
@ -34,7 +35,7 @@ class SASForm(forms.Form):
album = Album(parent=parent, name=self.cleaned_data['album_name'], owner=owner, is_moderated=automodere)
album.clean()
album.save()
notif = True
notif = not automodere
except Exception as e:
self.add_error(None, _("Error creating album %(album)s: %(msg)s") %
{'album': self.cleaned_data['album_name'], 'msg': repr(e)})
@ -47,7 +48,7 @@ class SASForm(forms.Form):
new_file.clean()
new_file.generate_thumbnails()
new_file.save()
notif = True
notif = not automodere
except Exception as e:
self.add_error(None, _("Error uploading file %(file_name)s: %(msg)s") % {'file_name': f, 'msg': repr(e)})
if notif:
@ -148,6 +149,25 @@ def send_compressed(request, picture_id):
def send_thumb(request, picture_id):
return send_file(request, picture_id, Picture, "thumbnail")
class AlbumUploadView(CanViewMixin, DetailView, FormMixin):
model = Album
form_class = SASForm
pk_url_kwarg = "album_id"
def post(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = self.get_form()
parent = SithFile.objects.filter(id=self.object.id).first()
files = request.FILES.getlist('images')
if request.user.is_authenticated() and request.user.is_subscribed():
if self.form.is_valid():
self.form.process(parent=parent, owner=request.user, files=files,
automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID))
if self.form.is_valid():
return HttpResponse(str(self.form.errors), status=200)
return HttpResponse(str(self.form.errors), status=500)
class AlbumView(CanViewMixin, DetailView, FormMixin):
model = Album
form_class = SASForm