Skip to content

Commit

Permalink
Removed optimization in utils.validateFileIsPDF to avoid multiple v…
Browse files Browse the repository at this point in the history
…alidation because it breaks validation of several elements added in the same `REQUEST` (when using rest api for example).

See #SUP-36258
  • Loading branch information
gbastien committed Mar 29, 2024
1 parent 604ef29 commit 098f78b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Changelog
[gbastien]
- Corrected typo in french translation of `Preview`.
[gbastien]
- Removed optimization in `utils.validateFileIsPDF` to avoid multiple validation
because it breaks validation of several elements added in the same `REQUEST`
(when using rest api for example).
[gbastien]

0.66 (2024-02-27)
-----------------
Expand Down
8 changes: 0 additions & 8 deletions src/collective/iconifiedcategory/tests/test_behaviors.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,14 @@ def test_only_pdf_invariant(self):
view = self.portal.restrictedTraverse('folder_contents')
request['PUBLISHED'] = view
self.assertFalse(invariants.validate(data))
# already_validateFileIsPDF was set in REQUEST
self.assertTrue(request.get('already_validateFileIsPDF'))
self.assertFalse(invariants.validate(data))
# PDF needed
request.set('already_validateFileIsPDF', False)
category.only_pdf = True
errors = invariants.validate(data)
request.set('already_validateFileIsPDF', False)
self.assertEqual(errors[0].message, u'You must select a PDF file!')
request.form['form.widgets.file'].headers['content-type'] = 'application/pdf'
# no file in request
request.form = {}
self.assertFalse(invariants.validate(data))
request.set('already_validateFileIsPDF', False)
# editing a stored element
category.only_pdf = False
obj = api.content.create(
Expand All @@ -311,11 +305,9 @@ def test_only_pdf_invariant(self):
view = obj.restrictedTraverse('view')
request['PUBLISHED'] = view
self.assertFalse(invariants.validate(data))
request.set('already_validateFileIsPDF', False)
# PDF needed
category.only_pdf = True
errors = invariants.validate(data)
request.set('already_validateFileIsPDF', False)
self.assertEqual(errors[0].message, u'You must select a PDF file!')
obj.file = self.file_pdf
self.assertFalse(invariants.validate(data))
4 changes: 0 additions & 4 deletions src/collective/iconifiedcategory/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,6 @@ def _get_content_type(data):

# check if file contentType is PDF only if used content_category requires it
request = getRequest()
# avoid double validation
if request.get('already_validateFileIsPDF', False):
return
request.set('already_validateFileIsPDF', True)
context = data.__context__ or request.get('PUBLISHED').context
contentType = _get_content_type(data)
if contentType is not None and contentType != 'application/pdf':
Expand Down

0 comments on commit 098f78b

Please sign in to comment.