Skip to content

Commit

Permalink
Refactor accept field in import plugins to match react-dropzone
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Nov 19, 2024
1 parent c3d133d commit 4973fbb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
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
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

0 comments on commit 4973fbb

Please sign in to comment.