Skip to content

Commit edf2834

Browse files
committed
Changed draft submission field validation taking place in clean() to associate the errors with the field in question, rather than raising them as general form errors.
- Legacy-Id: 17249
1 parent 76ee1b8 commit edf2834

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

ietf/submit/forms.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright The IETF Trust 2011-2019, All Rights Reserved
1+
# Copyright The IETF Trust 2011-2020, All Rights Reserved
22
# -*- coding: utf-8 -*-
33

44

@@ -160,7 +160,8 @@ def format_messages(where, e, log):
160160
continue
161161
self.file_types.append('.%s' % ext)
162162
if not ('.txt' in self.file_types or '.xml' in self.file_types):
163-
raise forms.ValidationError('Unexpected submission file types; found %s, but %s is required' % (', '.join(self.file_types), ' or '.join(self.base_formats)))
163+
if not self.errors:
164+
raise forms.ValidationError('Unexpected submission file types; found %s, but %s is required' % (', '.join(self.file_types), ' or '.join(self.base_formats)))
164165

165166
#debug.show('self.cleaned_data["xml"]')
166167
if self.cleaned_data.get('xml'):
@@ -187,14 +188,14 @@ def format_messages(where, e, log):
187188
self.xmlroot = self.xmltree.getroot()
188189
xml_version = self.xmlroot.get('version', '2')
189190
except Exception as e:
190-
raise forms.ValidationError("An exception occurred when trying to [arse the XML file: %s" % e)
191+
self.add_error('xml', "An exception occurred when trying to parse the XML file: %s" % e)
191192

192193
draftname = self.xmlroot.attrib.get('docName')
193194
if draftname is None:
194-
raise forms.ValidationError("No docName attribute found in the xml root element")
195+
self.add_error('xml', "No docName attribute found in the xml root element")
195196
name_error = validate_submission_name(draftname)
196197
if name_error:
197-
raise forms.ValidationError(name_error)
198+
self.add_error('xml', name_error)
198199
revmatch = re.search("-[0-9][0-9]$", draftname)
199200
if revmatch:
200201
self.revision = draftname[-2:]
@@ -230,10 +231,10 @@ def format_messages(where, e, log):
230231
prep.options.accept_prepped = True
231232
self.xmltree.tree = prep.prep()
232233
if self.xmltree.tree == None:
233-
raise forms.ValidationError("Error from xml2rfc (prep): %s" % prep.errors)
234+
self.add_error('xml', "Error from xml2rfc (prep): %s" % prep.errors)
234235
except Exception as e:
235236
msgs = format_messages('prep', e, xml2rfc.log)
236-
raise forms.ValidationError(msgs)
237+
self.add_error('xml', msgs)
237238

238239
# --- Convert to txt ---
239240
if not ('txt' in self.cleaned_data and self.cleaned_data['txt']):
@@ -255,9 +256,9 @@ def format_messages(where, e, log):
255256
except Exception as e:
256257
msgs = format_messages('txt', e, xml2rfc.log)
257258
log.log('\n'.join(msgs))
258-
raise forms.ValidationError(msgs)
259+
self.add_error('xml', msgs)
259260

260-
# --- Convert to xml ---
261+
# --- Convert to html ---
261262
if xml_version == '3':
262263
try:
263264
file_name['html'] = os.path.join(settings.IDSUBMIT_STAGING_PATH, '%s-%s.html' % (self.filename, self.revision))
@@ -272,7 +273,7 @@ def format_messages(where, e, log):
272273
xml_version))
273274
except Exception as e:
274275
msgs = format_messages('html', e, xml2rfc.log)
275-
raise forms.ValidationError(msgs)
276+
self.add_error('xml', msgs)
276277

277278
if xml_version == '2':
278279
ok, errors = self.xmltree.validate()
@@ -289,7 +290,7 @@ def format_messages(where, e, log):
289290
# line: the line at which the message originated (if applicable)
290291
# column: the character column at which the message originated (if applicable)
291292
# filename: the name of the file in which the message originated (if applicable)
292-
raise forms.ValidationError(
293+
self.add_error('xml',
293294
[ forms.ValidationError("One or more XML validation errors occurred when processing the XML file:") ] +
294295
[ forms.ValidationError("%s: Line %s: %s" % (xml_file.name, r.line, r.message), code="%s"%r.type) for r in errors ]
295296
)
@@ -306,24 +307,29 @@ def format_messages(where, e, log):
306307
try:
307308
text = bytes.decode(self.file_info['txt'].charset)
308309
except (UnicodeDecodeError, LookupError) as e:
309-
raise forms.ValidationError('Failed decoding the uploaded file: "%s"' % str(e))
310+
self.add_error('txt', 'Failed decoding the uploaded file: "%s"' % str(e))
310311
#
311312
self.parsed_draft = Draft(text, txt_file.name)
312313
if self.filename == None:
313314
self.filename = self.parsed_draft.filename
314315
elif self.filename != self.parsed_draft.filename:
315-
raise forms.ValidationError("Inconsistent name information: xml:%s, txt:%s" % (self.filename, self.parsed_draft.filename))
316+
self.add_error('txt', "Inconsistent name information: xml:%s, txt:%s" % (self.filename, self.parsed_draft.filename))
316317
if self.revision == None:
317318
self.revision = self.parsed_draft.revision
318319
elif self.revision != self.parsed_draft.revision:
319-
raise forms.ValidationError("Inconsistent revision information: xml:%s, txt:%s" % (self.revision, self.parsed_draft.revision))
320+
self.add_error('txt', "Inconsistent revision information: xml:%s, txt:%s" % (self.revision, self.parsed_draft.revision))
320321
if self.title == None:
321322
self.title = self.parsed_draft.get_title()
322323
elif self.title != self.parsed_draft.get_title():
323-
raise forms.ValidationError("Inconsistent title information: xml:%s, txt:%s" % (self.title, self.parsed_draft.get_title()))
324+
self.add_error('txt', "Inconsistent title information: xml:%s, txt:%s" % (self.title, self.parsed_draft.get_title()))
325+
326+
# The following errors are likely noise if we have previous field
327+
# errors:
328+
if self.errors:
329+
raise forms.ValidationError('')
324330

325331
if not self.filename:
326-
raise forms.ValidationError("Could not extract a valid draft name from the upload"
332+
raise forms.ValidationError("Could not extract a valid draft name from the upload. "
327333
"To fix this in a text upload, please make sure that the full draft name including "
328334
"revision number appears centered on its own line below the document title on the "
329335
"first page. In an xml upload, please make sure that the top-level <rfc/> "

0 commit comments

Comments
 (0)