From 53c9edfc71f1afc45b7e3d3abe9b605f241ea825 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Thu, 23 Nov 2023 12:30:21 +0100
Subject: [PATCH] move red flags & nominations when merging profiles add
 merging profile / contributor permssions

---
 scipost_django/profiles/forms.py                 | 12 +++++++++++-
 scipost_django/profiles/views.py                 |  8 ++++++--
 .../commands/add_groups_and_permissions.py       | 16 ++++++++++++++++
 scipost_django/scipost/views.py                  |  8 ++++----
 4 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/scipost_django/profiles/forms.py b/scipost_django/profiles/forms.py
index 92d6cd4fb..bfe378501 100644
--- a/scipost_django/profiles/forms.py
+++ b/scipost_django/profiles/forms.py
@@ -225,12 +225,22 @@ class ProfileMergeForm(forms.Form):
         profile_old.publicationauthorstable_set.all().update(profile=profile)
 
         # Move all invitations to the "new" profile
-        profile_old.refereeinvitation_set.all().update(profile=profile)
+        profile_old.refereeinvitation_set.all().update(
+            profile=profile,
+            referee=getattr(profile, "contributor", None)
+            or getattr(profile_old, "contributor", None),
+        )
         profile_old.registrationinvitation_set.all().update(profile=profile)
 
         # Move all PotentialFellowships to the "new" profile
         profile_old.potentialfellowship_set.all().update(profile=profile)
 
+        # Move all RedFlags to the "new" profile
+        profile.red_flags.add(*profile_old.red_flags.all())
+
+        # Move all Nomination instances to the "new" profile
+        profile.fellowship_nominations.add(*profile_old.fellowship_nominations.all())
+
         profile_old.delete()
         return Profile.objects.get(
             id=profile.id
diff --git a/scipost_django/profiles/views.py b/scipost_django/profiles/views.py
index 88989f6ab..1c0a05e8b 100644
--- a/scipost_django/profiles/views.py
+++ b/scipost_django/profiles/views.py
@@ -5,6 +5,7 @@ __license__ = "AGPL v3"
 from functools import reduce
 import re
 from django.contrib import messages
+from django.contrib.auth.decorators import login_required
 from django.contrib.auth.mixins import UserPassesTestMixin
 from django.urls import reverse, reverse_lazy
 from django.db import transaction
@@ -319,6 +320,8 @@ class ProfileListView(PermissionsMixin, PaginationMixin, ListView):
         return context
 
 
+@login_required
+@permission_required("scipost.can_merge_profiles")
 def profile_duplicates(request):
     """
     List Profiles with potential duplicates; allow to merge if necessary.
@@ -332,7 +335,7 @@ def profile_duplicates(request):
 
 @transaction.atomic
 @permission_required_htmx(
-    "scipost.can_create_profiles",
+    "scipost.can_merge_profiles",
     "You do not have permission to create profiles.",
 )
 def _hx_profile_mark_non_duplicate(request, profile1: int, profile2: int):
@@ -349,7 +352,7 @@ def _hx_profile_mark_non_duplicate(request, profile1: int, profile2: int):
 
 @transaction.atomic
 @permission_required_htmx(
-    "scipost.can_create_profiles",
+    "scipost.can_merge_profiles",
     "You do not have permission to create profiles.",
 )
 def _hx_profile_merge(request, to_merge: int, to_merge_into: int):
@@ -384,6 +387,7 @@ def _hx_profile_merge(request, to_merge: int, to_merge_into: int):
     return render(request, "profiles/_hx_profile_merge.html", context)
 
 
+@permission_required("scipost.can_merge_profiles")
 def _hx_profile_comparison(request):
     if request.method == "GET":
         try:
diff --git a/scipost_django/scipost/management/commands/add_groups_and_permissions.py b/scipost_django/scipost/management/commands/add_groups_and_permissions.py
index e57c7baca..3ff2dc980 100644
--- a/scipost_django/scipost/management/commands/add_groups_and_permissions.py
+++ b/scipost_django/scipost/management/commands/add_groups_and_permissions.py
@@ -117,6 +117,18 @@ class Command(BaseCommand):
             content_type=content_type,
         )
 
+        # Profile Management
+        can_merge_profiles, created = Permission.objects.get_or_create(
+            codename="can_merge_profiles",
+            name="Can merge Profiles",
+            content_type=content_type,
+        )
+        can_merge_contributors, created = Permission.objects.get_or_create(
+            codename="can_merge_contributors",
+            name="Can merge Contributors",
+            content_type=content_type,
+        )
+
         # Communications
         can_email_group_members, created = Permission.objects.get_or_create(
             codename="can_email_group_members",
@@ -443,6 +455,8 @@ class Command(BaseCommand):
                 can_view_statistics,
                 can_create_profiles,
                 can_view_profiles,
+                can_merge_profiles,
+                can_merge_contributors,
                 can_manage_ontology,
                 can_manage_organizations,
                 can_view_potentialfellowship_list,
@@ -503,6 +517,8 @@ class Command(BaseCommand):
                 can_view_statistics,
                 can_create_profiles,
                 can_view_profiles,
+                can_merge_profiles,
+                can_merge_contributors,
                 can_manage_ontology,
                 can_manage_organizations,
                 can_view_potentialfellowship_list,
diff --git a/scipost_django/scipost/views.py b/scipost_django/scipost/views.py
index 5ab19f51a..bd5fefbeb 100644
--- a/scipost_django/scipost/views.py
+++ b/scipost_django/scipost/views.py
@@ -1583,8 +1583,8 @@ class ContributorDuplicateListView(PermissionsMixin, ListView):
 
 @transaction.atomic
 @permission_required_htmx(
-    "scipost.can_vet_registration_requests",
-    "You do not have permission to vet registration requests.",
+    "scipost.can_merge_contributors",
+    "You are not allowed to merge Contributors.",
 )
 def _hx_contributor_comparison(request):
     """
@@ -1616,8 +1616,8 @@ def _hx_contributor_comparison(request):
 
 @transaction.atomic
 @permission_required_htmx(
-    "scipost.can_vet_registration_requests",
-    "You do not have permission to vet registration requests.",
+    "scipost.can_merge_contributors",
+    "You are not allowed to merge Contributors.",
 )
 def _hx_contributor_merge(request, to_merge: int, to_merge_into: int):
     """
-- 
GitLab