Skip to content

Commit 81228a6

Browse files
committed
Remove SystemLabel completely
1 parent d018d96 commit 81228a6

File tree

11 files changed

+135
-154
lines changed

11 files changed

+135
-154
lines changed

temba/contacts/tests/test_contact.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from temba.flows.models import Flow
1616
from temba.locations.models import AdminBoundary
1717
from temba.mailroom import modifiers
18-
from temba.msgs.models import Msg, SystemLabel
18+
from temba.msgs.models import Msg, MsgFolder
1919
from temba.orgs.models import Org
2020
from temba.schedules.models import Schedule
2121
from temba.tests import TembaTest, mock_mailroom
@@ -252,10 +252,10 @@ def test_status_changes_and_release(self, mr_mocks):
252252
label.toggle_label([msg1, msg2, msg3], add=True)
253253
static_group = self.create_group("Just Joe", [self.joe])
254254

255-
msg_counts = SystemLabel.get_counts(self.org)
256-
self.assertEqual(1, msg_counts[SystemLabel.TYPE_INBOX])
257-
self.assertEqual(1, msg_counts[SystemLabel.TYPE_FLOWS])
258-
self.assertEqual(1, msg_counts[SystemLabel.TYPE_ARCHIVED])
255+
msg_counts = MsgFolder.get_counts(self.org)
256+
self.assertEqual(1, msg_counts[MsgFolder.INBOX])
257+
self.assertEqual(1, msg_counts[MsgFolder.HANDLED])
258+
self.assertEqual(1, msg_counts[MsgFolder.ARCHIVED])
259259

260260
self.assertEqual(
261261
Contact.get_status_counts(self.org),
@@ -312,10 +312,10 @@ def test_status_changes_and_release(self, mr_mocks):
312312

313313
# but his messages are unchanged
314314
self.assertEqual(2, Msg.objects.filter(contact=self.joe, visibility="V").count())
315-
msg_counts = SystemLabel.get_counts(self.org)
316-
self.assertEqual(1, msg_counts[SystemLabel.TYPE_INBOX])
317-
self.assertEqual(1, msg_counts[SystemLabel.TYPE_FLOWS])
318-
self.assertEqual(1, msg_counts[SystemLabel.TYPE_ARCHIVED])
315+
msg_counts = MsgFolder.get_counts(self.org)
316+
self.assertEqual(1, msg_counts[MsgFolder.INBOX])
317+
self.assertEqual(1, msg_counts[MsgFolder.HANDLED])
318+
self.assertEqual(1, msg_counts[MsgFolder.ARCHIVED])
319319

320320
self.joe.archive(self.admin)
321321

@@ -374,10 +374,10 @@ def test_status_changes_and_release(self, mr_mocks):
374374
self.assertEqual(0, Msg.objects.filter(contact=self.joe).exclude(text="").count())
375375
self.assertEqual(0, label.msgs.count())
376376

377-
msg_counts = SystemLabel.get_counts(self.org)
378-
self.assertEqual(0, msg_counts[SystemLabel.TYPE_INBOX])
379-
self.assertEqual(0, msg_counts[SystemLabel.TYPE_FLOWS])
380-
self.assertEqual(0, msg_counts[SystemLabel.TYPE_ARCHIVED])
377+
msg_counts = MsgFolder.get_counts(self.org)
378+
self.assertEqual(0, msg_counts[MsgFolder.INBOX])
379+
self.assertEqual(0, msg_counts[MsgFolder.HANDLED])
380+
self.assertEqual(0, msg_counts[MsgFolder.ARCHIVED])
381381

382382
# and he shouldn't be in any groups
383383
self.assertEqual(set(static_group.contacts.all()), set())

temba/flows/tests/test_flowcrudl.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from temba.campaigns.models import Campaign, CampaignEvent
1313
from temba.contacts.models import URN
1414
from temba.flows.models import Flow, FlowLabel, FlowStart, FlowUserConflictException, ResultsExport
15-
from temba.msgs.models import SystemLabel
1615
from temba.orgs.models import Export
1716
from temba.templates.models import TemplateTranslation
1817
from temba.tests import CRUDLTestMixin, TembaTest, matchers, mock_mailroom
@@ -984,7 +983,7 @@ def test_preview_start(self, mr_mocks):
984983
)
985984

