From 08bca1f82c1eae5522265cbe664c918aa6a371a7 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Sat, 24 Jun 2017 19:59:21 +0200
Subject: [PATCH] Update personal data for Partnrs

---
 partners/templates/partners/dashboard.html    |   2 +
 .../scipost/update_personal_data.html         | 149 +++++++++---------
 scipost/views.py                              |  29 +++-
 3 files changed, 104 insertions(+), 76 deletions(-)

diff --git a/partners/templates/partners/dashboard.html b/partners/templates/partners/dashboard.html
index 596c20e84..17ae1a2f1 100644
--- a/partners/templates/partners/dashboard.html
+++ b/partners/templates/partners/dashboard.html
@@ -88,6 +88,8 @@
                 <ul class="list-group list-group-flush">
                   {% for agreement in personal_agreements %}
                       <li class="list-group-item">{% include 'partners/_agreement_card.html' with agreement=agreement %}</li>
+                  {% empty %}
+                      <li class="list-group-item">No Membership Agreements found.</li>
                   {% endfor %}
                 </ul>
           </div>
diff --git a/scipost/templates/scipost/update_personal_data.html b/scipost/templates/scipost/update_personal_data.html
index 1c3875d45..c78cffd25 100644
--- a/scipost/templates/scipost/update_personal_data.html
+++ b/scipost/templates/scipost/update_personal_data.html
@@ -6,82 +6,85 @@
 
 {% block content %}
 
-<script>
+{% if cont_form %}
+    <script>
 
-$(document).ready(function(){
+    $(document).ready(function(){
 
-  switch ($('select#id_discipline').val()) {
-    case "physics":
-      $("#id_expertises_0").closest("li").show();
-      $("#id_expertises_1").closest("li").hide();
-      $("#id_expertises_2").closest("li").hide();
-      $("#id_expertises_3").closest("li").hide();
-      break;
-    case "astrophysics":
-      $("#id_expertises_0").closest("li").hide();
-      $("#id_expertises_1").closest("li").show();
-      $("#id_expertises_2").closest("li").hide();
-      $("#id_expertises_3").closest("li").hide();
-      break;
-    case "mathematics":
-      $("#id_expertises_0").closest("li").hide();
-      $("#id_expertises_1").closest("li").hide();
-      $("#id_expertises_2").closest("li").show();
-      $("#id_expertises_3").closest("li").hide();
-      break;
-    case "computerscience":
-      $("#id_expertises_0").closest("li").hide();
-      $("#id_expertises_1").closest("li").hide();
-      $("#id_expertises_2").closest("li").hide();
-      $("#id_expertises_3").closest("li").show();
-      break;
-    default:
-      $("#id_expertises_0").closest("li").show();
-      $("#id_expertises_1").closest("li").show();
-      $("#id_expertises_2").closest("li").show();
-      $("#id_expertises_3").closest("li").show();
-      break;
-  }
+      switch ($('select#id_discipline').val()) {
+        case "physics":
+          $("#id_expertises_0").closest("li").show();
+          $("#id_expertises_1").closest("li").hide();
+          $("#id_expertises_2").closest("li").hide();
+          $("#id_expertises_3").closest("li").hide();
+          break;
+        case "astrophysics":
+          $("#id_expertises_0").closest("li").hide();
+          $("#id_expertises_1").closest("li").show();
+          $("#id_expertises_2").closest("li").hide();
+          $("#id_expertises_3").closest("li").hide();
+          break;
+        case "mathematics":
+          $("#id_expertises_0").closest("li").hide();
+          $("#id_expertises_1").closest("li").hide();
+          $("#id_expertises_2").closest("li").show();
+          $("#id_expertises_3").closest("li").hide();
+          break;
+        case "computerscience":
+          $("#id_expertises_0").closest("li").hide();
+          $("#id_expertises_1").closest("li").hide();
+          $("#id_expertises_2").closest("li").hide();
+          $("#id_expertises_3").closest("li").show();
+          break;
+        default:
+          $("#id_expertises_0").closest("li").show();
+          $("#id_expertises_1").closest("li").show();
+          $("#id_expertises_2").closest("li").show();
+          $("#id_expertises_3").closest("li").show();
+          break;
+      }
 
-  $('select#id_discipline').on('change', function() {
-    var selection = $(this).val();
-    switch(selection){
-    case "physics":
-      $("#id_expertises_0").closest("li").show();
-      $("#id_expertises_1").closest("li").hide();
-      $("#id_expertises_2").closest("li").hide();
-      $("#id_expertises_3").closest("li").hide();
-      break;
-    case "astrophysics":
-      $("#id_expertises_0").closest("li").hide();
-      $("#id_expertises_1").closest("li").show();
-      $("#id_expertises_2").closest("li").hide();
-      $("#id_expertises_3").closest("li").hide();
-      break;
-    case "mathematics":
-      $("#id_expertises_0").closest("li").hide();
-      $("#id_expertises_1").closest("li").hide();
-      $("#id_expertises_2").closest("li").show();
-      $("#id_expertises_3").closest("li").hide();
-      break;
-    case "computerscience":
-      $("#id_expertises_0").closest("li").hide();
-      $("#id_expertises_1").closest("li").hide();
-      $("#id_expertises_2").closest("li").hide();
-      $("#id_expertises_3").closest("li").show();
-      break;
-    default:
-      $("#id_expertises_0").closest("li").show();
-      $("#id_expertises_1").closest("li").show();
-      $("#id_expertises_2").closest("li").show();
-      $("#id_expertises_3").closest("li").show();
-      break;
-    }
-  });
+      $('select#id_discipline').on('change', function() {
+        var selection = $(this).val();
+        switch(selection){
+        case "physics":
+          $("#id_expertises_0").closest("li").show();
+          $("#id_expertises_1").closest("li").hide();
+          $("#id_expertises_2").closest("li").hide();
+          $("#id_expertises_3").closest("li").hide();
+          break;
+        case "astrophysics":
+          $("#id_expertises_0").closest("li").hide();
+          $("#id_expertises_1").closest("li").show();
+          $("#id_expertises_2").closest("li").hide();
+          $("#id_expertises_3").closest("li").hide();
+          break;
+        case "mathematics":
+          $("#id_expertises_0").closest("li").hide();
+          $("#id_expertises_1").closest("li").hide();
+          $("#id_expertises_2").closest("li").show();
+          $("#id_expertises_3").closest("li").hide();
+          break;
+        case "computerscience":
+          $("#id_expertises_0").closest("li").hide();
+          $("#id_expertises_1").closest("li").hide();
+          $("#id_expertises_2").closest("li").hide();
+          $("#id_expertises_3").closest("li").show();
+          break;
+        default:
+          $("#id_expertises_0").closest("li").show();
+          $("#id_expertises_1").closest("li").show();
+          $("#id_expertises_2").closest("li").show();
+          $("#id_expertises_3").closest("li").show();
+          break;
+        }
+      });
 
-});
+    });
+
+    </script>
+{% endif %}
 
