From 1fe6635778495f8c3dc454a7f3f9a789f70aaf0c Mon Sep 17 00:00:00 2001
From: Dan Braghis <dan.braghis@torchbox.com>
Date: Tue, 19 Jun 2018 14:10:20 +0100
Subject: [PATCH] Rename all fields

---
 opentech/apply/determinations/forms.py        | 36 +++++++++----------
 .../migrations/0005_rename_fields.py          | 28 +++++++++++++++
 opentech/apply/determinations/models.py       | 22 ++++++------
 .../determinations/determination_detail.html  |  4 +--
 .../templatetags/determination_tags.py        | 10 +++---
 .../apply/determinations/tests/factories.py   | 14 ++++----
 .../apply/determinations/tests/test_views.py  |  6 ++--
 opentech/apply/determinations/views.py        | 30 ++++++++--------
 8 files changed, 87 insertions(+), 63 deletions(-)
 create mode 100644 opentech/apply/determinations/migrations/0005_rename_fields.py

diff --git a/opentech/apply/determinations/forms.py b/opentech/apply/determinations/forms.py
index 1ef0848bf..2ee93b73e 100644
--- a/opentech/apply/determinations/forms.py
+++ b/opentech/apply/determinations/forms.py
@@ -3,7 +3,7 @@ from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
 from django_fsm import can_proceed
 
 from opentech.apply.funds.workflow import DETERMINATION_RESPONSE_TRANSITIONS
-from .models import Determination, DETERMINATION_CHOICES, UNDETERMINED, UNAPPROVED, APPROVED
+from .models import Determination, DETERMINATION_CHOICES, NEEDS_MORE_INFO, REJECTED, ACCEPTED
 
 from opentech.apply.utils.options import RICH_TEXT_WIDGET
 
@@ -44,7 +44,7 @@ class BaseDeterminationForm(forms.ModelForm):
                 field.required = False
 
     def get_initial_for_field(self, field, field_name):
-        if field_name == 'determination':
+        if field_name == 'outcome':
             action_name = self.request.GET.get('action')
             if action_name:
                 return self.get_determination_from_action_name(action_name)
@@ -55,8 +55,8 @@ class BaseDeterminationForm(forms.ModelForm):
         # update the instance data prior to validating uniqueness
         self.instance.submission = self.submission
         self.instance.author = self.request.user
-        self.instance.determination_data = {key: value for key, value in self.cleaned_data.items()
-                                            if key not in ['determination', 'determination_message']}
+        self.instance.data = {key: value for key, value in self.cleaned_data.items()
+                                            if key not in ['outcome', 'message']}
 
         try:
             self.instance.validate_unique()
@@ -68,7 +68,7 @@ class BaseDeterminationForm(forms.ModelForm):
 
         if self.draft_button_name not in self.data:
             action_name = self.request.GET.get('action') or \
-                self.get_action_name_from_determination(int(cleaned_data['determination']))
+                self.get_action_name_from_determination(int(cleaned_data['outcome']))
             if action_name:
                 transition = self.submission.get_transition(action_name)
                 if not can_proceed(transition):
@@ -80,8 +80,8 @@ class BaseDeterminationForm(forms.ModelForm):
         return cleaned_data
 
     def save(self, commit=True):
-        self.instance.determination = int(self.cleaned_data['determination'])
-        self.instance.determination_message = self.cleaned_data['determination_message']
+        self.instance.outcome = int(self.cleaned_data['outcome'])
+        self.instance.message = self.cleaned_data['message']
         self.instance.is_draft = self.draft_button_name in self.data
 
         if self.transition and not self.instance.is_draft:
@@ -93,18 +93,18 @@ class BaseDeterminationForm(forms.ModelForm):
     def get_determination_from_action_name(self, action_name):
         if action_name in DETERMINATION_RESPONSE_TRANSITIONS:
             if 'more_info' in action_name:
-                return UNDETERMINED
+                return NEEDS_MORE_INFO
             elif 'accepted' in action_name:
-                return APPROVED
-        return UNAPPROVED
+                return ACCEPTED
+        return REJECTED
 
     def get_action_name_from_determination(self, determination):
         action_name = None
 
         suffix = 'more_info'
-        if determination == APPROVED:
+        if determination == ACCEPTED:
             suffix = 'accepted'
-        elif determination == UNAPPROVED:
+        elif determination == REJECTED:
             suffix = 'rejected'
 
         # Use get_available_status_transitions()?
