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

Open
wants to merge 3 commits into
base: 2.3.0
Choose a base branch
from
Open
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
26 changes: 13 additions & 13 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(fettchImportUrlsSuccess(importUrls))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please fix my typo? fettch -> fetch

})

return dispatch(action)
.catch(error => dispatch(fetchImportUrlsError(error)))
Expand All @@ -109,7 +109,7 @@ export function fetchImportUrlsInit() {
}

export function fettchImportUrlsSuccess(importUrls) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same typo as above. Please fix it.

return {type: FETCH_IMPORT_URLS_SUCCESS, 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
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
8 changes: 7 additions & 1 deletion rdmo/projects/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ def get_view(self, view_uri):

class RDMOXMLImport(Import):

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

def check(self):
file_type, encoding = mimetypes.guess_type(self.file_name)
Expand Down Expand Up @@ -231,6 +234,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 @@ -378,7 +378,10 @@ def test_upload_accept(db, client, username, password):

if password:
assert response.status_code == 200
assert response.json() == '.xml'
assert response.json() == {
'application/xml': ['.xml'],
'text/xml': ['.xml']
}
else:
assert response.status_code == 401

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

from django.conf import settings
Expand Down Expand Up @@ -175,10 +176,13 @@ 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)
for key, values in import_plugin.accept.items():
accept[key].update(values)
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
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