Skip to content
Snippets Groups Projects
Commit 3f7f105e authored by Shrikrishna Singh's avatar Shrikrishna Singh Committed by Fredrik Jonsson
Browse files

Change DeterminationBlock choices based on phase and pop while edit

parent 43f33ae6
No related branches found
No related tags found
No related merge requests found
......@@ -58,16 +58,13 @@ class DeterminationModelForm(StreamBaseForm, forms.ModelForm, metaclass=MixedMet
for field in self._meta.widgets:
self.fields[field].disabled = True
# TODO need to find this field on runtime
# self.fields['8ab49696-f8ee-4810-bb60-551c000127c9'].choices = self.outcome_choices_for_phase(submission, user)
# self.fields['outcome'].choices = self.outcome_choices_for_phase(submission, user)
self.fields['outcome'].choices = self.outcome_choices_for_phase(submission, user)
if self.draft_button_name in self.data:
for field in self.fields.values():
field.required = False
if edit:
# TODO also need to pop the stream field for outcome
self.fields.pop('outcome')
self.draft_button_name = None
......
......@@ -38,12 +38,16 @@ from .utils import (
has_final_determination,
outcome_from_actions,
transition_from_outcome,
determination_actions
)
from .options import (
NEEDS_MORE_INFO,
TRANSITION_DETERMINATION,
DETERMINATION_CHOICES
)
from .blocks import DeterminationBlock
def get_form_for_stages(submissions):
forms = [
......@@ -83,6 +87,25 @@ def get_fields_for_stage(submission):
return forms[0].form.form_fields
def outcome_choices_for_phase(submission, user):
"""
Outcome choices correspond to Phase transitions.
We need to filter out non-matching choices.
i.e. a transition to In Review is not a determination, while Needs more info or Rejected are.
"""
available_choices = set()
choices = dict(DETERMINATION_CHOICES)
for transition_name in determination_actions(user, submission):
try:
determination_type = TRANSITION_DETERMINATION[transition_name]
except KeyError:
pass
else:
available_choices.add((determination_type, choices[determination_type]))
return available_choices
@method_decorator(staff_required, name='dispatch')
class DeterminationCreateOrUpdateView(BaseStreamForm, CreateOrUpdateView):
submission_form_class = DeterminationModelForm
......@@ -139,6 +162,17 @@ class DeterminationCreateOrUpdateView(BaseStreamForm, CreateOrUpdateView):
kwargs['action'] = self.request.GET.get('action')
return kwargs
def get_form_class(self):
form_fields = self.get_form_fields()
field_blocks = self.get_defined_fields()
for field_block in field_blocks:
if isinstance(field_block.block, DeterminationBlock):
outcome_choices = outcome_choices_for_phase(
self.submission, self.request.user
)
form_fields[field_block.id].choices = outcome_choices
return type('WagtailStreamForm', (self.submission_form_class,), form_fields)
def get_success_url(self):
return self.submission.get_absolute_url()
......@@ -490,6 +524,14 @@ class DeterminationEditView(BaseStreamForm, UpdateView):
kwargs['initial'] = self.object.data
return kwargs
def get_form_class(self):
form_fields = self.get_form_fields()
field_blocks = self.get_defined_fields()
for field_block in field_blocks:
if isinstance(field_block.block, DeterminationBlock):
form_fields.pop(field_block.id)
return type('WagtailStreamForm', (self.submission_form_class,), form_fields)
def form_valid(self, form):
super().form_valid(form)
determination = self.get_object()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment