Skip to content

Commit ae887db

Browse files
committed
use plugins instead of OptionSet.provider
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
1 parent 8b1e226 commit ae887db

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

rdmo/options/models.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from rdmo.conditions.models import Condition
1010
from rdmo.core.models import TranslationMixin
11-
from rdmo.core.plugins import get_plugin
1211
from rdmo.core.utils import join_url
1312

1413

@@ -87,20 +86,16 @@ def label(self) -> str:
8786
return self.uri
8887

8988
@property
90-
def provider(self) -> list:
91-
return get_plugin('OPTIONSET_PROVIDERS', self.provider_key)
92-
93-
@property
94-
def has_provider(self) -> bool:
95-
return self.provider is not None
89+
def has_plugins(self) -> bool:
90+
return self.plugins.exists()
9691

9792
@property
9893
def has_search(self) -> bool:
99-
return self.has_provider and self.provider.search
94+
return self.has_plugins and any(i.has_search for i in self.plugins.all())
10095

10196
@property
10297
def has_refresh(self) -> bool:
103-
return self.has_provider and self.provider.refresh
98+
return self.has_plugins and any(i.has_refresh for i in self.plugins.all())
10499

105100
@property
106101
def has_conditions(self) -> bool:

rdmo/projects/serializers/v1/page.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Meta:
4949
'uri',
5050
'model',
5151
'options',
52-
'has_provider',
52+
'has_plugins',
5353
'has_search',
5454
'has_refresh',
5555
'has_conditions'

rdmo/projects/viewsets.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -261,21 +261,28 @@ def options(self, request, pk=None):
261261

262262
# check if the optionset belongs to this catalog and if it has a provider
263263
project.catalog.prefetch_elements()
264-
if Question.objects.filter_by_catalog(project.catalog).filter(optionsets=optionset) and \
265-
optionset.provider is not None:
264+
if (
265+
Question.objects.filter_by_catalog(project.catalog).filter(optionsets=optionset)
266+
and optionset.has_plugins
267+
):
266268
options = []
267-
for option in optionset.provider.get_options(project, search=request.GET.get('search'),
268-
user=request.user, site=request.site):
269-
if 'id' not in option:
270-
raise RuntimeError(f"'id' is missing in options of '{optionset.provider.class_name}'")
271-
elif 'text' not in option:
272-
raise RuntimeError(f"'text' is missing in options of '{optionset.provider.class_name}'")
273-
if 'text_and_help' not in option:
274-
if 'help' in option:
275-
option['text_and_help'] = '{text} [{help}]'.format(**option)
276-
else:
277-
option['text_and_help'] = '{text}'.format(**option)
278-
options.append(option)
269+
for plugin in optionset.plugins.all():
270+
provider = plugin.initialize_class()
271+
if provider is None:
272+
continue # skip when plugin class initialization fails
273+
274+
for option in provider.get_options(project, search=request.GET.get('search'),
275+
user=request.user, site=request.site):
276+
if 'id' not in option:
277+
raise RuntimeError(f"'id' is missing in options of '{provider.class_name}'")
278+
elif 'text' not in option:
279+
raise RuntimeError(f"'text' is missing in options of '{provider.class_name}'")
280+
if 'text_and_help' not in option:
281+
if 'help' in option:
282+
option['text_and_help'] = '{text} [{help}]'.format(**option)
283+
else:
284+
option['text_and_help'] = '{text}'.format(**option)
285+
options.append(option)
279286

280287
return Response(options)
281288

0 commit comments

Comments
 (0)