From ad302ad392bb5670eb2fd33225d3ef5f6a462bda Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Sat, 3 Nov 2018 17:00:16 +0100
Subject: [PATCH] Add make email primary in profile card

---
 profiles/forms.py                              |  3 ++-
 profiles/templates/profiles/_profile_card.html |  5 +++--
 profiles/urls.py                               |  5 +++++
 profiles/views.py                              | 18 +++++++++++++++---
 4 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/profiles/forms.py b/profiles/forms.py
index ce6b1a515..10c3b586c 100644
--- a/profiles/forms.py
+++ b/profiles/forms.py
@@ -91,7 +91,8 @@ class ProfileMergeForm(forms.Form):
         # Model fields:
         if profile_to_merge.expertises:
             for expertise in profile_to_merge.expertises:
-                profile_to_merge_into.expertises.add(expertise)
+                if expertise not in profile_to_merge_into.expertises:
+                    profile_to_merge_into.expertises.append(expertise)
         if profile_to_merge.orcid_id and (profile_to_merge_into.orcid_id is None):
             profile_to_merge_into.orcid_id = profile_to_merge.orcid_id
         if profile_to_merge.webpage and (profile_to_merge_into.webpage is None):
diff --git a/profiles/templates/profiles/_profile_card.html b/profiles/templates/profiles/_profile_card.html
index fa8d437ba..e7599098e 100644
--- a/profiles/templates/profiles/_profile_card.html
+++ b/profiles/templates/profiles/_profile_card.html
@@ -32,8 +32,9 @@
 
                       </td>
                       <td class="d-flex">
-                          <form method="post" action="{% url 'profiles:toggle_email_status' profile_mail.id %}">{% csrf_token %}<button type="submit" class="btn btn-link p-0">{{ profile_mail.still_valid|yesno:'Deprecate,Mark valid' }}</button></form>
-                          <form method="post" action="{% url 'profiles:delete_profile_email' profile_mail.id %}">{% csrf_token %}<button type="submit" class="btn btn-link text-danger p-0 ml-2" onclick="return confirm('Sure you want to delete {{ profile_mail.email }}?')"><i class="fa fa-trash"></i></button></form>
+                        <form method="post" action="{% url 'profiles:toggle_email_status' profile_mail.id %}">{% csrf_token %}<button type="submit" class="btn btn-link">{{ profile_mail.still_valid|yesno:'Deprecate,Mark valid' }}</button></form>
+			<form method="post" action="{% url 'profiles:email_make_primary' profile_mail.id %}">{% csrf_token %}<button type="submit" class="btn btn-link">Make primary</button></form>
+                        <form method="post" action="{% url 'profiles:delete_profile_email' profile_mail.id %}">{% csrf_token %}<button type="submit" class="btn btn-link text-danger ml-2" onclick="return confirm('Sure you want to delete {{ profile_mail.email }}?')"><i class="fa fa-trash"></i></button></form>
                       </td>
                   </tr>
     	      {% endfor %}
diff --git a/profiles/urls.py b/profiles/urls.py
index 2a571772e..9d693d9f8 100644
--- a/profiles/urls.py
+++ b/profiles/urls.py
@@ -57,6 +57,11 @@ urlpatterns = [
         views.add_profile_email,
         name='add_profile_email'
     ),
+    url(
+        r'^emails/(?P<email_id>[0-9]+)/make_primary$',
+        views.email_make_primary,
+        name='email_make_primary'
+    ),
     url(
         r'^emails/(?P<email_id>[0-9]+)/toggle$',
         views.toggle_email_status,
diff --git a/profiles/views.py b/profiles/views.py
index 737eb6afe..49ef5a114 100644
--- a/profiles/views.py
+++ b/profiles/views.py
@@ -4,6 +4,7 @@ __license__ = "AGPL v3"
 
 from django.contrib import messages
 from django.core.urlresolvers import reverse, reverse_lazy
+from django.db import transaction
 from django.db.models import Q, Count
 from django.db.models.functions import Concat
 from django.shortcuts import get_object_or_404, render, redirect
@@ -249,9 +250,6 @@ class ProfileDuplicateListView(PermissionsMixin, ListView):
             nr_count=Count('full_name')
         ).filter(nr_count__gt=1)
         queryset = profiles.filter(full_name__in=[item['full_name'] for item in duplicates])
-        # duplicates = Profile.objects.values('last_name').annotate(
-        #     nr=Count('last_name')).filter(nr__gt=1)
-        # queryset = Profile.objects.filter(last_name__in=[item['last_name'] for item in duplicates])
         return queryset
 
     def get_context_data(self, *args, **kwargs):
@@ -260,6 +258,7 @@ class ProfileDuplicateListView(PermissionsMixin, ListView):
         return context
 
 
+@transaction.atomic
 @permission_required('scipost.can_create_profiles')
 def profile_merge(request):
     """
@@ -302,6 +301,19 @@ def add_profile_email(request, profile_id):
     return redirect(reverse('profiles:profiles'))
 
 
+@require_POST
+@permission_required('scipost.can_create_profiles')
+def email_make_primary(request, email_id):
+    """
+    Make this email the primary one for this Profile.
+    """
+    profile_email = get_object_or_404(ProfileEmail, pk=email_id)
+    ProfileEmail.objects.filter(profile=profile_email.profile).update(primary=False)
+    profile_email.primary = True
+    profile_email.save()
+    return redirect(profile_email.profile.get_absolute_url())
+
+
 @require_POST
 @permission_required('scipost.can_create_profiles')
 def toggle_email_status(request, email_id):
-- 
GitLab