Skip to content

Commit

Permalink
Add a check for options to copy_set action
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Jan 24, 2025
1 parent 4d40b43 commit 4c0fa17
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rdmo/projects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ def check_conditions(conditions, values, set_prefix=None, set_index=None):
return True


def check_options(project, value):
# loop over all values of a question and check if value.option matches the optionsets of the question
for question in filter(lambda q: q.attribute == value.attribute, project.catalog.questions):
question_options = [
option.id
for optionset in question.optionsets.all()
for option in optionset.options.all()
]

# fail if question requires an option but value has none
if question_options and value.option is None:
return False

# fail if the value's option is not allowed for this question
if value.option is not None and value.option.id not in question_options:
return False

return True


def copy_project(project, site, owners):
from .models import Membership, Value # to prevent circular inclusion

Expand Down
5 changes: 5 additions & 0 deletions rdmo/projects/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
from .serializers.v1.page import PageSerializer
from .utils import (
check_conditions,
check_options,
compute_set_prefix_from_set_value,
copy_project,
get_contact_message,
Expand Down Expand Up @@ -600,6 +601,10 @@ def copy_set(self, request, parent_lookup_project, pk=None):
else:
value.set_prefix = compute_set_prefix_from_set_value(set_value, value)

# skip this value if value.option does not match the optionsets of it's question
if not check_options(self.project, value):
continue

# check if the value already exists, we do not consider collection_index
# since we do not want to import e.g. into partially filled checkboxes
if (value.attribute_id, value.set_prefix, value.set_index) in set_values_list:
Expand Down

0 comments on commit 4c0fa17

Please sign in to comment.