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

Refactor upload accept #1156

Merged
merged 9 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 1 addition & 1 deletion rdmo/core/assets/js/components/UploadDropZone.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const UploadDropZone = ({ acceptedTypes, onImportFile }) => {
}

UploadDropZone.propTypes = {
acceptedTypes: PropTypes.arrayOf(PropTypes.string),
acceptedTypes: PropTypes.object,
onImportFile: PropTypes.func.isRequired,
}

Expand Down
28 changes: 14 additions & 14 deletions rdmo/projects/assets/js/projects/actions/projectsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export function fetchProjectsError(error) {
export function fetchCatalogs() {
return function(dispatch) {
dispatch(fetchCatalogsInit())
const action = (dispatch) => ProjectsApi.fetchCatalogs()
.then(catalogs => {
dispatch(fetchCatalogsSuccess({ catalogs }))})
const action = (dispatch) => ProjectsApi.fetchCatalogs().then(catalogs => {
dispatch(fetchCatalogsSuccess(catalogs))
})

return dispatch(action)
.catch(error => dispatch(fetchCatalogsError(error)))
Expand All @@ -71,9 +71,9 @@ export function fetchCatalogsError(error) {
export function fetchAllowedFileTypes() {
return function(dispatch) {
dispatch(fetchAllowedFileTypesInit())
const action = (dispatch) => ProjectsApi.fetchAllowedFileTypes()
.then(allowedTypes => {
dispatch(fetchAllowedFileTypesSuccess({ allowedTypes }))})
const action = (dispatch) => ProjectsApi.fetchAllowedFileTypes().then(allowedTypes => {
dispatch(fetchAllowedFileTypesSuccess(allowedTypes))
})

return dispatch(action)
.catch(error => dispatch(fetchAllowedFileTypesError(error)))
Expand All @@ -95,9 +95,9 @@ export function fetchAllowedFileTypesError(error) {
export function fetchImportUrls() {
return function(dispatch) {
dispatch(fetchImportUrlsInit())
const action = (dispatch) => ProjectsApi.fetchDirectImportUrls()
.then(importUrls => {
dispatch(fettchImportUrlsSuccess({ importUrls }))})
const action = (dispatch) => ProjectsApi.fetchDirectImportUrls().then(importUrls => {
dispatch(fetchImportUrlsSuccess(importUrls))
})

return dispatch(action)
.catch(error => dispatch(fetchImportUrlsError(error)))
Expand All @@ -108,8 +108,8 @@ export function fetchImportUrlsInit() {
return {type: FETCH_IMPORT_URLS_INIT}
}

export function fettchImportUrlsSuccess(importUrls) {
return {type: FETCH_IMPORT_URLS_SUCCESS, importUrls }
export function fetchImportUrlsSuccess(importUrls) {
return {type: FETCH_IMPORT_URLS_SUCCESS, importUrls}
}

export function fetchImportUrlsError(error) {
Expand All @@ -119,9 +119,9 @@ export function fetchImportUrlsError(error) {
export function fetchInvitations() {
return function(dispatch) {
dispatch(fetchInvitationsInit())
const action = (dispatch) => ProjectsApi.fetchInvites()
.then(invites => {
dispatch(fetchInvitationsSuccess({ invites }))})
const action = (dispatch) => ProjectsApi.fetchInvites().then(invites => {
dispatch(fetchInvitationsSuccess(invites))
})

return dispatch(action)
.catch(error => dispatch(fetchInvitationsError(error)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const ProjectImport = ({ allowedTypes, handleImport, importUrls}) => {
}

ProjectImport.propTypes = {
allowedTypes: PropTypes.arrayOf(PropTypes.string),
allowedTypes: PropTypes.object,
handleImport: PropTypes.func.isRequired,
importUrls: PropTypes.arrayOf(PropTypes.object).isRequired
}
Expand Down
16 changes: 8 additions & 8 deletions rdmo/projects/assets/js/projects/reducers/projectsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ export default function projectsReducer(state = initialState, action) {
case FETCH_PROJECTS_ERROR:
return {...state, errors: action.error.errors}
case FETCH_INVITATIONS_INIT:
return {...state, ...action.invites}
return {...state}
case FETCH_INVITATIONS_SUCCESS:
return {...state, ...action.invites}
return {...state, invites: action.invites}
case FETCH_INVITATIONS_ERROR:
return {...state, errors: action.error.errors}
case FETCH_CATALOGS_INIT:
return {...state, ...action.catalogs}
return {...state}
case FETCH_CATALOGS_SUCCESS:
return {...state, ...action.catalogs}
return {...state, catalogs: action.catalogs}
case FETCH_CATALOGS_ERROR:
return {...state, errors: action.error.errors}
case FETCH_FILETYPES_INIT:
return {...state, ...action.allowedTypes}
return {...state}
case FETCH_FILETYPES_SUCCESS:
return {...state, ...action.allowedTypes}
return {...state, allowedTypes: action.allowedTypes}
case FETCH_FILETYPES_ERROR:
return {...state, errors: action.error.errors}
case FETCH_IMPORT_URLS_INIT:
return {...state, ...action.importUrls}
return {...state}
case FETCH_IMPORT_URLS_SUCCESS:
return {...state, ...action.importUrls}
return {...state, importUrls: action.importUrls}
case FETCH_IMPORT_URLS_ERROR:
return {...state, errors: action.error.errors}
default:
Expand Down
7 changes: 6 additions & 1 deletion rdmo/projects/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ def get_view(self, view_uri):

class RDMOXMLImport(Import):

accept = '.xml'
accept = {
'application/xml': ['.xml']
}

def check(self):
file_type, encoding = mimetypes.guess_type(self.file_name)
Expand Down Expand Up @@ -231,6 +233,9 @@ def get_value(self, value_node):

class URLImport(RDMOXMLImport):

accept = False
upload = False

class Form(forms.Form):
url = forms.URLField(label=_('Import project from this URL'), required=True)

Expand Down
5 changes: 4 additions & 1 deletion rdmo/projects/tests/test_viewset_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@

if password:
assert response.status_code == 200
assert response.json() == '.xml'
assert response.json() == {

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: mysql)

test_upload_accept[author-author] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: mysql)

test_upload_accept[api-api] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: mysql)

test_upload_accept[site-site] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: mysql)

test_upload_accept[user-user] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: mysql)

test_upload_accept[guest-guest] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: mysql)

test_upload_accept[manager-manager] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: mysql)

test_upload_accept[owner-owner] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: postgres)

test_upload_accept[guest-guest] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: postgres)

test_upload_accept[site-site] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: postgres)

test_upload_accept[manager-manager] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: postgres)

test_upload_accept[owner-owner] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: postgres)

test_upload_accept[api-api] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: postgres)

test_upload_accept[author-author] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.8, DB: postgres)

test_upload_accept[user-user] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: mysql)

test_upload_accept[author-author] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: mysql)

test_upload_accept[guest-guest] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: mysql)

test_upload_accept[api-api] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: mysql)

test_upload_accept[site-site] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: mysql)

test_upload_accept[user-user] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: mysql)

test_upload_accept[manager-manager] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: mysql)

test_upload_accept[owner-owner] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: postgres)

test_upload_accept[author-author] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: postgres)

test_upload_accept[site-site] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: postgres)

test_upload_accept[owner-owner] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: postgres)

test_upload_accept[api-api] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: postgres)

test_upload_accept[guest-guest] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: postgres)

test_upload_accept[manager-manager] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }

Check failure on line 581 in rdmo/projects/tests/test_viewset_project.py

View workflow job for this annotation

GitHub Actions / Test (Python: 3.12, DB: postgres)

test_upload_accept[user-user] AssertionError: assert {'application/xml': ['.xml']} == {'application...ml': ['.xml']} Omitting 1 identical items, use -vv to show Right contains 1 more item: {'text/xml': ['.xml']} Full diff: { 'application/xml': [ '.xml', ], - 'text/xml': [ - '.xml', - ], }
'application/xml': ['.xml'],
'text/xml': ['.xml']
}
else:
assert response.status_code == 401

Expand Down
26 changes: 20 additions & 6 deletions rdmo/projects/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import mimetypes
from collections import defaultdict
from pathlib import Path

from django.conf import settings
Expand Down Expand Up @@ -278,13 +280,25 @@ def set_context_querystring_with_filter_and_page(context: dict) -> dict:


def get_upload_accept():
accept = set()
accept = defaultdict(set)
for import_plugin in get_plugins('PROJECT_IMPORTS').values():
if import_plugin.accept:
accept.add(import_plugin.accept)
else:
return None
return ','.join(accept)
if isinstance(import_plugin.accept, dict):
for mime_type, suffixes in import_plugin.accept.items():
accept[mime_type].update(suffixes)

elif isinstance(import_plugin.accept, str):
# legacy fallback for pre 2.3.0 RDMO, e.g. `accept = '.xml'`
suffix = import_plugin.accept
mime_type, encoding = mimetypes.guess_type(f'example{suffix}')
if mime_type:
accept[mime_type].update([suffix])

elif import_plugin.upload is True:
# if one of the plugins does not have the accept field, but is marked as upload plugin
# all file types are allowed
return {}

return accept


def compute_set_prefix_from_set_value(set_value, value):
Expand Down
4 changes: 3 additions & 1 deletion rdmo/projects/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ def get_context_data(self, **kwargs):
context['snapshots'] = project.snapshots.all()
context['invites'] = project.invites.all()
context['membership'] = Membership.objects.filter(project=project, user=self.request.user).first()
context['upload_accept'] = get_upload_accept()
context['upload_accept'] = ','.join([
suffix for suffixes in get_upload_accept().values() for suffix in suffixes
])
return context


Expand Down
Loading