Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add ability to remove customer from the dashboard

Pacu2853f8c8b0960
diff --git a/saleor/dashboard/customer/forms.py b/saleor/dashboard/customer/forms.py
index 12da3dc78b..bf139cf654 100644
--- a/saleor/dashboard/customer/forms.py
+++ b/saleor/dashboard/customer/forms.py
@@ -4,6 +4,35 @@ from django.utils.translation import pgettext_lazy
from ...account.models import CustomerNote, User
+class CustomerDeleteForm(forms.Form):
+
+ def __init__(self, *args, **kwargs):
+ self.instance = kwargs.pop('instance')
+ self.user = kwargs.pop('user')
+ super().__init__(*args, **kwargs)
+
+ def clean(self):
+ data = super().clean()
+ if not self.instance.is_staff:
+ return data
+
+ if self.instance == self.user:
+ raise forms.ValidationError(pgettext_lazy(
+ 'Edit customer details in order form error',
+ 'You can\'t delete your own account via dashboard, '
+ 'please try from the storefront.'))
+ if self.instance.is_superuser:
+ raise forms.ValidationError(pgettext_lazy(
+ 'Edit customer details in order form error',
+ 'Only superuser can delete his own account.'))
+ can_edit_staff_users = self.user.has_perm('account.edit_staff')
+ if not can_edit_staff_users:
+ raise forms.ValidationError(pgettext_lazy(
+ 'Edit customer details in order form error',
+ 'You have insufficient permissions, to edit staff users.'))
+ return data
+
+
class CustomerForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
diff --git a/saleor/dashboard/customer/urls.py b/saleor/dashboard/customer/urls.py
index aab8248797..715034b98d 100644
--- a/saleor/dashboard/customer/urls.py
+++ b/saleor/dashboard/customer/urls.py
@@ -8,6 +8,8 @@ urlpatterns = [
url(r'^(?P<pk>[0-9]+)/$', views.customer_details, name='customer-details'),
url(r'^(?P<pk>[0-9]+)/update/$', views.customer_edit,
name='customer-update'),
+ url(r'^(?P<pk>[0-9]+)/delete/$', views.customer_delete,
+ name='customer-delete'),
url(r'^ajax/users/$', views.ajax_users_list, name='ajax-users-list'),
url(r'^(?P<customer_pk>[0-9]+)/add-note/$',
views.customer_add_note, name='customer-add-note')]
diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.py
index 32e792434f..b9d6a83644 100644
--- a/saleor/dashboard/customer/views.py
+++ b/saleor/dashboard/customer/views.py
@@ -13,7 +13,7 @@ from ...core.utils import get_paginator_items
from ..emails import send_set_password_email
from ..views import staff_member_required
from .filters import UserFilter
-from .forms import CustomerForm, CustomerNoteForm
+from .forms import CustomerForm, CustomerNoteForm, CustomerDeleteForm
@staff_member_required
@@ -117,3 +117,26 @@ def customer_add_note(request, customer_pk):
ctx.update(csrf(request))
template = 'dashboard/customer/modal/add_note.html'
return TemplateResponse(request, template, ctx, status=status)
+
+
+@staff_member_required
+@permission_required('account.edit_user')
+def customer_delete(request, pk):
+ customer = get_object_or_404(User, pk=pk)
+ form = CustomerDeleteForm(
+ request.POST or None, instance=customer, user=request.user)
+ status = 200
+ if request.method == 'POST' and form.is_valid():
+ customer.delete()
+ msg = pgettext_lazy(
+ 'Dashboard message',
+ '%(customer_name)s successfully removed') % {
+ 'customer_name': customer}
+ messages.success(request, msg)
+ return redirect('dashboard:customers')
+ elif request.method == 'POST' and not form.is_valid():
+ status = 400
+ ctx = {'customer': customer, 'form': form}
+ return TemplateResponse(
+ request, 'dashboard/customer/modal/confirm_delete.html', ctx,
+ status=status)
diff --git a/templates/dashboard/customer/detail.html b/templates/dashboard/customer/detail.html
index c5f1f8eb4a..b10eabc5fd 100644
--- a/templates/dashboard/customer/detail.html
+++ b/templates/dashboard/customer/detail.html
@@ -74,6 +74,9 @@
<a href="{% url 'dashboard:customer-update' customer.pk %}" class="btn-flat waves-effect">
{% trans "Edit customer" context "Customer action" %}
</a>
+ <a href="#base-modal" data-href="{% url 'dashboard:customer-delete' pk=customer.pk %}" class="modal-trigger-custom btn-flat waves-effect">
+ {% trans "Remove customer" context "Customer action" %}
+ </a>
{% endif %}
{% if perms.account.impersonate_user %}
<a href="{% url 'impersonate-start' uid=customer.pk %}" class="btn-flat waves-effect">
diff --git a/templates/dashboard/customer/modal/confirm_delete.html b/templates/dashboard/customer/modal/confirm_delete.html
new file mode 100644
index 0000000000..f940448e1a
--- /dev/null
+++ b/templates/dashboard/customer/modal/confirm_delete.html
@@ -0,0 +1,29 @@
+{% extends "dashboard/base_modal.html" %}
+
+{% load i18n %}
+
+{% block form_class %}form-async{% endblock %}
+
+{% block action_url %}
+{% url 'dashboard:customer-delete' pk=customer.pk %}
+{% endblock %}
+
+{% block title %}
+{% blocktrans trimmed context "Header of modal used for removing customer's account " %}
+ Remove customer {{ customer }}
+{% endblocktrans %}
+{% endblock %}
+
+{% block content %}
+ <div class="col s12">
+ {% blocktrans trimmed context "Modal remove collection text" %}
+ Are you sure you want to remove customer <strong>{{ customer }}</strong>?
+ Please note that this procedure will remove customer's account,
+ but customer's orders and addresses associated with them will persist for the financial records.
+ {% endblocktrans %}
+ </div>
+{% endblock content %}
+
+{% block primary_action %}
+{% trans "Remove" context "Dashboard delete action" %}
+{% endblock %}
diff --git a/tests/dashboard/test_customer.py b/tests/dashboard/test_customer.py
index e6755b86d5..87dc4f5fa1 100644
--- a/tests/dashboard/test_customer.py
+++ b/tests/dashboard/test_customer.py
@@ -1,7 +1,7 @@
from django.urls import reverse
-
-from saleor.account.models import CustomerNote
-from saleor.dashboard.customer.forms import CustomerNoteForm
+from saleor.account.models import CustomerNote, User
+from saleor.dashboard.customer.forms import (
+ CustomerDeleteForm, CustomerNoteForm)
def test_add_note_to_customer(admin_user, customer_user):
@@ -22,3 +22,45 @@ def test_add_note_to_customer_from_url(admin_client, customer_user):
response = admin_client.post(url, data)
assert response.status_code == 200
+
+
+def test_view_delete_customer(admin_client, admin_user, customer_user):
+ url = reverse('dashboard:customer-delete', args=[admin_user.pk])
+ response = admin_client.post(url, data={'csrf': 'exampledata'})
+ assert response.status_code == 400
+
+ url = reverse('dashboard:customer-delete', args=[customer_user.pk])
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ response = admin_client.post(url, data={'csrf': 'exampledata'})
+ assert not User.objects.filter(pk=customer_user.pk).exists()
+ assert response.status_code == 302
+
+
+def test_form_delete_customer(
+ staff_user, customer_user, admin_user, permission_edit_staff,
+ staff_group):
+ data = {'csrf': 'example-data'}
+ form = CustomerDeleteForm(data, instance=customer_user, user=staff_user)
+ assert form.is_valid()
+
+ # Deleting your own account is not allowed
+ form = CustomerDeleteForm(data, instance=staff_user, user=staff_user)
+ assert not form.is_valid()
+
+ # Deleting a superuser is not allowed
+ form = CustomerDeleteForm(data, instance=admin_user, user=staff_user)
+ assert not form.is_valid()
+
+ # Deleting another staff is not allowed without relevant permission
+ another_staff_user = User.objects.create(is_staff=True, email='[email protected]')
+ form = CustomerDeleteForm(
+ data, instance=another_staff_user, user=staff_user)
+ assert not form.is_valid()
+
+ staff_group.permissions.add(permission_edit_staff)
+ staff_user.groups.add(staff_group)
+ staff_user = User.objects.get(pk=staff_user.pk)
+ form = CustomerDeleteForm({}, instance=another_staff_user, user=staff_user)
+ assert form.is_valid()
diff --git a/tests/dashboard/test_permissions.py b/tests/dashboard/test_permissions.py
index f1b8233c6a..2161e5c2ff 100644
--- a/tests/dashboard/test_permissions.py
+++ b/tests/dashboard/test_permissions.py
@@ -1273,3 +1273,8 @@ def test_staff_group_member_can_edit_menu_items(
assert response.status_code == 200
response = staff_client.get(menu_item_delete_url)
assert response.status_code == 200
+
+
+def test_staff_group_member_can_remove_user(
+ staff_client, staff_user, staff_group, permission_edit_user):
+ pass