diff --git a/hypha/apply/determinations/forms.py b/hypha/apply/determinations/forms.py index 1a8a6c82395f41d8863342f286c291704ccadfcb..3e8e32518d0f88b68419ce5f1797d35e0111a38d 100644 --- a/hypha/apply/determinations/forms.py +++ b/hypha/apply/determinations/forms.py @@ -449,6 +449,8 @@ class DeterminationModelForm(StreamBaseForm, forms.ModelForm, metaclass=MixedMet super().__init__(*args, initial=initial, instance=instance, **kwargs) for field in self._meta.widgets: + # Need to disable the model form fields as these fields would be + # rendered via streamfield form. self.fields[field].disabled = True if self.draft_button_name in self.data: @@ -477,6 +479,7 @@ class DeterminationModelForm(StreamBaseForm, forms.ModelForm, metaclass=MixedMet self.instance.message = self.cleaned_data[self.instance.message_field.id] try: self.instance.outcome = int(self.cleaned_data[self.instance.determination_field.id]) + # Need to catch KeyError as outcome field would not exist in case of edit. except KeyError: pass self.instance.is_draft = self.draft_button_name in self.data diff --git a/hypha/apply/determinations/models.py b/hypha/apply/determinations/models.py index 0e2dfe9668e5bd47205c3dc1ed35d5dce9e85a24..4b72205b31d620685f81b43a0add72709f63ffd3 100644 --- a/hypha/apply/determinations/models.py +++ b/hypha/apply/determinations/models.py @@ -97,7 +97,11 @@ class Determination(DeterminationFormFieldsMixin, AccessFormData, models.Model): outcome = models.IntegerField(verbose_name=_("Determination"), choices=DETERMINATION_CHOICES, default=1) message = models.TextField(verbose_name=_("Determination message"), blank=True) + + # Stores old determination forms data data = JSONField(blank=True, null=True) + + # Stores data submitted via streamfield determination forms form_data = JSONField(default=dict, encoder=DjangoJSONEncoder) is_draft = models.BooleanField(default=False, verbose_name=_("Draft")) created_at = models.DateTimeField(verbose_name=_('Creation time'), auto_now_add=True) @@ -130,9 +134,23 @@ class Determination(DeterminationFormFieldsMixin, AccessFormData, models.Model): def __repr__(self): return f'<{self.__class__.__name__}: {str(self.form_data)}>' + @property + def use_new_determination_form(self): + """ + Checks if a submission has the new streamfield determination form + attached to it and along with that it also verify that if self.data is None. + + self.data would be set as None for the determination which are created using + streamfield determination forms. + + But old lab forms can be edited to add new determination forms + so we need to use old determination forms for already submitted determination. + """ + return self.submission.is_determination_form_attached and self.data is None + @property def detailed_data(self): - if not self.submission.is_determination_form_attached: + if not self.use_new_determination_form: from .views import get_form_for_stage return get_form_for_stage(self.submission).get_detailed_response(self.data) return self.get_detailed_response() diff --git a/hypha/apply/determinations/views.py b/hypha/apply/determinations/views.py index 15029846aded56072e70137ed1b340c4976fe303..cb65e3a343060faf686405d2a917b7e59acd412c 100644 --- a/hypha/apply/determinations/views.py +++ b/hypha/apply/determinations/views.py @@ -141,6 +141,14 @@ class BatchDeterminationCreateView(BaseStreamForm, CreateView): return kwargs def check_all_submissions_are_of_same_type(self, submissions): + """ + Checks if all the submission as the new determination form attached to it. + + Or all should be using the old determination forms. + + We can not create batch determination with submissions using two different + type of forms. + """ return len(set( [ submission.is_determination_form_attached @@ -156,11 +164,15 @@ class BatchDeterminationCreateView(BaseStreamForm, CreateView): " - please contact admin" ) if not submissions[0].is_determination_form_attached: + # If all the submission has same type of forms but they are not the + # new streamfield forms then use the old determination forms. return get_form_for_stages(submissions) 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 is already set in case of batch determinations so we do + # not need to render this field. form_fields.pop(field_block.id) return type('WagtailStreamForm', (self.submission_form_class,), form_fields) @@ -315,6 +327,7 @@ class DeterminationCreateOrUpdateView(BaseStreamForm, CreateOrUpdateView): def get_form_class(self): if not self.submission.is_determination_form_attached: + # If new determination forms are not attached use the old ones. return get_form_for_stage(self.submission) form_fields = self.get_form_fields() field_blocks = self.get_defined_fields() @@ -323,6 +336,7 @@ class DeterminationCreateOrUpdateView(BaseStreamForm, CreateOrUpdateView): outcome_choices = outcome_choices_for_phase( self.submission, self.request.user ) + # Outcome field choices need to be set according to the phase. form_fields[field_block.id].choices = outcome_choices return type('WagtailStreamForm', (self.submission_form_class,), form_fields) @@ -552,12 +566,14 @@ class DeterminationEditView(BaseStreamForm, UpdateView): def get_form_class(self): determination = self.get_object() - if not determination.submission.is_determination_form_attached: + if not determination.use_new_determination_form: return get_form_for_stage(determination.submission) 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 can not be edited after being set once, so we do not + # need to render this field. form_fields.pop(field_block.id) return type('WagtailStreamForm', (self.submission_form_class,), form_fields) diff --git a/hypha/apply/funds/models/submissions.py b/hypha/apply/funds/models/submissions.py index 1a434fc56080793594c8926e47f7614b65203263..1fcdbfb45516c761968c0983cc2dfc4fd6bb4377 100644 --- a/hypha/apply/funds/models/submissions.py +++ b/hypha/apply/funds/models/submissions.py @@ -538,6 +538,15 @@ class ApplicationSubmission( @property def is_determination_form_attached(self): + """ + We use old django determination forms but now as we are moving + to streamfield determination forms which can be created and attached + to funds in admin. + + This method checks if there are new determination forms attached to the + submission or we would still use the old determination forms for backword + compatiablity. + """ return self.get_from_parent('determination_forms').count() > 0 def progress_application(self, **kwargs):