From c7f52b1674e121cec10cdedac3fd9afe11d0cff1 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Thu, 1 Mar 2018 11:05:02 +0000
Subject: [PATCH] Enable progressing the application using the form

---
 .../templates/activity/include/comment_form.html |  4 ++--
 opentech/apply/funds/forms.py                    | 12 +++++++++++-
 .../templates/funds/includes/progress_form.html  |  4 ++--
 opentech/apply/funds/views.py                    | 16 ++++++++++++----
 4 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/opentech/apply/activity/templates/activity/include/comment_form.html b/opentech/apply/activity/templates/activity/include/comment_form.html
index 06a11b9a9..c20300aec 100644
--- a/opentech/apply/activity/templates/activity/include/comment_form.html
+++ b/opentech/apply/activity/templates/activity/include/comment_form.html
@@ -1,5 +1,5 @@
-<form method="post">
+<form method="post" id="comment-form">
     {% csrf_token %}
     {{ comment_form }}
-    <input type="submit" value="Comment">
+    <input id="comment-form-submit" name="form-submitted" type="submit" form="comment-form" value="Comment">
 </form>
diff --git a/opentech/apply/funds/forms.py b/opentech/apply/funds/forms.py
index 478f3a9ed..e4a66f7c9 100644
--- a/opentech/apply/funds/forms.py
+++ b/opentech/apply/funds/forms.py
@@ -4,9 +4,19 @@ from .models import ApplicationSubmission
 
 
 class ProgressSubmissionForm(forms.ModelForm):
+    action = forms.ChoiceField()
+
     class Meta:
         model = ApplicationSubmission
-        fields = ('status',)
+        fields = []
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
+        choices = [(action, action) for action in self.instance.phase.action_names]
+        self.fields['action'].choices = choices
+        self.fields['action'].label = self.instance.phase.name
+
+    def save(self, *args, **kwargs):
+        new_phase = self.instance.workflow.process(self.instance.phase, self.cleaned_data['action'])
+        self.instance.status = str(new_phase)
+        return super().save(*args, **kwargs)
diff --git a/opentech/apply/funds/templates/funds/includes/progress_form.html b/opentech/apply/funds/templates/funds/includes/progress_form.html
index 434c6d783..29a256a5a 100644
--- a/opentech/apply/funds/templates/funds/includes/progress_form.html
+++ b/opentech/apply/funds/templates/funds/includes/progress_form.html
@@ -1,5 +1,5 @@
-<form method="post">
+<form method="post" id="progress-form">
     {% csrf_token %}
     {{ progress_form }}
-    <input type="submit" value="Progress">
+    <input id="progress-form-submit" name="form-submitted" type="submit" form="progress-form" value="Progress">
 </form>
diff --git a/opentech/apply/funds/views.py b/opentech/apply/funds/views.py
index de22ef1bd..3a1f60d24 100644
--- a/opentech/apply/funds/views.py
+++ b/opentech/apply/funds/views.py
@@ -53,12 +53,20 @@ class ProgressContextMixin:
 
 
 class ProgressSubmissionView(DelegatedViewMixin, UpdateView):
-     form_class = ProgressSubmissionForm
-     context_name = 'progress_form'
+    model = ApplicationSubmission
+    form_class = ProgressSubmissionForm
+    context_name = 'progress_form'
+
+    def form_valid(self, form):
+        return super().form_valid(form)
 
 
 class SubmissionDetailView(CommentContextMixin, ProgressContextMixin, DetailView):
     model = ApplicationSubmission
+    form_views = {
+        'progress': ProgressSubmissionView,
+        'comment': CommentFormView,
+    }
 
     def get_context_data(self, **kwargs):
         return super().get_context_data(
@@ -75,8 +83,8 @@ class SubmissionDetailView(CommentContextMixin, ProgressContextMixin, DetailView
         kwargs['template_names'] = self.get_template_names()
         kwargs['context'] = self.get_context_data()
 
-        import pudb; pudb.set_trace()
-        view = CommentFormView.as_view()
+        form_submitted = request.POST['form-submitted'].lower()
+        view = self.form_views[form_submitted].as_view()
 
         return view(request, *args, **kwargs)
 
-- 
GitLab