Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to display the modified version of an upload in the forms #114

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion osis_document/contrib/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def __init__(self, **kwargs):
self.post_process_params = kwargs.pop('post_process_params', None)
self.with_cropping = kwargs.pop('with_cropping', False)
self.cropping_options = kwargs.pop('cropping_options', None)
self.for_modified_upload = kwargs.pop('for_modified_upload', False)
kwargs.setdefault(
'widget',
FileUploadWidget(
Expand All @@ -101,6 +102,7 @@ def __init__(self, **kwargs):
post_process_params=self.post_process_params,
with_cropping=self.with_cropping,
cropping_options=self.cropping_options,
for_modified_upload=self.for_modified_upload,
),
)
base_field = TokenField(
Expand Down Expand Up @@ -146,5 +148,14 @@ def prepare_value(self, value):
if isinstance(value, list):
from osis_document.api.utils import get_remote_token

return [get_remote_token(v, write_token=True) if is_uuid(v) else v for v in value]
return [
get_remote_token(
v,
write_token=True,
for_modified_upload=self.for_modified_upload
)
if is_uuid(v)
else v
for v in value
]
return value
12 changes: 11 additions & 1 deletion osis_document/contrib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self, **kwargs):
self.post_process_params = kwargs.pop('post_process_params', None)
self.with_cropping = kwargs.pop('with_cropping', False)
self.cropping_options = kwargs.pop('cropping_options', None)
self.for_modified_upload = kwargs.pop('for_modified_upload', False)
if kwargs.get('size', None) is None:
kwargs['size'] = 0
super().__init__(widget=forms.TextInput, **kwargs)
Expand Down Expand Up @@ -156,5 +157,14 @@ def format_value(self, values):

return filter(
None,
[get_remote_token(value, write_token=True) if isinstance(value, uuid.UUID) else value for value in values],
[
get_remote_token(
value,
write_token=True,
for_modified_upload=self.for_modified_upload,
)
if isinstance(value, uuid.UUID)
else value
for value in values
],
)
4 changes: 2 additions & 2 deletions osis_document/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_model_form_submit(
get_remote_metadata,
):
# For the sake of simplicity, let's say a remote confirm is local
confirm_remote_upload.side_effect = lambda token, upload_to, **_: confirm_upload(token, upload_to)
confirm_remote_upload.side_effect = lambda token, upload_to, **_: str(confirm_upload(token, upload_to))

token = WriteTokenFactory()
get_remote_metadata.return_value = {
Expand Down Expand Up @@ -238,7 +238,7 @@ def test_create_from_uuid_saving(
instance = TestDocument(documents=[token.token])
with patch('osis_document.api.utils.confirm_remote_upload') as confirm_remote_upload:
# For the sake of simplicity, let's say a remote confirm is local
confirm_remote_upload.side_effect = lambda token, upload_to, **_: confirm_upload(token, upload_to)
confirm_remote_upload.side_effect = lambda token, upload_to, **_: str(confirm_upload(token, upload_to))
instance.save()

self.assertEqual(len(instance.documents), 1)
Expand Down
46 changes: 45 additions & 1 deletion osis_document/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from django import forms
from django.test import TestCase, override_settings
from osis_document.contrib.forms import FileUploadField, TokenField
from osis_document.tests.factories import WriteTokenFactory
from osis_document.tests.factories import WriteTokenFactory, PdfUploadFactory, ModifiedUploadFactory


@override_settings(OSIS_DOCUMENT_BASE_URL='http://dummyurl.com/document/')
Expand Down Expand Up @@ -271,3 +271,47 @@ class TestForm(forms.Form):

self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data.get('media'), ['a:token'])


@patch('osis_document.api.utils.get_remote_token')
def test_for_modified_upload(self, get_remote_token):
get_remote_token.return_value = 'a:token'

class TestFormWithModifiedUpload(forms.Form):
media = FileUploadField(for_modified_upload=True)

upload = PdfUploadFactory()
modified_upload = ModifiedUploadFactory(upload=upload)

form = TestFormWithModifiedUpload(
initial={
'media': [upload.uuid],
},
)

form.as_p()

get_remote_token.assert_called_once_with(
upload.uuid,
write_token=True,
for_modified_upload=True,
)

get_remote_token.reset_mock()

class TestFormWithoutModifiedUpload(forms.Form):
media = FileUploadField()

form = TestFormWithoutModifiedUpload(
initial={
'media': [upload.uuid],
},
)

form.as_p()

get_remote_token.assert_called_once_with(
upload.uuid,
write_token=True,
for_modified_upload=False,
)
17 changes: 17 additions & 0 deletions osis_document/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ def test_widget_should_not_expose_uuid(self, mock_remote_token):
render = widget.render('foo', [stub_uuid])
self.assertNotIn(str(stub_uuid), render)
self.assertIn('data-values="some:token"', render)
mock_remote_token.assert_called_once_with(
stub_uuid,
write_token=True,
for_modified_upload=False,
)

@patch('osis_document.api.utils.get_remote_token')
def test_widget_with_modified_upload(self, mock_remote_token):
mock_remote_token.return_value = 'some:token'
widget = FileUploadWidget(size=2, for_modified_upload=True)
stub_uuid = PdfUploadFactory().uuid
widget.render('foo', [stub_uuid])
mock_remote_token.assert_called_once_with(
stub_uuid,
write_token=True,
for_modified_upload=True,
)

def test_widget_renders_attributes(self):
widget = FileUploadWidget(size=1, mimetypes=['application/pdf'])
Expand Down
1 change: 1 addition & 0 deletions osis_document/tests/views/test_request_upload_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def test_request_upload_with_bad_extension(self):
response = self.client.post(resolve_url('request-upload'), {'file': file})
self.assertEqual(400, response.status_code)

@override_settings(ENABLE_MIMETYPE_VALIDATION=True)
def test_request_upload_with_mime_smuggling(self):
file = ContentFile(SMALLEST_PDF, 'foo.doc')
response = self.client.post(resolve_url('request-upload'), {'file': file})
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

setup(
name='OSIS Document',
version='0.10.5',
version='0.10.6',
description='Document management API and widget',
url='http://github.com/uclouvain/osis-document',
author='Université catholique de Louvain',
Expand Down
Loading