From 7d49901d00175bbfdb59e5566a97ed7f707e19d2 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Tue, 19 Feb 2019 10:24:58 +0100
Subject: [PATCH] Add link of ProspectivePartner to Organization

---
 .../organizations/organization_list.html      |  1 +
 organizations/views.py                        |  4 +-
 partners/forms.py                             |  8 ++++
 partners/templates/partners/dashboard.html    |  3 ++
 .../prospartner_link_organization.html        | 43 +++++++++++++++++++
 partners/urls.py                              |  3 ++
 partners/views.py                             | 26 ++++++++++-
 7 files changed, 86 insertions(+), 2 deletions(-)
 create mode 100644 partners/templates/partners/prospartner_link_organization.html

diff --git a/organizations/templates/organizations/organization_list.html b/organizations/templates/organizations/organization_list.html
index 0077f4117..f238fc47c 100644
--- a/organizations/templates/organizations/organization_list.html
+++ b/organizations/templates/organizations/organization_list.html
@@ -32,6 +32,7 @@ $(document).ready(function($) {
     <ul>
       <li><a href="{% url 'organizations:organization_create' %}">Create a new Organization instance</a></li>
       <li><a href="{% url 'funders:funders_dashboard' %}">Link Funders to Organizations</a> ({{ nr_funders_wo_organization }} found in need of linking)</li>
+      <li><a href="{% url 'partners:prospartner_link_organization' %}">Link ProspectivePartners to Organizations</a> ({{ nr_prospartners_wo_organization }} found in need of linking)</li>
       <li>Link Partners to Organizations ({{ nr_partners_wo_organization }} found in need of linking)</li>
     </ul>
     {% endif %}
diff --git a/organizations/views.py b/organizations/views.py
index 0896d3108..7ad4bd007 100644
--- a/organizations/views.py
+++ b/organizations/views.py
@@ -12,7 +12,7 @@ from .constants import ORGTYPE_PRIVATE_BENEFACTOR
 from .models import Organization
 
 from funders.models import Funder
-from partners.models import Partner
+from partners.models import ProspectivePartner, Partner
 
 from scipost.mixins import PermissionsMixin
 
@@ -55,6 +55,8 @@ class OrganizationListView(ListView):
         context = super().get_context_data(*args, **kwargs)
         if self.request.user.has_perm('scipost.can_manage_organizations'):
             context['nr_funders_wo_organization'] = Funder.objects.filter(organization=None).count()
+            context['nr_prospartners_wo_organization'] = ProspectivePartner.objects.filter(
+                organization=None).count()
             context['nr_partners_wo_organization'] = Partner.objects.filter(organization=None).count()
         context['pubyears'] = range(int(timezone.now().strftime('%Y')), 2015, -1)
         return context
diff --git a/partners/forms.py b/partners/forms.py
index daf2ed44d..aa34fb488 100644
--- a/partners/forms.py
+++ b/partners/forms.py
@@ -452,6 +452,14 @@ class ProspectivePartnerEventForm(forms.ModelForm):
         }
 
 
+class ProspectivePartnerOrganizationSelectForm(forms.ModelForm):
+    organization = AutoCompleteSelectField('organization_lookup')
+
+    class Meta:
+        model = ProspectivePartner
+        fields = []
+
+
 class MembershipQueryForm(forms.Form):
     """
     This form is to be used by an agent of the prospective Partner,
diff --git a/partners/templates/partners/dashboard.html b/partners/templates/partners/dashboard.html
index ce7dc05d2..29f0696e8 100644
--- a/partners/templates/partners/dashboard.html
+++ b/partners/templates/partners/dashboard.html
@@ -120,6 +120,7 @@
             	<div class="col-12">
             	  <h2 class="highlight">Prospective Partners</h2>
                   <h3><a href="{% url 'partners:add_prospective_partner' %}">Add a prospective partner</a></h3>
+		  <h3><i class="fa fa-exclamation-circle text-warning"></i> Link an Organization to <a href="{% url 'partners:prospartner_link_organization' %}">the next</a> ProspectivePartner without one ({{ nr_prospartners_wo_organization }} to handle)</h3>
             	</div>
               </div>
 
@@ -129,6 +130,7 @@
                     	<th>Country</th>
                     	<th>Institution name</th>
                     	<th>Kind</th>
+			<th>Organization</th>
                     	<th>Status</th>
                     	<th>Date received</th>
                     </tr>
@@ -140,6 +142,7 @@
                     		  <td>{{ partner.get_country_display }}</td>
                     		  <td>{{ partner.institution_name }}</td>
                     		  <td>{{ partner.get_kind_display }}</td>
+				  <td>{{ partner.organization }}</td>
                     		  <td style="background-color:{{ partner.status|partnerstatuscolor }}">{{ partner.get_status_display }}</td>
                     		  <td>{{ partner.date_received|date:"Y-m-d" }}</td>
                     	</tr>
diff --git a/partners/templates/partners/prospartner_link_organization.html b/partners/templates/partners/prospartner_link_organization.html
new file mode 100644
index 000000000..4fb258224
--- /dev/null
+++ b/partners/templates/partners/prospartner_link_organization.html
@@ -0,0 +1,43 @@
+{% extends 'partners/base.html' %}
+
+{% block pagetitle %}: link ProspectivePartner to Organization{% endblock pagetitle %}
+
+{% load bootstrap %}
+
+{% block breadcrumb_items %}
+{{ block.super }}
+<span class="breadcrumb-item">{{ prospectivepartner }}</span>
+{% endblock %}
+
+{% block content %}
+
+<h1>ProspectivePartner: link to Organization</h1>
+<div class="row">
+  <div class="col-4">
+    <table class="table">
+      <tbody>
+	<tr><td>Name:</td><td>{{ prospectivepartner.institution_name }}</td></tr>
+	<tr><td>Country:</td><td>{{ prospectivepartner.country }}</td></tr>
+	<tr><td>Organization:</td><td>{{ prospectivepartner.organization }}</td></tr>
+      </tbody>
+    </table>
+  </div>
+  <div class="col-6">
+    <h3>Link to:</h3>
+    <form action="{% url 'partners:prospartner_link_organization' %}" method="post">
+      {% csrf_token %}
+      {{ form|bootstrap }}
+      <input type="submit" value="Link" class="btn btn-primary">
+    </form>
+  </div>
+  <div class="col-2">
+    <p>Can't find it in the selector? <a href="{% url 'organizations:organization_create' %}" target="_blank">Add a new organization to our database</a> (opens in new window)</p>
+  </div>
+</div>
+
+{% endblock content %}
+
+{% block footer_script %}
+{{ block.super }}
+{{ form.media }}
+{% endblock footer_script %}
diff --git a/partners/urls.py b/partners/urls.py
index 94c869e7a..f297b2058 100644
--- a/partners/urls.py
+++ b/partners/urls.py
@@ -15,6 +15,9 @@ urlpatterns = [
     # Prospects
     url(r'^prospects/add$', views.add_prospective_partner,
         name='add_prospective_partner'),
+    url(r'^prospects/prospartner_link_organization/$',
+        views.LinkProspectivePartnerToOrganizationView.as_view(),
+        name='prospartner_link_organization'),
     url(r'^prospects/contacts/(?P<contact_id>[0-9]+)/email$',
         views.email_prospartner_contact, name='email_prospartner_contact'),
     url(r'^prospects/contacts/(?P<contact_id>[0-9]+)/email/(?P<mail>followup)$',
diff --git a/partners/views.py b/partners/views.py
index 681490166..9bc5ea824 100644
--- a/partners/views.py
+++ b/partners/views.py
@@ -29,7 +29,7 @@ from .models import Partner, ProspectivePartner, ProspectiveContact, ContactRequ
     ProspectivePartnerEvent, MembershipAgreement, Contact, PartnersAttachment
 from .forms import ProspectivePartnerForm, ProspectiveContactForm,\
     PromoteToPartnerForm,\
-    ProspectivePartnerEventForm, MembershipQueryForm,\
+    ProspectivePartnerEventForm, ProspectivePartnerOrganizationSelectForm, MembershipQueryForm,\
     PartnerForm, ContactForm, ContactFormset, ContactModelFormset,\
     NewContactForm, ActivationForm, PartnerEventForm,\
     MembershipAgreementForm, RequestContactForm, RequestContactFormSet,\
@@ -77,6 +77,8 @@ def dashboard(request):
         context['partners'] = Partner.objects.all()
         context['prospective_partners'] = ProspectivePartner.objects.order_by(
             'country', 'institution_name')
+        context['nr_prospartners_wo_organization'] = ProspectivePartner.objects.filter(
+            organization=None).count()
         context['ppevent_form'] = ProspectivePartnerEventForm()
         context['agreements'] = MembershipAgreement.objects.order_by('date_requested')
     return render(request, 'partners/dashboard.html', context)
@@ -134,6 +136,28 @@ def promote_prospartner(request, prospartner_id):
     return render(request, 'partners/promote_prospartner.html', context)
 
 
+class LinkProspectivePartnerToOrganizationView(PermissionsMixin, UpdateView):
+    """
+    For an existing ProspectivePartner instance, specify the link to an Organization.
+    """
+    permission_required = 'scipost.can_manage_organizations'
+    model = ProspectivePartner
+    form_class = ProspectivePartnerOrganizationSelectForm
+    template_name = 'partners/prospartner_link_organization.html'
+    success_url = reverse_lazy('partners:prospartner_link_organization')
+
+    def get_object(self):
+        prospectivepartner = ProspectivePartner.objects.filter(
+                organization=None).first()
+        return prospectivepartner
+
+    def form_valid(self, form):
+        form.instance.organization = form.cleaned_data['organization']
+        return super().form_valid(form)
+
+
+
+
 ###############
 # Partner views
 ###############
-- 
GitLab