Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optin as field to channelevents #4875

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions temba/channels/migrations/0177_channelevent_optin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.3 on 2023-10-04 15:54

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("msgs", "0250_alter_msg_msg_type"),
("channels", "0176_remove_channel_alert_email_delete_alert"),
]

operations = [
migrations.AddField(
model_name="channelevent",
name="optin",
field=models.ForeignKey(
null=True, on_delete=django.db.models.deletion.PROTECT, related_name="optins", to="msgs.optin"
),
),
]
1 change: 1 addition & 0 deletions temba/channels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ class ChannelEvent(models.Model):
contact_urn = models.ForeignKey(
"contacts.ContactURN", on_delete=models.PROTECT, null=True, related_name="channel_events"
)
optin = models.ForeignKey("msgs.OptIn", null=True, on_delete=models.PROTECT, related_name="optins")
extra = JSONAsTextField(null=True, default=dict)
occurred_on = models.DateTimeField()
created_on = models.DateTimeField(default=timezone.now)
Expand Down
4 changes: 2 additions & 2 deletions temba/contacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def get_history(self, after: datetime, before: datetime, include_event_types: se
self.msgs.filter(created_on__gte=after, created_on__lt=before)
.exclude(status=Msg.STATUS_PENDING)
.order_by("-created_on", "-id")
.select_related("channel", "contact_urn", "broadcast")[:limit]
.select_related("channel", "contact_urn", "broadcast", "optin")[:limit]
)

# get all runs start started or ended in this period
Expand All @@ -797,7 +797,7 @@ def get_history(self, after: datetime, before: datetime, include_event_types: se
channel_events = (
self.channel_events.filter(created_on__gte=after, created_on__lt=before)
.order_by("-created_on")
.select_related("channel")[:limit]
.select_related("channel", "optin")[:limit]
)

campaign_events = (
Expand Down
2 changes: 1 addition & 1 deletion temba/mailroom/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def from_channel_event(cls, org: Org, user: User, obj: ChannelEvent) -> dict:
if obj.event_type in ChannelEvent.CALL_TYPES:
ch_event["duration"] = extra.get("duration")
elif obj.event_type in (ChannelEvent.TYPE_OPTIN, ChannelEvent.TYPE_OPTOUT):
ch_event["optin"] = _optin(OptIn.objects.get(org=org, id=extra.get("optin_id")))
ch_event["optin"] = _optin(obj.optin) if obj.optin else None

return {
"type": cls.TYPE_CHANNEL_EVENT,
Expand Down
3 changes: 2 additions & 1 deletion temba/mailroom/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,8 @@ def test_from_channel_event(self):
self.channel,
"tel:+250979111111",
ChannelEvent.TYPE_OPTIN,
extra={"optin_id": optin.id, "optin_name": "Polls"},
optin=optin,
extra={"title": "Polls", "payload": str(optin.id)},
)

self.assertEqual(
Expand Down
3 changes: 2 additions & 1 deletion temba/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ def create_channel(
modified_by=self.admin,
)

def create_channel_event(self, channel, urn, event_type, occurred_on=None, extra=None):
def create_channel_event(self, channel, urn, event_type, occurred_on=None, optin=None, extra=None):
urn_obj = ContactURN.lookup(channel.org, urn, country_code=channel.country)
if urn_obj:
contact = urn_obj.contact
Expand All @@ -694,6 +694,7 @@ def create_channel_event(self, channel, urn, event_type, occurred_on=None, extra
contact_urn=urn_obj,
occurred_on=occurred_on or timezone.now(),
event_type=event_type,
optin=optin,
extra=extra,
)

Expand Down