Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest user merge if user with same email address exists #2065

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion evap/staff/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ class Meta:

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user_with_same_email = None
evaluations_in_active_semester = Evaluation.objects.filter(course__semester=Semester.active_semester())
excludes = [x.id for x in evaluations_in_active_semester if x.is_single_result]
evaluations_in_active_semester = evaluations_in_active_semester.exclude(id__in=excludes)
Expand Down Expand Up @@ -998,7 +999,8 @@ def clean_email(self):
if self.instance and self.instance.pk:
user_with_same_email = user_with_same_email.exclude(pk=self.instance.pk)

if user_with_same_email.exists():
if user_with_same_email:
self.user_with_same_email = user_with_same_email.first()
raise forms.ValidationError(_("A user with the email '%s' already exists") % email)
return email

Expand Down
10 changes: 10 additions & 0 deletions evap/staff/templates/staff_user_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ <h3>{% if form.instance.id %}{% trans 'Edit user' %}{% else %}{% trans 'Create u
{% endif %}
</div>

{% if user_with_same_email %}
<div class="alert alert-warning alert-dismissible">
<p>
{% trans "A user with this email address already exists. You probably want to merge the users." %}
</p>
<a type="button" class="btn btn-primary btn-sm" href="{% url 'staff:user_merge' user_with_same_email.id form.instance.id %}">{% trans "Merge both users" %}</a>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
{% endif %}


<form id="user-form" method="POST" class="form-horizontal multiselect-form">
{% csrf_token %}
Expand Down
16 changes: 13 additions & 3 deletions evap/staff/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,22 @@ def setUpTestData(cls):
cls.testuser = baker.make(UserProfile)
cls.url = f"/staff/user/{cls.testuser.pk}/edit"

def test_questionnaire_edit(self):
def test_user_edit(self):
page = self.app.get(self.url, user=self.manager, status=200)
form = page.forms["user-form"]
form["email"] = "lfo9e7bmxp1xi@institution.example.com"
form["email"] = "test@institution.example.com"
form.submit()
self.assertTrue(UserProfile.objects.filter(email="[email protected]").exists())
self.assertTrue(UserProfile.objects.filter(email="[email protected]").exists())

def test_user_edit_duplicate_email(self):
second_user = baker.make(UserProfile, email="[email protected]")
page = self.app.get(self.url, user=self.manager, status=200)
form = page.forms["user-form"]
form["email"] = second_user.email
page = form.submit()
self.assertContains(
page, "A user with this email address already exists. You probably want to merge the users."
)

@patch("evap.staff.forms.remove_user_from_represented_and_ccing_users")
def test_inactive_edit(self, mock_remove):
Expand Down
1 change: 1 addition & 0 deletions evap/staff/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2211,6 +2211,7 @@ def notify_reward_points(grantings, **_kwargs):
"evaluations_contributing_to": evaluations_contributing_to,
"has_due_evaluations": bool(user.get_sorted_due_evaluations()),
"user_id": user_id,
"user_with_same_email": form.user_with_same_email,
},
)

Expand Down