-</script>
 
 <div class="row">
     <div class="col-lg-10 offset-lg-1">
@@ -89,7 +92,9 @@ $(document).ready(function(){
         <form action="{% url 'scipost:update_personal_data' %}" method="post">
             {% csrf_token %}
             {{user_form|bootstrap}}
-        	{{cont_form|bootstrap}}
+            {% if cont_form %}
+            	{{cont_form|bootstrap}}
+            {% endif %}
             <input type="submit" class="btn btn-secondary" value="Update" />
         </form>
     </div>
diff --git a/scipost/views.py b/scipost/views.py
index c70097886..27508f14c 100644
--- a/scipost/views.py
+++ b/scipost/views.py
@@ -912,7 +912,11 @@ def change_password(request):
         # Update user's session hash to stay logged in.
         update_session_auth_hash(request, request.user)
         messages.success(request, 'Your SciPost password has been successfully changed')
-        return redirect(reverse('scipost:personal_page'))
+        try:
+            request.user.contributor
+            return redirect(reverse('scipost:personal_page'))
+        except Contributor.DoesNotExist:
+            return redirect(reverse('partners:dashboard'))
     return render(request, 'scipost/change_password.html', {'form': form})
 
 
@@ -929,9 +933,19 @@ def reset_password(request):
                           post_reset_redirect=reverse('scipost:login'))
 
 
-@login_required
-@user_passes_test(has_contributor)
-def update_personal_data(request):
+def _update_personal_data_user_only(request):
+    user_form = UpdateUserDataForm(request.POST or None, instance=request.user)
+    if user_form.is_valid():
+        user_form.save()
+        messages.success(request, 'Your personal data has been updated.')
+        return redirect(reverse('partners:dashboard'))
+    context = {
+        'user_form': user_form
+    }
+    return render(request, 'scipost/update_personal_data.html', context)
+
+
+def _update_personal_data_contributor(request):
     contributor = Contributor.objects.get(user=request.user)
     user_form = UpdateUserDataForm(request.POST or None, instance=request.user)
     cont_form = UpdatePersonalDataForm(request.POST or None, instance=contributor)
@@ -948,6 +962,13 @@ def update_personal_data(request):
                   {'user_form': user_form, 'cont_form': cont_form})
 
 
+@login_required
+def update_personal_data(request):
+    if has_contributor(request.user):
+        return _update_personal_data_contributor(request)
+    return _update_personal_data_user_only(request)
+
+
 @login_required
 @user_passes_test(has_contributor)
 def claim_authorships(request):
-- 
GitLab