@@ -117,12 +117,12 @@ class BaseDeterminationForm(forms.ModelForm):
 
 
 class ConceptDeterminationForm(BaseDeterminationForm):
-    determination = forms.ChoiceField(
+    outcome = forms.ChoiceField(
         choices=DETERMINATION_CHOICES,
         label='Determination',
         help_text='Do you recommend requesting a proposal based on this concept note?',
     )
-    determination_message = RichTextField(
+    message = RichTextField(
         label='Determination message',
         help_text='This text will be e-mailed to the applicant. '
         'Ones when text is first added and then every time the text is changed.'
@@ -188,19 +188,19 @@ class ProposalDeterminationForm(BaseDeterminationForm):
 
     # A. Determination
 
-    determination = forms.ChoiceField(
+    outcome = forms.ChoiceField(
         choices=DETERMINATION_CHOICES,
         label='Determination',
         help_text='Do you recommend requesting a proposal based on this concept note?'
     )
-    determination.group = 1
+    outcome.group = 1
 
-    determination_message = RichTextField(
+    message = RichTextField(
         label='Determination message',
         help_text='This text will be e-mailed to the applicant. '
         'Ones when text is first added and then every time the text is changed.'
     )
-    determination_message.group = 1
+    message.group = 1
 
     # B. General thoughts
     liked = RichTextField(
diff --git a/opentech/apply/determinations/migrations/0005_rename_fields.py b/opentech/apply/determinations/migrations/0005_rename_fields.py
new file mode 100644
index 000000000..3c87c7983
--- /dev/null
+++ b/opentech/apply/determinations/migrations/0005_rename_fields.py
@@ -0,0 +1,28 @@
+# Generated by Django 2.0.2 on 2018-06-19 13:01
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('determinations', '0004_determination_tweaks'),
+    ]
+
+    operations = [
+        migrations.RenameField(
+            model_name='determination',
+            old_name='determination_data',
+            new_name='data',
+        ),
+        migrations.RenameField(
+            model_name='determination',
+            old_name='determination_message',
+            new_name='message',
+        ),
+        migrations.RenameField(
+            model_name='determination',
+            old_name='determination',
+            new_name='outcome',
+        ),
+    ]
diff --git a/opentech/apply/determinations/models.py b/opentech/apply/determinations/models.py
index 101c05f60..ed01749ac 100644
--- a/opentech/apply/determinations/models.py
+++ b/opentech/apply/determinations/models.py
@@ -8,14 +8,14 @@ from django.utils.translation import ugettext_lazy as _
 
 from opentech.apply.activity.models import Activity
 
-UNAPPROVED = 0
-UNDETERMINED = 1
-APPROVED = 2
+REJECTED = 0
+NEEDS_MORE_INFO = 1
+ACCEPTED = 2
 
 DETERMINATION_CHOICES = (
-    (UNAPPROVED, _('Rejected')),
-    (UNDETERMINED, _('Needs more info')),
-    (APPROVED, _('Accepted')),
+    (REJECTED, _('Rejected')),
+    (NEEDS_MORE_INFO, _('Needs more info')),
+    (ACCEPTED, _('Accepted')),
 )
 
 
@@ -30,9 +30,9 @@ class Determination(models.Model):
         on_delete=models.PROTECT,
     )
 
-    determination = models.IntegerField(verbose_name=_("Determination"), choices=DETERMINATION_CHOICES, default=1)
-    determination_message = models.TextField(verbose_name=_("Determination message"), blank=True)
-    determination_data = JSONField(blank=True)
+    outcome = models.IntegerField(verbose_name=_("Determination"), choices=DETERMINATION_CHOICES, default=1)
+    message = models.TextField(verbose_name=_("Determination message"), blank=True)
+    data = JSONField(blank=True)
     is_draft = models.BooleanField(default=False, verbose_name=_("Draft"))
     created_at = models.DateTimeField(verbose_name=_('Creation time'), auto_now_add=True)
     updated_at = models.DateTimeField(verbose_name=_('Update time'), auto_now=True)
@@ -44,13 +44,13 @@ class Determination(models.Model):
         return reverse('apply:submissions:determinations:detail', args=(self.id,))
 
     def submitted(self):
-        return self.determination != UNDETERMINED and not self.is_draft
+        return self.outcome != NEEDS_MORE_INFO and not self.is_draft
 
     def __str__(self):
         return f'Determination for {self.submission.title} by {self.author!s}'
 
     def __repr__(self):
-        return f'<{self.__class__.__name__}: {str(self.determination_data)}>'
+        return f'<{self.__class__.__name__}: {str(self.data)}>'
 
 
 @receiver(post_save, sender=Determination)
diff --git a/opentech/apply/determinations/templates/determinations/determination_detail.html b/opentech/apply/determinations/templates/determinations/determination_detail.html
index 2fdd06b9d..dd7be238d 100644
--- a/opentech/apply/determinations/templates/determinations/determination_detail.html
+++ b/opentech/apply/determinations/templates/determinations/determination_detail.html
@@ -12,8 +12,8 @@
 <div class="grid">
     <div>
         <h5>Determination</h5>
-        <p>{{ determination.get_determination_display }}</p>
-        {{ determination.determination_message|bleach }}
+        <p>{{ determination.get_outcome_display }}</p>
+        {{ determination.message|bleach }}
     </div>
 </div>
 
diff --git a/opentech/apply/determinations/templatetags/determination_tags.py b/opentech/apply/determinations/templatetags/determination_tags.py
index 812b3eade..54dedb5b7 100644
--- a/opentech/apply/determinations/templatetags/determination_tags.py
+++ b/opentech/apply/determinations/templatetags/determination_tags.py
@@ -1,7 +1,7 @@
 from django import template
 from django.core.exceptions import ObjectDoesNotExist
 
-from opentech.apply.determinations.models import UNDETERMINED
+from opentech.apply.determinations.models import NEEDS_MORE_INFO
 from opentech.apply.funds.workflow import DETERMINATION_PHASES
 
 register = template.Library()
@@ -21,9 +21,7 @@ def can_add_determination(user, submission):
     - there was not determination response (i.e. accepted / rejected)
     """
     try:
-        has_determination_response = submission.determination \
-            and not submission.determination.is_draft \
-            and submission.determination.determination != UNDETERMINED
+        has_determination_response = submission.determination.submitted
     except ObjectDoesNotExist:
         has_determination_response = False
 
@@ -52,8 +50,8 @@ def pending_determination(submission, user):
     if submission.status in DETERMINATION_PHASES:
         try:
             return not submission.determination \
-                or (submission.determination.is_draft and not user.is_apply_staff) \
-                or submission.determination.determination == UNDETERMINED
+                   or (submission.determination.is_draft and not user.is_apply_staff) \
+                   or submission.determination.outcome == NEEDS_MORE_INFO
         except ObjectDoesNotExist:
             return True
 
diff --git a/opentech/apply/determinations/tests/factories.py b/opentech/apply/determinations/tests/factories.py
index 5a3c5f9d4..44ffcc160 100644
--- a/opentech/apply/determinations/tests/factories.py
+++ b/opentech/apply/determinations/tests/factories.py
@@ -2,7 +2,7 @@ import factory
 
 from opentech.apply.funds.tests.factories import ApplicationSubmissionFactory
 
-from ..models import Determination, APPROVED, UNDETERMINED, UNAPPROVED
+from ..models import Determination, ACCEPTED, NEEDS_MORE_INFO, REJECTED
 from ..views import get_form_for_stage
 
 
@@ -24,16 +24,16 @@ class DeterminationFactory(factory.DjangoModelFactory):
         model = Determination
 
     class Params:
-        submitted = factory.Trait(determination=APPROVED, is_draft=False)
-        approved = factory.Trait(determination=APPROVED)
-        rejected = factory.Trait(determination=UNAPPROVED)
+        submitted = factory.Trait(outcome=ACCEPTED, is_draft=False)
+        accepted = factory.Trait(outcome=ACCEPTED)
+        rejected = factory.Trait(outcome=REJECTED)
         not_draft = factory.Trait(is_draft=False)
 
     submission = factory.SubFactory(ApplicationSubmissionFactory)
     author = factory.SelfAttribute('submission.lead')
 
-    determination = UNDETERMINED
-    determination_message = factory.Faker('sentence')
-    determination_data = factory.Dict({'submission': factory.SelfAttribute('..submission')}, dict_factory=DeterminationDataFactory)
+    outcome = NEEDS_MORE_INFO
+    message = factory.Faker('sentence')
+    data = factory.Dict({'submission': factory.SelfAttribute('..submission')}, dict_factory=DeterminationDataFactory)
 
     is_draft = True
diff --git a/opentech/apply/determinations/tests/test_views.py b/opentech/apply/determinations/tests/test_views.py
index 7c88c1079..4da1d3457 100644
--- a/opentech/apply/determinations/tests/test_views.py
+++ b/opentech/apply/determinations/tests/test_views.py
@@ -74,21 +74,21 @@ class DeterminationFormTestCase(BaseTestCase):
     def test_cant_resubmit_determination(self):
         submission = ApplicationSubmissionFactory(status='in_discussion', lead=self.user)
         determination = DeterminationFactory(submission=submission, author=self.user, submitted=True)
-        response = self.post_page(submission, {'data': 'value', 'determination': determination.determination}, 'form')
+        response = self.post_page(submission, {'data': 'value', 'determination': determination.outcome}, 'form')
         self.assertTrue(response.context['has_determination_response'])
         self.assertContains(response, 'You have already added a determination for this submission')
 
     def test_can_edit_draft_determination(self):
         submission = ApplicationSubmissionFactory(status='in_discussion', lead=self.user)
         determination = DeterminationFactory(submission=submission, author=self.user)
-        response = self.post_page(submission, {'data': 'value', 'determination': determination.determination}, 'form')
+        response = self.post_page(submission, {'data': 'value', 'determination': determination.outcome}, 'form')
         self.assertFalse(response.context['has_determination_response'])
         self.assertEqual(response.context['title'], 'Update Determination draft')
 
     def test_cannot_edit_draft_determination_if_not_lead(self):
         submission = ApplicationSubmissionFactory(status='in_discussion')
         determination = DeterminationFactory(submission=submission, author=self.user)
-        response = self.post_page(submission, {'data': 'value', 'determination': determination.determination}, 'form')
+        response = self.post_page(submission, {'data': 'value', 'determination': determination.outcome}, 'form')
         self.assertEqual(response.status_code, 403)
 
 
diff --git a/opentech/apply/determinations/views.py b/opentech/apply/determinations/views.py
index 540345085..b108b39fb 100644
--- a/opentech/apply/determinations/views.py
+++ b/opentech/apply/determinations/views.py
@@ -12,7 +12,7 @@ from opentech.apply.funds.models import ApplicationSubmission
 from opentech.apply.funds.workflow import DETERMINATION_PHASES
 
 from .forms import ConceptDeterminationForm, ProposalDeterminationForm
-from .models import Determination, UNDETERMINED
+from .models import Determination, NEEDS_MORE_INFO
 
 
 def get_form_for_stage(submission):
@@ -66,8 +66,8 @@ class DeterminationCreateOrUpdateView(CreateOrUpdateView):
 
     def get_context_data(self, **kwargs):
         try:
-            has_determination_response = self.submission.determination.determination != UNDETERMINED \
-                and not self.submission.determination.is_draft
+            has_determination_response = self.submission.determination.outcome != NEEDS_MORE_INFO \
+                                         and not self.submission.determination.is_draft
         except ObjectDoesNotExist:
             has_determination_response = False
 
@@ -87,9 +87,9 @@ class DeterminationCreateOrUpdateView(CreateOrUpdateView):
         kwargs['submission'] = self.submission
 
         if self.object:
-            kwargs['initial'] = self.object.determination_data
-            kwargs['initial']['determination'] = self.object.determination
-            kwargs['initial']['determination_message'] = self.object.determination_message
+            kwargs['initial'] = self.object.data
+            kwargs['initial']['outcome'] = self.object.outcome
+            kwargs['initial']['message'] = self.object.message
 
         return kwargs
 
@@ -115,28 +115,26 @@ class DeterminationDetailView(DetailView):
         return super().dispatch(request, *args, **kwargs)
 
     def get_context_data(self, **kwargs):
-        determination_data = self.get_object().determination_data
-        form_used = get_form_for_stage(self.get_object().submission)
-        form_determination_data = {}
+        determination = self.get_object()
+        form_used = get_form_for_stage(determination.submission)
+        form_data = {}
 
         for name, field in form_used.base_fields.items():
             try:
                 # Add any titles that exist
                 title = form_used.titles[field.group]
-                form_determination_data.setdefault(title, '')
+                form_data.setdefault(title, '')
             except AttributeError:
                 pass
 
             try:
-                value = determination_data[name]
-                form_determination_data.setdefault(field.label, str(value))
+                value = determination.data[name]
+                form_data.setdefault(field.label, str(value))
             except KeyError:
                 pass
 
-        lead_or_admin = self.request.user.is_superuser or self.request.user == self.get_object().submission.lead
-
         return super().get_context_data(
-            can_view_extended_data=lead_or_admin,
-            determination_data=form_determination_data,
+            can_view_extended_data=determination.submission.user_lead_or_admin(self.request.user),
+            determination_data=form_data,
             **kwargs
         )
-- 
GitLab