misc: create file token if it doesn't exist at all (#53432)

This commit is contained in:
Frédéric Péters 2021-04-26 20:01:00 +02:00
parent d1b0510f55
commit ae24eeb294
1 changed files with 7 additions and 7 deletions

View File

@ -760,14 +760,14 @@ class FileWithPreviewWidget(CompositeWidget):
def set_value(self, value):
self.value = value
if hasattr(self.value, 'token'):
self.get_widget('token').set_value(self.value.token)
if not get_session().get_tempfile(self.value.token):
# oops, it has a token but it's not in the session; this is
# probably because it was restored from a draft file created
# from an expired session.
if self.value:
if not hasattr(self.value, 'token') or not get_session().get_tempfile(self.value.token):
# it has no token, or its token is not in the session; this may be
# because the file value has not been created when filling a form,
# or because it was restored from a draft created from an expired
# session. Either way, create and use a new token.
self.value.token = get_session().add_tempfile(self.value, storage=self.storage).get('token')
self.get_widget('token').set_value(self.value.token)
self.get_widget('token').set_value(self.value.token)
def add_media(self):
get_response().add_javascript(['qommon.fileupload.js'])