986985
# if we have too many messages in our outbox we should block
987-
self.org.counts.create(scope=f"msgs:folder:{SystemLabel.TYPE_OUTBOX}", count=1_000_001)
986+
self.org.counts.create(scope="msgs:folder:O", count=1_000_001)
988987
preview_url = reverse("flows.flow_preview_start", args=[flow.id])
989988
mr_mocks.flow_start_preview(query="age > 30", total=1000)
990989

temba/msgs/models.py

+12-32
Original file line numberDiff line numberDiff line change
@@ -909,36 +909,16 @@ def get_archive_query(self) -> dict:
909909
def from_code(cls, code):
910910
return next(f for f in cls if f.code == code)
911911

912-
913-
class SystemLabel:
914-
"""
915-
TODO replace with MsgFolder once we figure out what to do with calls and broadcasts.
916-
"""
917-
918-
TYPE_INBOX = MsgFolder.INBOX.code
919-
TYPE_FLOWS = MsgFolder.HANDLED.code
920-
TYPE_ARCHIVED = MsgFolder.ARCHIVED.code
921-
TYPE_OUTBOX = MsgFolder.OUTBOX.code
922-
TYPE_SENT = MsgFolder.SENT.code
923-
TYPE_FAILED = MsgFolder.FAILED.code
924-
TYPE_SCHEDULED = "E"
925-
TYPE_CALLS = "C"
926-
927-
TYPE_CHOICES = (
928-
(TYPE_INBOX, "Inbox"),
929-
(TYPE_FLOWS, "Flows"),
930-
(TYPE_ARCHIVED, "Archived"),
931-
(TYPE_OUTBOX, "Outbox"),
932-
(TYPE_SENT, "Sent"),
933-
(TYPE_FAILED, "Failed"),
934-
(TYPE_SCHEDULED, "Scheduled"),
935-
(TYPE_CALLS, "Calls"),
936-
)
937-
938912
@classmethod
939-
def get_counts(cls, org):
913+
def get_counts(cls, org) -> dict:
940914
counts = org.counts.prefix("msgs:folder:").scope_totals()
941-
return {lb: counts.get(f"msgs:folder:{lb}", 0) for lb, n in cls.TYPE_CHOICES}
915+
by_folder = {folder: counts.get(f"msgs:folder:{folder.code}", 0) for folder in cls}
916+
917+
# TODO stuff counts for scheduled broadcasts and calls until we figure out what to do with them
918+
by_folder["scheduled"] = counts.get("msgs:folder:E", 0)
919+
by_folder["calls"] = counts.get("msgs:folder:C", 0)
920+
921+
return by_folder
942922

943923

944924
class Label(TembaModel, DependencyMixin):
@@ -1113,14 +1093,14 @@ class MessageExport(ExportType):
11131093
download_template = "msgs/export_download.html"
11141094

