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:
@ -19,6 +19,9 @@ class BaseFile(models.Model):
|
||||
def __str__(self):
|
||||
return self.file.name
|
||||
|
||||
def name(self):
|
||||
return self.file.name
|
||||
|
||||
|
||||
class File(BaseFile):
|
||||
pass
|
||||
|
@ -65,10 +65,25 @@ class MultipleFilesWidget {
|
||||
}
|
||||
|
||||
rebuildListFromField() {
|
||||
let idList = this.element.field.value.split(",")
|
||||
for (let id of idList) {
|
||||
this.addFile(id, `Saved upload (id ${id})`, this.status.DONE)
|
||||
const fieldContents = this.element.field.value
|
||||
|
||||
// Nothing to do
|
||||
if (fieldContents === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
let list = JSON.parse(fieldContents)
|
||||
for (let file of list) {
|
||||
this.addFile(file.id, file.name, this.status.DONE)
|
||||
}
|
||||
} catch (e) {
|
||||
// Not JSON, let's parse as CSV
|
||||
for (let id of fieldContents.split(",")) {
|
||||
this.addFile(id, `Saved upload (id ${id})`, this.status.DONE)
|
||||
}
|
||||
}
|
||||
|
||||
this.viewFileList()
|
||||
}
|
||||
|
||||
@ -145,9 +160,13 @@ class MultipleFilesWidget {
|
||||
|
||||
updateFormField() {
|
||||
let oldVal = this.element.field.value
|
||||
this.element.field.value = this.fileList.filter(f => f.id != null)
|
||||
.map(f => f.id)
|
||||
.toString()
|
||||
let fileList = this.fileList.filter(f => f.id != null)
|
||||
.map(f => ({
|
||||
id: f.id,
|
||||
name: f.name
|
||||
}))
|
||||
|
||||
this.element.field.value = JSON.stringify(fileList)
|
||||
|
||||
// Mark form as dirty
|
||||
if (this.element.field.value !== oldVal) {
|
||||
|
Reference in New Issue
Block a user