Extra file upload fields, delete files w/ draft

This commit is contained in:
Carl van Tonder 2018-04-30 22:17:18 -04:00
parent f4c21006de
commit e43c32b843
4 changed files with 45 additions and 21 deletions

View File

@ -190,4 +190,10 @@ $(function() {
window.official_project_documents = new MultipleFilesWidget(
document.querySelector('[data-field=official_project_documents_files]')
)
window.other_documents = new MultipleFilesWidget(
document.querySelector('[data-field=other_documents_files]')
)
window.shapefiles = new MultipleFilesWidget(
document.querySelector('[data-field=shapefiles_files]')
)
})

View File

@ -1,5 +1,6 @@
from django.shortcuts import render
from django.http import JsonResponse
from django.shortcuts import render
from django.views.generic import FormView, DetailView
from .forms import FileForm

View File

@ -103,8 +103,8 @@ class LongCaseStudyForm(BaseCaseStudyForm):
other_documents = forms.FileField(
widget=BootstrapClearableFileInput(attrs={
'data-url': reverse_lazy('files:upload'),
'data-field': 'other_documents_files',
'url': reverse_lazy('files:upload'),
'field': 'other_documents_files',
}), required=False
)
@ -116,8 +116,8 @@ class LongCaseStudyForm(BaseCaseStudyForm):
shapefiles = forms.FileField(
widget=BootstrapClearableFileInput(attrs={
'data-url': reverse_lazy('files:upload'),
'data-field': 'shapefiles_files',
'url': reverse_lazy('files:upload'),
'field': 'shapefiles_files',
}), required=False
)

View File

@ -1,3 +1,5 @@
import json
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.mail import send_mail
@ -11,6 +13,8 @@ from django.views.generic.edit import CreateView
from dal import autocomplete
from apps.files.models import File
from .models import CaseStudy, CaseStudyDraft, SpatialRefSys
from .forms import ShortCaseStudyForm, LongCaseStudyForm
@ -55,12 +59,10 @@ class BaseForm(LoginRequiredMixin, CreateView):
fail_silently=False,
)
def form_invalid(self, form):
print(form.errors)
return super().form_invalid(form)
def form_valid(self, form):
from pdb import set_trace; set_trace()
form.cleaned_data.pop('official_project_documents', None)
form.cleaned_data.pop('other_documents', None)
form.cleaned_data.pop('shapefiles', None)
self.object = form.save()
@ -77,7 +79,7 @@ class BaseForm(LoginRequiredMixin, CreateView):
self.send_email()
# Delete the corresponding draft
draft = CaseStudyDraft.objects.get(author=request.user)
draft = CaseStudyDraft.objects.get(author=self.request.user)
if draft:
draft.delete()
@ -127,9 +129,6 @@ class Drafts(View):
return None
def get(self, request):
if not request.user.is_authenticated:
return HttpResponse(status=403) # Forbidden
draft = self.get_object(request)
if draft == None:
return HttpResponse(status=404) # Not Found
@ -137,9 +136,6 @@ class Drafts(View):
return HttpResponse(draft.data, content_type="application/json")
def put(self, request):
if not request.user.is_authenticated:
return HttpResponse(status=403) # Forbidden
# Find an existing object is there is one
draft = self.get_object(request)
if draft == None:
@ -153,14 +149,35 @@ class Drafts(View):
return HttpResponse(status=200) # OK
def delete(self, request):
if not request.user.is_authenticated:
return HttpResponse(status=403) # Forbidden
draft = self.get_object(request)
if draft != None:
data = json.loads(draft.data)
for k in ['official_project_documents', 'other_documents',
'shapefiles']:
items = list(filter(
lambda x: (
x['name'] == '{0}_files'.format(k)
and x['value'] != ''
), data['data']['form']
))
try:
items = items[0]['value'].split(',')
except IndexError:
continue
for item in items:
try:
f = File.objects.get(id=item)
if f.user != self.request.user:
continue
f.delete()
except File.DoesNotExist:
continue
draft.delete()
from pdb import set_trace; set_trace()
return HttpResponse(status=204)