11151095
@classmethod
1116-
def create(cls, org, user, start_date, end_date, system_label=None, label=None, with_fields=(), with_groups=()):
1096+
def create(cls, org, user, start_date, end_date, folder=None, label=None, with_fields=(), with_groups=()):
11171097
export = Export.objects.create(
11181098
org=org,
11191099
export_type=cls.slug,
11201100
start_date=start_date,
11211101
end_date=end_date,
11221102
config={
1123-
"system_label": system_label,
1103+
"system_label": folder.code if folder else None,
11241104
"label_uuid": str(label.uuid) if label else None,
11251105
"with_fields": [f.id for f in with_fields],
11261106
"with_groups": [g.id for g in with_groups],
@@ -1129,7 +1109,7 @@ def create(cls, org, user, start_date, end_date, system_label=None, label=None,
11291109
)
11301110
return export
11311111

1132-
def get_folder(self, export):
1112+
def get_folder(self, export) -> tuple:
11331113
label_uuid = export.config.get("label_uuid")
11341114
folder_code = export.config.get("system_label")
11351115
if label_uuid:
@@ -1248,5 +1228,5 @@ def _write_msgs(self, export, exporter, msgs):
12481228
)
12491229

12501230
def get_download_context(self, export) -> dict:
1251-
system_label, label = self.get_folder(export)
1231+
folder, label = self.get_folder(export)
12521232
return {"label": label} if label else {}

temba/msgs/tests/test_broadcast.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from temba import mailroom
55
from temba.channels.models import ChannelCount
6-
from temba.msgs.models import Broadcast, LabelCount, Media, Msg, SystemLabel
6+
from temba.msgs.models import Broadcast, LabelCount, Media, Msg, MsgFolder
77
from temba.schedules.models import Schedule
88
from temba.tests import TembaTest, mock_mailroom
99
from temba.utils.compose import compose_deserialize_attachments
@@ -58,10 +58,11 @@ def test_delete(self):
5858
msg_in1.labels.add(label)
5959
self.assertEqual(LabelCount.get_totals([label])[label], 1)
6060

61-
self.assertEqual(SystemLabel.get_counts(self.org)[SystemLabel.TYPE_INBOX], 2)
62-
self.assertEqual(SystemLabel.get_counts(self.org)[SystemLabel.TYPE_FLOWS], 1)
63-
self.assertEqual(SystemLabel.get_counts(self.org)[SystemLabel.TYPE_SENT], 6)
64-
self.assertEqual(SystemLabel.get_counts(self.org)[SystemLabel.TYPE_FAILED], 1)
61+
msg_counts = MsgFolder.get_counts(self.org)
62+
self.assertEqual(msg_counts[MsgFolder.INBOX], 2)
63+
self.assertEqual(msg_counts[MsgFolder.HANDLED], 1)
64+
self.assertEqual(msg_counts[MsgFolder.SENT], 6)
65+
self.assertEqual(msg_counts[MsgFolder.FAILED], 1)
6566

6667
today = timezone.now().date()
6768
self.assertEqual(ChannelCount.get_day_count(self.channel, ChannelCount.INCOMING_MSG_TYPE, today), 3)
@@ -76,11 +77,12 @@ def test_delete(self):
7677
# broadcasts should be unaffected
7778
self.assertEqual(2, Broadcast.objects.count())
7879

79-
# check system label counts have been updated
80-
self.assertEqual(0, SystemLabel.get_counts(self.org)[SystemLabel.TYPE_INBOX])
81-
self.assertEqual(1, SystemLabel.get_counts(self.org)[SystemLabel.TYPE_FLOWS])
82-
self.assertEqual(0, SystemLabel.get_counts(self.org)[SystemLabel.TYPE_SENT])
83-
self.assertEqual(0, SystemLabel.get_counts(self.org)[SystemLabel.TYPE_FAILED])
80+
# check msg folder counts have been updated
81+
msg_counts = MsgFolder.get_counts(self.org)
82+
self.assertEqual(0, msg_counts[MsgFolder.INBOX])
83+
self.assertEqual(1, msg_counts[MsgFolder.HANDLED])
84+
self.assertEqual(0, msg_counts[MsgFolder.SENT])
85+
self.assertEqual(0, msg_counts[MsgFolder.FAILED])
8486

8587
# check user label
8688
self.assertEqual(0, LabelCount.get_totals([label])[label])

temba/msgs/tests/test_broadcastcrudl.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.utils import timezone
66

77
from temba import mailroom
8-
from temba.msgs.models import Broadcast, Media, OptIn, SystemLabel
8+
from temba.msgs.models import Broadcast, Media, OptIn
99
from temba.msgs.views import ScheduleForm
1010
from temba.schedules.models import Schedule
1111
from temba.templates.models import TemplateTranslation
@@ -538,7 +538,7 @@ def test_preview(self, mr_mocks):
538538

539539
# if we have too many messages in our outbox we should block
540540
mr_mocks.msg_broadcast_preview(query="age > 30", total=2)
541-
self.org.counts.create(scope=f"msgs:folder:{SystemLabel.TYPE_OUTBOX}", count=1_000_001)
541+
self.org.counts.create(scope="msgs:folder:O", count=1_000_001)
542542
response = self.client.post(preview_url, {"query": "age > 30"}, content_type="application/json")
543543
self.assertEqual(
544544
[

temba/msgs/tests/test_export.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django.core.files.storage import default_storage
77

88
from temba.archives.models import Archive
9-
from temba.msgs.models import Attachment, MessageExport, Msg, SystemLabel
9+
from temba.msgs.models import Attachment, MessageExport, Msg, MsgFolder
1010
from temba.orgs.models import Export
1111
from temba.tests import TembaTest
1212

@@ -22,14 +22,14 @@ def setUp(self):
2222
self.just_joe = self.create_group("Just Joe", [self.joe])
2323
self.joe_and_frank = self.create_group("Joe and Frank", [self.joe, self.frank])
2424

25-
def _export(self, system_label, label, start_date, end_date, with_groups=(), with_fields=()):
25+
def _export(self, folder, label, start_date, end_date, with_groups=(), with_fields=()):
2626
export = MessageExport.create(
2727
self.org,
2828
self.admin,
2929
start_date,
3030
end_date,
31-
system_label,
32-
label,
31+
folder=folder,
32+
label=label,
3333
with_groups=with_groups,
3434
with_fields=with_fields,
3535
)
@@ -225,7 +225,7 @@ def test_export_from_archives(self):
225225
self.org.timezone,
226226
)
227227

228-
workbook = self._export(SystemLabel.TYPE_INBOX, None, msg5.created_on.date(), msg7.created_on.date())
228+
workbook = self._export(MsgFolder.INBOX, None, msg5.created_on.date(), msg7.created_on.date())
229229
self.assertExcelSheet(
230230
workbook.worksheets[0],
231231
[
@@ -248,7 +248,7 @@ def test_export_from_archives(self):
248248
self.org.timezone,
249249
)
250250

251-
workbook = self._export(SystemLabel.TYPE_SENT, None, date(2000, 9, 1), date(2022, 9, 1))
251+
workbook = self._export(MsgFolder.SENT, None, date(2000, 9, 1), date(2022, 9, 1))
252252
self.assertExcelSheet(
253253
workbook.worksheets[0],
254254
[
@@ -271,7 +271,7 @@ def test_export_from_archives(self):
271271
self.org.timezone,
272272
)
273273

274-
workbook = self._export(SystemLabel.TYPE_FAILED, None, date(2000, 9, 1), date(2022, 9, 1))
274+
workbook = self._export(MsgFolder.FAILED, None, date(2000, 9, 1), date(2022, 9, 1))
275275
self.assertExcelSheet(
276276
workbook.worksheets[0],
277277
[
@@ -308,7 +308,7 @@ def test_export_from_archives(self):
308308
self.org.timezone,
309309
)
310310

311-
workbook = self._export(SystemLabel.TYPE_FLOWS, None, date(2000, 9, 1), date(2022, 9, 1))
311+
workbook = self._export(MsgFolder.HANDLED, None, date(2000, 9, 1), date(2022, 9, 1))
312312
self.assertExcelSheet(
313313
workbook.worksheets[0],
314314
[
@@ -571,7 +571,7 @@ def test_export(self):
571571

572572
# export just archived messages
573573
self.assertExcelSheet(
574-
self._export(SystemLabel.TYPE_ARCHIVED, None, date(2000, 9, 1), date(2022, 9, 28)).worksheets[0],
574+
self._export(MsgFolder.ARCHIVED, None, date(2000, 9, 1), date(2022, 9, 28)).worksheets[0],
575575
[
576576
expected_headers,
577577
[

temba/msgs/tests/test_msg.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from temba.channels.models import ChannelLog
77
from temba.flows.models import Flow
8-
from temba.msgs.models import Msg, SystemLabel
8+
from temba.msgs.models import Msg, MsgFolder
99
from temba.msgs.tasks import fail_old_android_messages
1010
from temba.tests import CRUDLTestMixin, TembaTest
1111
from temba.tickets.models import Ticket
@@ -175,7 +175,7 @@ def test_archive_and_release(self):
175175
def test_release_counts(self):
176176
flow = self.create_flow("Test")
177177

178-
def assertReleaseCount(direction, status, visibility, flow, label):
178+
def assertReleaseCount(direction, status, visibility, flow, folder):
179179
if direction == Msg.DIRECTION_OUT:
180180
msg = self.create_outgoing_msg(self.joe, "Whattup Joe", flow=flow, status=status)
181181
else:
@@ -184,23 +184,23 @@ def assertReleaseCount(direction, status, visibility, flow, label):
184184
Msg.objects.filter(id=msg.id).update(visibility=visibility)
185185

186186
# assert our folder count is right
187-
counts = SystemLabel.get_counts(self.org)
188-
self.assertEqual(counts[label], 1)
187+
counts = MsgFolder.get_counts(self.org)
188+
self.assertEqual(counts[folder], 1)
189189

190190
# delete the msg, count should now be 0
191191
msg.delete()
192-
counts = SystemLabel.get_counts(self.org)
193-
self.assertEqual(counts[label], 0)
192+
counts = MsgFolder.get_counts(self.org)
193+
self.assertEqual(counts[folder], 0)
194194

195195
# outgoing labels
196-
assertReleaseCount("O", Msg.STATUS_SENT, Msg.VISIBILITY_VISIBLE, None, SystemLabel.TYPE_SENT)
197-
assertReleaseCount("O", Msg.STATUS_QUEUED, Msg.VISIBILITY_VISIBLE, None, SystemLabel.TYPE_OUTBOX)
198-
assertReleaseCount("O", Msg.STATUS_FAILED, Msg.VISIBILITY_VISIBLE, flow, SystemLabel.TYPE_FAILED)
196+
assertReleaseCount("O", Msg.STATUS_SENT, Msg.VISIBILITY_VISIBLE, None, MsgFolder.SENT)
197+
assertReleaseCount("O", Msg.STATUS_QUEUED, Msg.VISIBILITY_VISIBLE, None, MsgFolder.OUTBOX)
198+
assertReleaseCount("O", Msg.STATUS_FAILED, Msg.VISIBILITY_VISIBLE, flow, MsgFolder.FAILED)
199199

200200
# incoming labels
201-
assertReleaseCount("I", Msg.STATUS_HANDLED, Msg.VISIBILITY_VISIBLE, None, SystemLabel.TYPE_INBOX)
202-
assertReleaseCount("I", Msg.STATUS_HANDLED, Msg.VISIBILITY_ARCHIVED, None, SystemLabel.TYPE_ARCHIVED)
203-
assertReleaseCount("I", Msg.STATUS_HANDLED, Msg.VISIBILITY_VISIBLE, flow, SystemLabel.TYPE_FLOWS)
201+
assertReleaseCount("I", Msg.STATUS_HANDLED, Msg.VISIBILITY_VISIBLE, None, MsgFolder.INBOX)
202+
assertReleaseCount("I", Msg.STATUS_HANDLED, Msg.VISIBILITY_ARCHIVED, None, MsgFolder.ARCHIVED)
203+
assertReleaseCount("I", Msg.STATUS_HANDLED, Msg.VISIBILITY_VISIBLE, flow, MsgFolder.HANDLED)
204204

205205
def test_fail_old_android_messages(self):
206206
msg1 = self.create_outgoing_msg(self.joe, "Hello", status=Msg.STATUS_QUEUED)

0 commit comments

Comments
 (0)