Further work on multi-file-upload
This commit is contained in:
@ -1,9 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.6 on 2018-04-19 19:19
|
||||
# Generated by Django 1.11.6 on 2018-04-23 02:20
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
@ -11,7 +10,6 @@ class Migration(migrations.Migration):
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('map', '0056_delete_shapefile'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
@ -20,8 +18,6 @@ class Migration(migrations.Migration):
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('file', models.FileField(upload_to='.')),
|
||||
('case_study', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='files', to='map.CaseStudy')),
|
||||
('case_study_draft', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='files', to='map.CaseStudyDraft')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
|
@ -7,16 +7,13 @@ class BaseFile(models.Model):
|
||||
file = models.FileField(
|
||||
upload_to='.',
|
||||
)
|
||||
case_study = models.ForeignKey(
|
||||
CaseStudy, related_name='files', blank=True, null=True,
|
||||
)
|
||||
case_study_draft = models.ForeignKey(
|
||||
CaseStudyDraft, related_name='files', blank=True, null=True
|
||||
)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def __str__(self):
|
||||
return self.file.name
|
||||
|
||||
|
||||
class File(BaseFile):
|
||||
pass
|
||||
|
@ -1,9 +1,40 @@
|
||||
$(function () {
|
||||
/* 2. INITIALIZE THE FILE UPLOAD COMPONENT */
|
||||
$("input[type=file]").fileupload({
|
||||
dataType: 'json',
|
||||
done: function (e, data) { /* 3. PROCESS THE RESPONSE FROM THE SERVER */
|
||||
console.log(data);
|
||||
}
|
||||
});
|
||||
// un-set "name" attributes to avoid submitting to server
|
||||
$(".fileupload").removeAttr('name');
|
||||
// set up all file inputs for jQuery-fileUpload
|
||||
$(".fileupload").fileupload({
|
||||
dataType: 'json',
|
||||
paramName: 'file',
|
||||
done: function (e, data) {
|
||||
// process server response
|
||||
if (data.result.is_valid) {
|
||||
var $field = $('#id_' + $(this).attr('data-field')),
|
||||
$ul = $(this).siblings('ul'),
|
||||
$li = $("<li>"),
|
||||
$remove = $('<a title="remove"></a>'),
|
||||
ids = $field.val().split(",").filter(function (v) {
|
||||
return v != '';
|
||||
});
|
||||
|
||||
ids.push(data.result.id);
|
||||
|
||||
if (!$ul.length) {
|
||||
$ul = $("<ul>").insertAfter(this);
|
||||
}
|
||||
|
||||
$li.text(data.result.name);
|
||||
|
||||
// FIXME AJAXify
|
||||
//$remove.attr('href', '/files/' + data.result.id + '/delete/');
|
||||
//$remove.append($('<i class="glyphicon glyphicon-remove"/>'));
|
||||
//$li.append($remove);
|
||||
|
||||
$ul.append($li);
|
||||
|
||||
$field.val(ids.toString());
|
||||
|
||||
document.getElementById('case-study-form').dispatchEvent(new Event('dirty'))
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -1,9 +1,10 @@
|
||||
from django.conf.urls import url
|
||||
|
||||
from .views import FileUploadView
|
||||
from .views import FileUploadView, FileDeleteView
|
||||
|
||||
app_name = 'files'
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^upload/?$', FileUploadView.as_view(), name='upload'),
|
||||
url(r'^upload/$', FileUploadView.as_view(), name='upload'),
|
||||
url(r'^delete/(?P<pk>\d+)/$', FileDeleteView.as_view(), name='delete'),
|
||||
]
|
||||
|
@ -1,19 +1,45 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import JsonResponse
|
||||
from django.views.generic import CreateView
|
||||
from django.views.generic import FormView, DetailView
|
||||
|
||||
from .forms import FileForm
|
||||
from .models import File
|
||||
|
||||
class FileUploadView(CreateView):
|
||||
class FileUploadView(FormView):
|
||||
# FIXME require login
|
||||
|
||||
model = File
|
||||
form_class = FileForm
|
||||
|
||||
def form_valid(self, form):
|
||||
# save the File to the database
|
||||
super().form_valid(form)
|
||||
self.object = form.save()
|
||||
|
||||
return JsonResponse({'is_valid': True, 'url': self.object.file.url})
|
||||
# FIXME set File owner
|
||||
|
||||
return JsonResponse({
|
||||
'is_valid': True, 'url': self.object.file.url,
|
||||
'name': self.object.file.name,
|
||||
'id': self.object.pk
|
||||
})
|
||||
|
||||
def form_invalid(self, form):
|
||||
return JsonResponse({'is_valid': False, 'errors': form.errors})
|
||||
|
||||
|
||||
class FileDeleteView(DetailView):
|
||||
# FIXME require login
|
||||
|
||||
model = File
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
return self.post(request, *args, **kwargs)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
# FIXME check file ownership
|
||||
|
||||
self.object = self.get_object()
|
||||
self.object.delete()
|
||||
|
||||
return JsonResponse({
|
||||
'success': True
|
||||
})
|
||||
|
Reference in New Issue
Block a user