Skip to content

Commit

Permalink
Suggest user merge if user with same email address exists
Browse files Browse the repository at this point in the history
  • Loading branch information
hansegucker committed Nov 6, 2023
1 parent b4d7d3b commit fa76484
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 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 @@ -999,6 +1000,7 @@ def clean_email(self):
user_with_same_email = user_with_same_email.exclude(pk=self.instance.pk)

if user_with_same_email.exists():
self.user_with_same_email = user_with_same_email.get()
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
14 changes: 13 additions & 1 deletion evap/staff/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,25 @@ 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"] = "[email protected]"
form.submit()
self.assertTrue(UserProfile.objects.filter(email="[email protected]").exists())

def test_user_edit_duplicate_email(self):
second_user = baker.make(UserProfile)
second_user.email = "[email protected]"
second_user.save()
page = self.app.get(self.url, user=self.manager, status=200)
form = page.forms["user-form"]
form["email"] = "[email protected]"
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):
mock_remove.return_value = ["This text is supposed to be visible on the website."]
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

0 comments on commit fa76484

Please sign in to comment.