Skip to content

Commit

Permalink
Merge pull request #5810 from nyaruka/simplify_ticket_assignment
Browse files Browse the repository at this point in the history
Remove no longer needed functionality for restricting which users can be assigned to tickets
  • Loading branch information
rowanseymour authored Jan 15, 2025
2 parents 8368b0f + 82ca8c7 commit 7c2ed1c
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 32 deletions.
4 changes: 2 additions & 2 deletions temba/contacts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
BaseUsagesModal,
)
from temba.orgs.views.mixins import BulkActionMixin, OrgObjPermsMixin, OrgPermsMixin
from temba.tickets.models import Ticket, Topic
from temba.tickets.models import Topic
from temba.utils import json, on_transaction_commit
from temba.utils.dates import datetime_to_timestamp, timestamp_to_datetime
from temba.utils.fields import CheckboxWidget, InputWidget, SelectWidget, TembaChoiceField
Expand Down Expand Up @@ -780,7 +780,7 @@ def __init__(self, instance, org, **kwargs):
super().__init__(**kwargs)

self.fields["topic"].queryset = org.topics.filter(is_active=True).order_by("name")
self.fields["assignee"].queryset = Ticket.get_allowed_assignees(org).order_by("email")
self.fields["assignee"].queryset = org.get_users().order_by("email")

form_class = Form
submit_button_name = _("Open")
Expand Down
10 changes: 2 additions & 8 deletions temba/orgs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from timezone_field import TimeZoneField

from django.conf import settings
from django.contrib.auth.models import Group, Permission, User as AuthUser
from django.contrib.auth.models import Group, User as AuthUser
from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.indexes import OpClass
from django.contrib.postgres.validators import ArrayMinLengthValidator
Expand Down Expand Up @@ -1037,18 +1037,12 @@ def format_datetime(self, d, *, show_time=True, seconds=False):
format = formats[1] if show_time else formats[0]
return datetime_to_str(d, format, self.timezone)

def get_users(self, *, roles: list = None, with_perm: str = None):
def get_users(self, *, roles: list = None):
"""
Gets users in this org, filtered by role or permission.
"""
qs = self.users.filter(is_active=True).select_related("settings")

if with_perm:
app_label, codename = with_perm.split(".")
permission = Permission.objects.get(content_type__app_label=app_label, codename=codename)
groups = Group.objects.filter(permissions=permission)
roles = [OrgRole.from_group(g) for g in groups]

if roles is not None:
qs = qs.filter(orgmembership__org=self, orgmembership__role_code__in=[r.code for r in roles])

Expand Down
8 changes: 0 additions & 8 deletions temba/orgs/tests/test_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ def test_get_users(self):
list(self.org2.get_users(roles=[OrgRole.ADMINISTRATOR, OrgRole.EDITOR]).order_by("id")),
)

self.assertEqual(
[self.admin, self.editor, self.agent, admin3],
list(self.org.get_users(with_perm="tickets.ticket_assignee").order_by("id")),
)
self.assertEqual(
[self.admin, self.admin2], list(self.org2.get_users(with_perm="tickets.ticket_assignee").order_by("id"))
)

self.assertEqual([self.admin, admin3], list(self.org.get_admins().order_by("id")))
self.assertEqual([self.admin, self.admin2], list(self.org2.get_admins().order_by("id")))

Expand Down
3 changes: 1 addition & 2 deletions temba/settings_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
"workspace",
),
"request_logs.httplog": ("webhooks", "classifier"),
"tickets.ticket": ("assign", "assignee", "menu", "note", "export_stats", "export"),
"tickets.ticket": ("assign", "menu", "note", "export_stats", "export"),
"triggers.trigger": ("archived", "type", "menu"),
}

Expand Down Expand Up @@ -585,7 +585,6 @@
"orgs.org_languages",
"orgs.org_menu",
"tickets.ticket_assign",
"tickets.ticket_assignee",
"tickets.ticket_list",
"tickets.ticket_menu",
"tickets.ticket_note",
Expand Down
7 changes: 0 additions & 7 deletions temba/tickets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ class Ticket(models.Model):
STATUS_CLOSED = "C"
STATUS_CHOICES = ((STATUS_OPEN, _("Open")), (STATUS_CLOSED, _("Closed")))

# permission that users need to have a ticket assigned to them
ASSIGNEE_PERMISSION = "tickets.ticket_assignee"

MAX_NOTE_LENGTH = 10_000

uuid = models.UUIDField(unique=True, default=uuid4)
Expand Down Expand Up @@ -238,10 +235,6 @@ def bulk_reopen(cls, org, user, tickets):
def _bulk_response(self, resp: dict, tickets: list) -> list:
return [t for t in tickets if t.id in resp["changed_ids"]]

@classmethod
def get_allowed_assignees(cls, org):
return org.get_users(with_perm=cls.ASSIGNEE_PERMISSION)

@classmethod
def get_assignee_count(cls, org, user, topics, status: str) -> int:
"""
Expand Down
6 changes: 1 addition & 5 deletions temba/tickets/tests/test_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ def test_model(self, mr_mocks):

self.assertEqual([call(self.org, self.admin, [ticket])], mr_mocks.calls["ticket_reopen"])

def test_allowed_assignees(self):
self.assertEqual({self.admin, self.editor, self.agent}, set(Ticket.get_allowed_assignees(self.org)))
self.assertEqual({self.admin2}, set(Ticket.get_allowed_assignees(self.org2)))

@mock_mailroom
def test_counts(self, mr_mocks):
general = self.org.default_ticket_topic
Expand All @@ -81,7 +77,7 @@ def assert_counts(
org, *, assignee_open: dict, assignee_closed: dict, topic_open: dict, topic_closed: dict, contacts: dict
):
all_topics = org.topics.filter(is_active=True)
assignees = [None] + list(Ticket.get_allowed_assignees(org))
assignees = [None] + list(org.get_users())

self.assertEqual(
assignee_open, {u: Ticket.get_assignee_count(org, u, all_topics, Ticket.STATUS_OPEN) for u in assignees}
Expand Down

0 comments on commit 7c2ed1c

Please sign in to comment.