From 16ea0857b8ae443ccec52f2463bd908c1a3ef847 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Sun, 27 Oct 2019 19:30:38 +0000
Subject: [PATCH] Add download link to simplified project page

---
 .../project_simplified_detail.html            |  6 ++++
 hypha/apply/projects/urls.py                  |  2 ++
 hypha/apply/projects/views/project.py         | 28 ++++++++++++++++++-
 3 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/hypha/apply/projects/templates/application_projects/project_simplified_detail.html b/hypha/apply/projects/templates/application_projects/project_simplified_detail.html
index 40cc585fb..675d22286 100644
--- a/hypha/apply/projects/templates/application_projects/project_simplified_detail.html
+++ b/hypha/apply/projects/templates/application_projects/project_simplified_detail.html
@@ -17,6 +17,12 @@
                 <span>{{ object.submission.round }}</span>
                 <span>Lead: {{ object.lead }}</span>
             </h5>
+            <a
+                class="button button--primary simplified__button"
+                href="{% url "apply:projects:download" pk=object.pk %}"
+            >
+                Download PDF
+            </a>
         </div>
     </div>
 
diff --git a/hypha/apply/projects/urls.py b/hypha/apply/projects/urls.py
index 41d284975..75b596ba7 100644
--- a/hypha/apply/projects/urls.py
+++ b/hypha/apply/projects/urls.py
@@ -12,6 +12,7 @@ from .views import (
     ProjectDetailView,
     ProjectEditView,
     ProjectListView,
+    ProjectDetailPDFView,
     ProjectOverviewView,
     ProjectPrivateMediaView,
     ReportDetailView,
@@ -31,6 +32,7 @@ urlpatterns = [
         path('edit/', ProjectEditView.as_view(), name="edit"),
         path('documents/<int:file_pk>/', ProjectPrivateMediaView.as_view(), name="document"),
         path('contract/<int:file_pk>/', ContractPrivateMediaView.as_view(), name="contract"),
+        path('download/', ProjectDetailPDFView.as_view(), name='download'),
         path('simplified/', ProjectDetailSimplifiedView.as_view(), name='simplified'),
         path('request/', CreatePaymentRequestView.as_view(), name='request'),
     ])),
diff --git a/hypha/apply/projects/views/project.py b/hypha/apply/projects/views/project.py
index f90774d2f..657046a6c 100644
--- a/hypha/apply/projects/views/project.py
+++ b/hypha/apply/projects/views/project.py
@@ -6,7 +6,7 @@ from django.contrib.auth.mixins import UserPassesTestMixin
 from django.core.exceptions import PermissionDenied
 from django.db import transaction
 from django.db.models import Count
-from django.http import Http404
+from django.http import FileResponse, Http404
 from django.shortcuts import get_object_or_404, redirect
 from django.urls import reverse, reverse_lazy
 from django.utils import timezone
@@ -14,6 +14,7 @@ from django.utils.decorators import method_decorator
 from django.utils.functional import cached_property
 from django.utils.safestring import mark_safe
 from django.utils.translation import ugettext_lazy as _
+from django.views import View
 from django.views.generic import (
     CreateView,
     DetailView,
@@ -21,12 +22,14 @@ from django.views.generic import (
     TemplateView,
     UpdateView,
 )
+from django.views.generic.detail import SingleObjectMixin
 from django_filters.views import FilterView
 from django_tables2 import SingleTableMixin
 
 from hypha.apply.activity.messaging import MESSAGES, messenger
 from hypha.apply.activity.views import ActivityContextMixin, CommentFormView
 from hypha.apply.users.decorators import approver_required, staff_required
+from hypha.apply.utils.pdfs import make_pdf
 from hypha.apply.utils.storage import PrivateMediaView
 from hypha.apply.utils.views import DelegateableView, DelegatedViewMixin, ViewDispatcher
 
@@ -518,6 +521,29 @@ class ProjectDetailSimplifiedView(DetailView):
     template_name_suffix = '_simplified_detail'
 
 
+@method_decorator(staff_required, name='dispatch')
+class ProjectDetailPDFView(SingleObjectMixin, View):
+    model = Project
+
+    def get(self, request, *args, **kwargs):
+        self.object = self.get_object().submission
+        pdf = make_pdf(
+            title=self.object.title,
+            meta=[
+                self.object.stage,
+                self.object.page,
+                self.object.round,
+                f"Lead: { self.object.lead }",
+            ],
+            content=self.object.output_text_answers()
+        )
+        return FileResponse(
+            pdf,
+            as_attachment=True,
+            filename=self.object.title + '.pdf',
+        )
+
+
 class ProjectApprovalEditView(UpdateView):
     form_class = ProjectApprovalForm
     model = Project
-- 
GitLab