Restore names of uploaded files on submit/draft restore (#65)

This involved turning the list of file IDs stored in the hidden
text field into JSON.
This commit is contained in:
2018-10-12 18:30:47 -04:00
parent b5ccbf631d
commit 79dffadd14
5 changed files with 93 additions and 38 deletions

View File

@ -68,7 +68,7 @@ class BaseForm(LoginRequiredMixin, CreateView):
self.object = form.save()
self.object.official_project_documents = form.cleaned_data.get(
'official_project_document_files', []
'official_project_documents_files', []
)
self.object.other_documents = form.cleaned_data.get(
'other_documents_files', []
@ -76,6 +76,9 @@ class BaseForm(LoginRequiredMixin, CreateView):
self.object.shapefiles = form.cleaned_data.get(
'shapefiles_files', []
)
self.object.images = form.cleaned_data.get(
'images_files', []
)
self.object.author = self.request.user
self.object.save()
@ -160,37 +163,33 @@ class Drafts(LoginRequiredMixin, View):
if draft != None:
data = json.loads(draft.data)
for k in ['official_project_documents', 'other_documents',
'shapefiles', 'images' ]:
for k in [ 'official_project_documents', 'other_documents',
'shapefiles', 'images' ]:
try:
# Filter the dictionary of form fields in the draft
# down to just the ones right the right name `k`
items = list(filter(
lambda x: (
x['name'] == '{0}_files'.format(k)
and x['value'] != ''
), data['data']['form']
))
except KeyError:
continue
keyname = k + '_files'
field = data['data']['form'][keyname]
# Split the comma-separated list of IDs into a list
try:
items = items[0]['value'].split(',')
except IndexError:
continue
# Delete those items
for item in items:
try:
f = File.objects.get(id=item)
if f.user != self.request.user:
continue
f.delete()
except File.DoesNotExist:
# Ignore empty fields
if field['value'] == '':
continue
file_list = json.loads(field['value'])
# Delete those items
for item in file_list:
try:
f = File.objects.get(id=item['id'])
if f.user != self.request.user:
continue
f.delete()
except File.DoesNotExist:
continue
except:
continue
draft.delete()
return HttpResponse(status=204)