From 76e2c958b555b165eaab16dfb8c633cf87e5da5b Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Wed, 20 Feb 2019 10:41:09 +0100
Subject: [PATCH] Add dashboard for organizations; redirect contact there upon
 login

---
 organizations/decorators.py                   | 14 +++++++
 .../templates/organizations/dashboard.html    | 41 +++++++++++++++++++
 organizations/urls.py                         |  5 +++
 organizations/views.py                        | 20 +++++++++
 scipost/forms.py                              |  4 +-
 scipost/templates/scipost/navbar.html         |  6 +--
 scipost/views.py                              |  3 +-
 7 files changed, 87 insertions(+), 6 deletions(-)
 create mode 100644 organizations/decorators.py
 create mode 100644 organizations/templates/organizations/dashboard.html

diff --git a/organizations/decorators.py b/organizations/decorators.py
new file mode 100644
index 000000000..c517f1665
--- /dev/null
+++ b/organizations/decorators.py
@@ -0,0 +1,14 @@
+__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
+__license__ = "AGPL v3"
+
+
+from .models import Contact
+
+
+def has_contact(user):
+    """Requires user to be related to a Contact."""
+    try:
+        user.org_contact
+        return True
+    except Contact.DoesNotExist:
+        return False
diff --git a/organizations/templates/organizations/dashboard.html b/organizations/templates/organizations/dashboard.html
new file mode 100644
index 000000000..8c9bd6e8e
--- /dev/null
+++ b/organizations/templates/organizations/dashboard.html
@@ -0,0 +1,41 @@
+{% extends 'organizations/base.html' %}
+
+{% load bootstrap %}
+
+{% block pagetitle %}: organizations dashboard{% endblock pagetitle %}
+
+{% block content %}
+
+<div class="row">
+    <div class="col-12">
+        <h1 class="highlight">Welcome to your Organizations dashboard, {{ request.user.org_contact.get_title_display }} {{ request.user.last_name }}</h1>
+    </div>
+</div>
+
+<div class="row">
+  <div class="col-12">
+    <h3>Your Organizations-related roles:</h3>
+    <table class="table">
+      <tr>
+	<th>Organization</th>
+	<th>Role kind</th>
+	<th>Date from</th>
+	<th>Date until</th>
+      </tr>
+      {% for role in roles %}
+      <tr>
+	<td><a href="{{ role.organization.get_absolute_url }}">{{ role.organization }}</a></td>
+	<td>{{ role.get_kind_display }}</td>
+	<td>{{ role.date_from|date:"Y-m-d" }}</td>
+	<td>{{ role.date_until|date:"Y-m-d" }}</td>
+      </tr>
+      {% empty %}
+      <tr>
+	<td>No role has been defined</td>
+      </tr>
+      {% endfor %}
+    </table>
+  </div>
+</div>
+
+{% endblock content %}
diff --git a/organizations/urls.py b/organizations/urls.py
index 5ad00b7c7..4e8dcb3a2 100644
--- a/organizations/urls.py
+++ b/organizations/urls.py
@@ -32,4 +32,9 @@ urlpatterns = [
         views.OrganizationDetailView.as_view(),
         name='organization_details'
     ),
+    url(
+        r'^dashboard/$',
+        views.dashboard,
+        name='dashboard'
+    ),
 ]
diff --git a/organizations/views.py b/organizations/views.py
index 7ad4bd007..0997a0f08 100644
--- a/organizations/views.py
+++ b/organizations/views.py
@@ -2,12 +2,16 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
 __license__ = "AGPL v3"
 
 
+from django.contrib.auth.decorators import login_required
 from django.core.urlresolvers import reverse_lazy
+from django.shortcuts import render
 from django.utils import timezone
 from django.views.generic.detail import DetailView
 from django.views.generic.edit import CreateView, UpdateView, DeleteView
 from django.views.generic.list import ListView
 
+from guardian.decorators import permission_required
+
 from .constants import ORGTYPE_PRIVATE_BENEFACTOR
 from .models import Organization
 
@@ -92,3 +96,19 @@ class OrganizationDetailView(DetailView):
         if not self.request.user.has_perm('scipost.can_manage_organizations'):
             queryset = queryset.exclude(orgtype=ORGTYPE_PRIVATE_BENEFACTOR)
         return queryset
+
+
+@login_required
+def dashboard(request):
+    """
+    Administration page for Organization Contacts.
+
+    This page is meant as a personal page for Contacts, where they will for example be able
+    to read their personal data and agreements.
+    """
+    context = {}
+    try:
+        context['roles'] = request.user.org_contact.roles.all()
+    except:
+        pass
+    return render(request, 'organizations/dashboard.html', context)
diff --git a/scipost/forms.py b/scipost/forms.py
index eb2fd287d..5e6c95dba 100644
--- a/scipost/forms.py
+++ b/scipost/forms.py
@@ -34,7 +34,7 @@ from .models import Contributor, DraftInvitation, UnavailabilityPeriod, \
 
 from affiliations.models import Affiliation, Institution
 from common.forms import MonthYearWidget, ModelChoiceFieldwithid
-from partners.decorators import has_contact
+from organizations.decorators import has_contact
 
 from colleges.models import Fellowship, PotentialFellowshipEvent
 from commentaries.models import Commentary
@@ -343,7 +343,7 @@ class AuthenticationForm(forms.Form):
             if has_contributor(request.user):
                 return reverse_lazy('scipost:personal_page')
             elif has_contact(request.user):
-                return reverse_lazy('partners:dashboard')
+                return reverse_lazy('organizations:dashboard')
             else:
                 return reverse_lazy('scipost:index')
         return redirect_to
diff --git a/scipost/templates/scipost/navbar.html b/scipost/templates/scipost/navbar.html
index e364be488..eb1054765 100644
--- a/scipost/templates/scipost/navbar.html
+++ b/scipost/templates/scipost/navbar.html
@@ -63,9 +63,9 @@
                         <a class="nav-link" href="{% url 'scipost:personal_page' %}">Personal Page</a>
                       </li>
                   {% endif %}
-                  {% if user.partner_contact %}
-                      <li class="nav-item{% if '/partners/dashboard' in request.path %} active{% endif %}">
-                        <a class="nav-link" href="{% url 'partners:dashboard' %}">Partner Page</a>
+                  {% if user.org_contact %}
+                      <li class="nav-item{% if '/organizations/dashboard' in request.path %} active{% endif %}">
+                        <a class="nav-link" href="{% url 'organizations:dashboard' %}">Orgs dashboard</a>
                       </li>
                   {% endif %}
               {% else %}
diff --git a/scipost/views.py b/scipost/views.py
index efdec24ed..f75b4e7b5 100644
--- a/scipost/views.py
+++ b/scipost/views.py
@@ -401,7 +401,8 @@ def login_view(request):
                                       '(our admins will verify your credentials very soon)'))
         elif form.user_is_inactive():
             form.add_error(None, ('Your account is not yet activated. '
-                                  'Please first activate your account.'))
+                                  'Please first activate your account by clicking on the '
+                                  'activation link we emailed you.'))
         else:
             form.add_error(None, 'Invalid username/password.')
     context = {'form': form}
-- 
GitLab