Skip to content

Commit

Permalink
Add Team.all_topics to more easily model a team that can access all t…
Browse files Browse the repository at this point in the history
…opics
  • Loading branch information
rowanseymour committed Oct 23, 2024
1 parent ad87740 commit 78081da
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
12 changes: 12 additions & 0 deletions temba/tickets/migrations/0065_team_all_topics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 5.1.2 on 2024-10-23 19:51

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [("tickets", "0064_shortcut")]

operations = [
migrations.AddField(model_name="team", name="all_topics", field=models.BooleanField(default=False)),
]
14 changes: 11 additions & 3 deletions temba/tickets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ def create_from_import_def(cls, org, user, definition: dict):
def release(self, user):
assert not (self.is_system and self.org.is_active), "can't release system topics"
assert not self.tickets.exists(), "can't release topic with tickets"

super().release(user)

for team in self.teams.all():
team.topics.remove(self)

self.is_active = False
self.name = self._deleted_name()
self.modified_by = user
Expand Down Expand Up @@ -402,20 +406,24 @@ class Meta:

class Team(TembaModel):
"""
Every user can be a member of a ticketing team
Agent users are assigned to a team which controls which topics they can access.
"""

org = models.ForeignKey(Org, on_delete=models.PROTECT, related_name="teams")
topics = models.ManyToManyField(Topic, related_name="teams")
all_topics = models.BooleanField(default=False)

org_limit_key = Org.LIMIT_TEAMS

@classmethod
def create(cls, org, user, name: str):
def create(cls, org, user, name: str, *, topics=(), all_topics: bool = False):
assert cls.is_valid_name(name), f"'{name}' is not a valid team name"
assert not org.teams.filter(name__iexact=name, is_active=True).exists()
assert not (topics and all_topics), "can't specify topics and all_topics"

return org.teams.create(name=name, created_by=user, modified_by=user)
team = org.teams.create(name=name, all_topics=all_topics, created_by=user, modified_by=user)
team.topics.add(*topics)
return team

def get_users(self):
return self.org.users.filter(orgmembership__team=self)
Expand Down
26 changes: 20 additions & 6 deletions temba/tickets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,14 +1286,20 @@ def _import(definition, preview=False):

def test_release(self):
topic1 = Topic.create(self.org, self.admin, "Sales")
topic2 = Topic.create(self.org, self.admin, "Support")
flow = self.create_flow("Test")
flow.topic_dependencies.add(topic1)
team = Team.create(self.org, self.admin, "Sales & Support", topics=[topic1, topic2])

topic1.release(self.admin)

self.assertFalse(topic1.is_active)
self.assertTrue(topic1.name.startswith("deleted-"))

# topic should be removed from team
self.assertEqual({topic2}, set(team.topics.all()))

# flow should be flagged as having issues
flow.refresh_from_db()
self.assertTrue(flow.has_issues)

Expand All @@ -1313,24 +1319,32 @@ def test_release(self):

class TeamTest(TembaTest):
def test_create(self):
team1 = Team.create(self.org, self.admin, "Sales")
sales = Topic.create(self.org, self.admin, "Sales")
support = Topic.create(self.org, self.admin, "Support")
team1 = Team.create(self.org, self.admin, "Sales & Support", topics=[sales, support])
agent2 = self.create_user("[email protected]")
self.org.add_user(self.agent, OrgRole.AGENT, team=team1)
self.org.add_user(agent2, OrgRole.AGENT, team=team1)

self.assertEqual("Sales", team1.name)
self.assertEqual("Sales", str(team1))
self.assertEqual(f'<Team: id={team1.id} name="Sales">', repr(team1))

self.assertEqual("Sales & Support", team1.name)
self.assertEqual("Sales & Support", str(team1))
self.assertEqual(f'<Team: id={team1.id} name="Sales & Support">', repr(team1))
self.assertEqual({self.agent, agent2}, set(team1.get_users()))
self.assertEqual({sales, support}, set(team1.topics.all()))
self.assertFalse(team1.all_topics)

# create an unrestricted team
team2 = Team.create(self.org, self.admin, "Any Topic", all_topics=True)
self.assertEqual(set(), set(team2.topics.all()))
self.assertTrue(team2.all_topics)

# try to create with invalid name
with self.assertRaises(AssertionError):
Team.create(self.org, self.admin, '"Support"')

# try to create with name that already exists
with self.assertRaises(AssertionError):
Team.create(self.org, self.admin, "Sales")
Team.create(self.org, self.admin, "Sales & Support")

def test_release(self):
team1 = Team.create(self.org, self.admin, "Sales")
Expand Down

0 comments on commit 78081da

Please sign in to comment.