From 5fc9a908c52d10ff5895f548eaa11c40a7552fc4 Mon Sep 17 00:00:00 2001 From: javad Date: Sun, 13 Sep 2026 15:17:01 +0330 Subject: [PATCH] fix contact for admin --- apps/tickets/views.py | 10 +++++++++- tests/test_organizational_ticketing.py | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/apps/tickets/views.py b/apps/tickets/views.py index d5a3b0f..c3e21dc 100644 --- a/apps/tickets/views.py +++ b/apps/tickets/views.py @@ -338,7 +338,15 @@ def staff_create(request): return render(request,"tickets/staff_create.html",{"form":form,"staff_unit":TicketPolicy.primary_unit(request.user),"active_tab_title":"ثبت تیکت برای مشتری"}) def _customer_rows(search="", user=None): - qs=User.objects.filter(account_type=User.AccountType.CUSTOMER).select_related("customer_profile") + # A CUSTOMER account can temporarily exist without its one-to-one profile + # (legacy/imported or interrupted registrations). Such an account is not + # an address-book contact and would make the contacts template dereference + # a missing profile, especially for superusers whose scope includes all + # customers. + qs=User.objects.filter( + account_type=User.AccountType.CUSTOMER, + customer_profile__isnull=False, + ).select_related("customer_profile") has_unit_membership = user is not None and user.unit_memberships.filter(is_active=True, unit__is_active=True).exists() if user is not None and has_unit_membership and not (user.is_superuser or user.has_perm("tickets.view_all_tickets")): visible_customer_ids = staff_ticket_rows(user).values_list("customer_id", flat=True) diff --git a/tests/test_organizational_ticketing.py b/tests/test_organizational_ticketing.py index 87f80f5..151aa08 100644 --- a/tests/test_organizational_ticketing.py +++ b/tests/test_organizational_ticketing.py @@ -232,6 +232,31 @@ def test_staff_operations_pages_respect_unit_scoped_permissions(client): assert client.get(reverse("staff_tickets:sms_templates")).status_code == 200 +@pytest.mark.django_db +def test_superuser_without_staff_profile_can_open_all_staff_contacts(client): + unit = OrganizationalUnit.objects.create(name="واحد مخاطبان مدیر", slug="admin-contacts-unit") + contact = make_contact("admin-visible-contact", "09120000503") + group = ContactGroup.objects.create(name="گروه قابل مشاهده مدیر", unit=unit) + contact.customer_profile.groups.add(group) + User.objects.create_user( + username="customer-without-profile", + mobile="09120000504", + account_type=User.AccountType.CUSTOMER, + ) + admin = User.objects.create_superuser( + username="contacts-admin", + password="safe-password", + account_type=User.AccountType.STAFF, + ) + + client.force_login(admin) + response = client.get(reverse("staff_tickets:contacts")) + + assert response.status_code == 200 + assert "admin-visible-contact" in response.content.decode() + assert "customer-without-profile" not in response.content.decode() + + @pytest.mark.django_db def test_unit_scoped_add_ticket_preserves_contact_ownership_and_stakeholder(client): unit=OrganizationalUnit.objects.create(name="حسابداری عملیاتی",slug="accounting-ops")