Skip to content

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

Remove staff memeber's account only if has no orders

Anna Siwik7d7bf536ad39
diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.py
index cd8270bc73..213e213c7a 100644
--- a/saleor/dashboard/customer/views.py
+++ b/saleor/dashboard/customer/views.py
@@ -72,4 +72,3 @@ def customer_edit(request, pk=None):
return redirect('dashboard:customer-details', pk=customer.pk)
ctx = {'form': form, 'customer': customer}
return TemplateResponse(request, 'dashboard/customer/form.html', ctx)
-
diff --git a/saleor/dashboard/staff/views.py b/saleor/dashboard/staff/views.py
index 8df1e340be..54ca997349 100644
--- a/saleor/dashboard/staff/views.py
+++ b/saleor/dashboard/staff/views.py
@@ -7,7 +7,8 @@ from django.utils.translation import pgettext_lazy
from .filters import StaffFilter
from .forms import StaffForm
-from ..emails import send_promote_customer_to_staff_email, send_set_password_email
+from ..emails import (
+ send_promote_customer_to_staff_email, send_set_password_email)
from ..views import staff_member_required
from ...core.utils import get_paginator_items
from ...userprofile.models import User
@@ -75,7 +76,12 @@ def staff_delete(request, pk):
queryset = User.objects.prefetch_related('orders')
staff = get_object_or_404(queryset, pk=pk)
if request.method == 'POST':
- staff.delete()
+ # do not remove staff with related orders if any
+ if staff.orders.exists():
+ staff.is_staff = False
+ staff.save()
+ else:
+ staff.delete()
msg = pgettext_lazy(
'Dashboard message', 'Removed staff member %s') % (staff,)
messages.success(request, msg)
diff --git a/templates/dashboard/staff/modal/confirm_delete.html b/templates/dashboard/staff/modal/confirm_delete.html
index 4eb5769bd4..e5edc3ebc7 100644
--- a/templates/dashboard/staff/modal/confirm_delete.html
+++ b/templates/dashboard/staff/modal/confirm_delete.html
@@ -16,12 +16,7 @@
{% block content %}
<div class="col s12">
- {% blocktrans trimmed context "Modal remove group text" %}
- <p>Are you sure you want to remove staff member?</p>
- <p>Deleting staff member also removes him from customer list and removes all his related data (orders, payment etc.).</p>
- <p>We strongly recommend not deleting user accounts that were used for purchases (for example customer which was promoted with staff member status).</p>
- <p>{{ staff }} has <strong>{{ orders }}</strong> orders.</p>
- {% endblocktrans %}
+ {% trans "Are you sure you want to remove staff member?" context "Modal remove group text" %}
</div>
{% endblock content %}
diff --git a/tests/dashboard/test_staff.py b/tests/dashboard/test_staff.py
index 6d2f530d54..66ad451970 100644
--- a/tests/dashboard/test_staff.py
+++ b/tests/dashboard/test_staff.py
@@ -63,6 +63,19 @@ def test_delete_staff_no_POST(admin_client, staff_user):
assert User.objects.all().count() == 2
+def test_delete_staff_with_orders(admin_client, staff_user, order):
+ order.user = staff_user
+ order.save()
+ assert User.objects.all().count() == 2
+ url = reverse('dashboard:staff-delete', kwargs={'pk': staff_user.pk})
+ data = {'pk': staff_user.pk}
+ response = admin_client.post(url, data)
+ assert User.objects.all().count() == 2
+ staff_user.refresh_from_db()
+ assert not staff_user.is_staff
+ assert response['Location'] == '/dashboard/staff/'
+
+
def test_staff_create_email_with_set_link_password(
admin_client, staff_group):
url = reverse('dashboard:staff-create')