From 7781152b5d10fde8110ded39fd9f3eacb69fc33d Mon Sep 17 00:00:00 2001 From: Todd Dembrey <todd.dembrey@torchbox.com> Date: Wed, 14 Feb 2018 12:10:44 +0000 Subject: [PATCH] Add the status and a denormed name onto the submission --- .../0022_applicationsubmission_status_name.py | 21 +++++++++++++++++++ opentech/apply/funds/models.py | 17 ++++++++++++++- opentech/apply/funds/tests/test_models.py | 8 +++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 opentech/apply/funds/migrations/0022_applicationsubmission_status_name.py diff --git a/opentech/apply/funds/migrations/0022_applicationsubmission_status_name.py b/opentech/apply/funds/migrations/0022_applicationsubmission_status_name.py new file mode 100644 index 000000000..78643214c --- /dev/null +++ b/opentech/apply/funds/migrations/0022_applicationsubmission_status_name.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.8 on 2018-02-14 12:08 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('funds', '0021_rename_workflow_field'), + ] + + operations = [ + migrations.AddField( + model_name='applicationsubmission', + name='status_name', + field=models.CharField(default='', max_length=50), + preserve_default=False, + ), + ] diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py index 9966d2969..8e7dbf33d 100644 --- a/opentech/apply/funds/models.py +++ b/opentech/apply/funds/models.py @@ -438,9 +438,24 @@ class ApplicationSubmission(WorkflowHelpers, AbstractFormSubmission): # Workflow inherited from WorkflowHelpers status = models.CharField(max_length=254) + status_name = models.CharField(max_length=50) objects = JSONOrderable.as_manager() + def save(self, *args, **kwargs): + if not self.id: + # We are creating the object default to first stage + try: + self.workflow_name = self.round.workflow_name + except AttributeError: + # We are a lab submission + self.workflow_name = self.page.workflow_name + self.status = str(self.workflow.first()) + + # Ensure the display value is up to date + self.status_name = self.workflow.current(self.status).name + return super().save(*args, **kwargs) + def get_data(self): # Updated for JSONField form_data = self.form_data @@ -454,7 +469,7 @@ class ApplicationSubmission(WorkflowHelpers, AbstractFormSubmission): # fall back to values defined on the data if item in REQUIRED_BLOCK_NAMES: return self.get_data()[item] - return super().__getattr__(item) + raise AttributeError('{} has no attribute "{}"'.format(repr(self), item)) def __str__(self): return str(super().__str__()) diff --git a/opentech/apply/funds/tests/test_models.py b/opentech/apply/funds/tests/test_models.py index 91d41aad8..9f4e443ac 100644 --- a/opentech/apply/funds/tests/test_models.py +++ b/opentech/apply/funds/tests/test_models.py @@ -216,6 +216,14 @@ class TestFormSubmission(TestCase): except AttributeError: return page.serve(request) + def test_workflow_and_status_assigned(self): + self.submit_form() + submission = ApplicationSubmission.objects.first() + first_phase = self.round_page.workflow.first() + self.assertEqual(submission.workflow_name, self.round_page.workflow_name) + self.assertEqual(submission.status, str(first_phase)) + self.assertEqual(submission.status_name, first_phase.name) + def test_can_submit_if_new(self): self.submit_form() -- GitLab