Skip to content

Commit

Permalink
Remove unused code from MsgCRUDL.Menu and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Oct 17, 2024
1 parent 518117a commit 6253545
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 98 deletions.
27 changes: 27 additions & 0 deletions temba/msgs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,33 @@ def test_foreign_keys(self):


class MsgCRUDLTest(TembaTest, CRUDLTestMixin):
def test_menu(self):
menu_url = reverse("msgs.msg_menu")

contact = self.create_contact("Joe Blow", phone="+250788000001")
spam = self.create_label("Spam")
msg1 = self.create_incoming_msg(contact, "Hi")
spam.toggle_label([msg1], add=True)

self.assertRequestDisallowed(menu_url, [None, self.agent])
self.assertPageMenu(
menu_url,
self.admin,
[
"Inbox (1)",
"Handled (0)",
"Archived (0)",
"Outbox (0)",
"Sent (0)",
"Failed (0)",
"Scheduled (0)",
"Broadcasts",
"Templates",
"Calls (0)",
("Labels", ["Spam (1)"]),
],
)

def test_inbox(self):
contact1 = self.create_contact("Joe Blow", phone="+250788000001")
contact2 = self.create_contact("Frank", phone="+250788000002")
Expand Down
179 changes: 81 additions & 98 deletions temba/msgs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,113 +765,96 @@ class MsgCRUDL(SmartCRUDL):
model = Msg
actions = ("inbox", "flow", "archived", "menu", "outbox", "sent", "failed", "filter", "export", "legacy_inbox")

class Menu(BaseMenuView): # pragma: no cover
class Menu(BaseMenuView):
def derive_menu(self):
org = self.request.org
counts = SystemLabel.get_counts(org)

if self.request.GET.get("labels"):
labels = Label.get_active_for_org(org).order_by(Lower("name"))
label_counts = LabelCount.get_totals([lb for lb in labels])

menu = []
for label in labels:
menu.append(
self.create_menu_item(
menu_id=label.uuid,
name=label.name,
href=reverse("msgs.msg_filter", args=[label.uuid]),
count=label_counts[label],
)
)
return menu
else:
labels = Label.get_active_for_org(org).order_by(Lower("name"))
menu = [
self.create_menu_item(
menu_id="inbox",
name=_("Inbox"),
href=reverse("msgs.msg_inbox"),
count=counts[SystemLabel.TYPE_INBOX],
icon="inbox",
),
self.create_menu_item(
menu_id="handled",
name=_("Handled"),
href=reverse("msgs.msg_flow"),
count=counts[SystemLabel.TYPE_FLOWS],
icon="flow",
),
self.create_menu_item(
menu_id="archived",
name=_("Archived"),
href=reverse("msgs.msg_archived"),
count=counts[SystemLabel.TYPE_ARCHIVED],
icon="archive",
),
self.create_divider(),
self.create_menu_item(
menu_id="outbox",
name=_("Outbox"),
href=reverse("msgs.msg_outbox"),
count=counts[SystemLabel.TYPE_OUTBOX],
),
self.create_menu_item(
menu_id="sent",
name=_("Sent"),
href=reverse("msgs.msg_sent"),
count=counts[SystemLabel.TYPE_SENT],
),
self.create_menu_item(
menu_id="failed",
name=_("Failed"),
href=reverse("msgs.msg_failed"),
count=counts[SystemLabel.TYPE_FAILED],
),
self.create_divider(),
self.create_menu_item(
menu_id="scheduled",
name=_("Scheduled"),
href=reverse("msgs.broadcast_scheduled"),
count=counts[SystemLabel.TYPE_SCHEDULED],
),
self.create_menu_item(
menu_id="broadcasts",
name=_("Broadcasts"),
href=reverse("msgs.broadcast_list"),
),
self.create_menu_item(
menu_id="templates",
name=_("Templates"),
href=reverse("templates.template_list"),
),
self.create_divider(),
self.create_menu_item(
menu_id="calls",
name=_("Calls"),
href=reverse("ivr.call_list"),
count=counts[SystemLabel.TYPE_CALLS],
),
]

menu = [
self.create_menu_item(
menu_id="inbox",
name=_("Inbox"),
href=reverse("msgs.msg_inbox"),
count=counts[SystemLabel.TYPE_INBOX],
icon="inbox",
),
self.create_menu_item(
menu_id="handled",
name=_("Handled"),
href=reverse("msgs.msg_flow"),
count=counts[SystemLabel.TYPE_FLOWS],
icon="flow",
),
self.create_menu_item(
menu_id="archived",
name=_("Archived"),
href=reverse("msgs.msg_archived"),
count=counts[SystemLabel.TYPE_ARCHIVED],
icon="archive",
),
self.create_divider(),
self.create_menu_item(
menu_id="outbox",
name=_("Outbox"),
href=reverse("msgs.msg_outbox"),
count=counts[SystemLabel.TYPE_OUTBOX],
),
self.create_menu_item(
menu_id="sent",
name=_("Sent"),
href=reverse("msgs.msg_sent"),
count=counts[SystemLabel.TYPE_SENT],
),
labels = Label.get_active_for_org(org).order_by(Lower("name"))
label_items = []
label_counts = LabelCount.get_totals([lb for lb in labels])
for label in labels:
label_items.append(
self.create_menu_item(
menu_id="failed",
name=_("Failed"),
href=reverse("msgs.msg_failed"),
count=counts[SystemLabel.TYPE_FAILED],
),
self.create_divider(),
self.create_menu_item(
menu_id="scheduled",
name=_("Scheduled"),
href=reverse("msgs.broadcast_scheduled"),
count=counts[SystemLabel.TYPE_SCHEDULED],
),
self.create_menu_item(
menu_id="broadcasts",
name=_("Broadcasts"),
href=reverse("msgs.broadcast_list"),
),
self.create_menu_item(
menu_id="templates",
name=_("Templates"),
href=reverse("templates.template_list"),
),
self.create_divider(),
self.create_menu_item(
menu_id="calls",
name=_("Calls"),
href=reverse("ivr.call_list"),
count=counts[SystemLabel.TYPE_CALLS],
),
]

label_items = []
label_counts = LabelCount.get_totals([lb for lb in labels])
for label in labels:
label_items.append(
self.create_menu_item(
icon="label",
menu_id=label.uuid,
name=label.name,
count=label_counts[label],
href=reverse("msgs.msg_filter", args=[label.uuid]),
)
icon="label",
menu_id=label.uuid,
name=label.name,
count=label_counts[label],
href=reverse("msgs.msg_filter", args=[label.uuid]),
)
)

if label_items:
menu.append(self.create_menu_item(menu_id="labels", name="Labels", items=label_items, inline=True))
if label_items:
menu.append(self.create_menu_item(menu_id="labels", name="Labels", items=label_items, inline=True))

return menu
return menu

class Export(BaseExportModal):
class Form(BaseExportModal.Form):
Expand Down

0 comments on commit 6253545

Please sign in to comment.