forked from rapidpro/rapidpro
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into allauth-progress
- Loading branch information
Showing
27 changed files
with
897 additions
and
889 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[project] | ||
name = "temba" | ||
version = "10.1.71" | ||
version = "10.1.74" | ||
description = "Hosted service for visually building interactive messaging applications" | ||
authors = [ | ||
{"name" = "Nyaruka", "email" = "[email protected]"} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
temba/contacts/migrations/0205_create_session_expires_fires.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Generated by Django 5.1.4 on 2025-02-26 16:27 | ||
|
||
import random | ||
from datetime import timedelta | ||
|
||
from django.db import migrations | ||
from django.db.models import Exists, OuterRef | ||
|
||
|
||
def create_session_expires_fires(apps, schema_editor): | ||
Contact = apps.get_model("contacts", "Contact") | ||
ContactFire = apps.get_model("contacts", "ContactFire") | ||
FlowSession = apps.get_model("flows", "FlowSession") | ||
|
||
num_created, num_skipped = 0, 0 | ||
|
||
while True: | ||
# find contacts with waiting sessions that don't have a corresponding session expiration fire | ||
batch = list( | ||
Contact.objects.filter(current_session_uuid__isnull=False) | ||
.filter(~Exists(ContactFire.objects.filter(contact=OuterRef("pk"), fire_type="S"))) | ||
.only("id", "org_id", "current_session_uuid")[:1000] | ||
) | ||
if not batch: | ||
break | ||
|
||
sessions = FlowSession.objects.filter(uuid__in=[c.current_session_uuid for c in batch], status="W").only( | ||
"uuid", "created_on" | ||
) | ||
created_on_by_uuid = {s.uuid: s.created_on for s in sessions} | ||
|
||
to_create = [] | ||
for contact in batch: | ||
session_created_on = created_on_by_uuid.get(contact.current_session_uuid) | ||
if session_created_on: | ||
to_create.append( | ||
ContactFire( | ||
org_id=contact.org_id, | ||
contact=contact, | ||
fire_type="S", | ||
scope="", | ||
fire_on=session_created_on + timedelta(days=30) + timedelta(seconds=random.randint(0, 86400)), | ||
session_uuid=contact.current_session_uuid, | ||
) | ||
) | ||
else: | ||
contact.current_session_uuid = None | ||
contact.current_flow = None | ||
contact.save(update_fields=("current_session_uuid", "current_flow")) | ||
|
||
num_skipped += 1 | ||
|
||
if to_create: | ||
ContactFire.objects.bulk_create(to_create) | ||
num_created += len(to_create) | ||
print(f"Created {num_created} session expiration fires ({num_skipped} skipped)") | ||
|
||
|
||
def apply_manual(): # pragma: no cover | ||
from django.apps import apps | ||
|
||
create_session_expires_fires(apps, None) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("contacts", "0204_alter_contactfire_fire_type"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(create_session_expires_fires, migrations.RunPython.noop), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from datetime import timedelta | ||
|
||
from django.utils import timezone | ||
|
||
from temba.contacts.models import ContactFire | ||
from temba.flows.models import FlowSession | ||
from temba.tests import MigrationTest | ||
from temba.utils.uuid import uuid4 | ||
|
||
|
||
class CreateSessionExpiresFiresTest(MigrationTest): | ||
app = "contacts" | ||
migrate_from = "0204_alter_contactfire_fire_type" | ||
migrate_to = "0205_create_session_expires_fires" | ||
|
||
def setUpBeforeMigration(self, apps): | ||
def create_contact_and_sessions(name, phone, current_session_uuid): | ||
contact = self.create_contact(name, phone=phone, current_session_uuid=current_session_uuid) | ||
FlowSession.objects.create( | ||
uuid=uuid4(), | ||
contact=contact, | ||
status=FlowSession.STATUS_COMPLETED, | ||
output_url="http://sessions.com/123.json", | ||
created_on=timezone.now(), | ||
ended_on=timezone.now(), | ||
) | ||
FlowSession.objects.create( | ||
uuid=current_session_uuid, | ||
contact=contact, | ||
status=FlowSession.STATUS_WAITING, | ||
output_url="http://sessions.com/123.json", | ||
created_on=timezone.now(), | ||
) | ||
return contact | ||
|
||
# contacts with waiting sessions but no session expiration fire | ||
self.contact1 = create_contact_and_sessions("Ann", "+1234567001", "a0e707ef-ae06-4e39-a9b1-49eed0273dae") | ||
self.contact2 = create_contact_and_sessions("Bob", "+1234567002", "4a675e5d-ebc1-4fe7-be74-0450f550f8ee") | ||
|
||
# contact with waiting session and already has a session expiration fire | ||
self.contact3 = create_contact_and_sessions("Cat", "+1234567003", "a83a82f4-6a25-4662-a8e1-b53ee7d259a2") | ||
ContactFire.objects.create( | ||
org=self.org, | ||
contact=self.contact3, | ||
fire_type="S", | ||
scope="", | ||
fire_on=timezone.now() + timedelta(days=30), | ||
session_uuid="a83a82f4-6a25-4662-a8e1-b53ee7d259a2", | ||
) | ||
|
||
# contact with no waiting session | ||
self.contact4 = self.create_contact("Dan", phone="+1234567004") | ||
|
||
# contact with session mismatch | ||
self.contact5 = self.create_contact( | ||
"Dan", phone="+1234567004", current_session_uuid="ffca65c7-42ac-40cd-bef0-63aedc099ec9" | ||
) | ||
FlowSession.objects.create( | ||
uuid="80466ed4-de5c-49e8-acad-2432b4e9cdf9", | ||
contact=self.contact5, | ||
status=FlowSession.STATUS_WAITING, | ||
output_url="http://sessions.com/123.json", | ||
created_on=timezone.now(), | ||
) | ||
|
||
def test_migration(self): | ||
def assert_session_expire(contact): | ||
self.assertTrue(contact.fires.exists()) | ||
|
||
session = contact.sessions.filter(status="W").get() | ||
fire = contact.fires.get() | ||
|
||
self.assertEqual(fire.org, contact.org) | ||
self.assertEqual(fire.fire_type, "S") | ||
self.assertEqual(fire.scope, "") | ||
self.assertGreaterEqual(fire.fire_on, session.created_on + timedelta(days=30)) | ||
self.assertLess(fire.fire_on, session.created_on + timedelta(days=31)) | ||
self.assertEqual(fire.session_uuid, session.uuid) | ||
|
||
assert_session_expire(self.contact1) | ||
assert_session_expire(self.contact2) | ||
assert_session_expire(self.contact3) | ||
|
||
self.assertFalse(self.contact4.fires.exists()) | ||
self.assertFalse(self.contact5.fires.exists()) | ||
|
||
self.contact5.refresh_from_db() | ||
self.assertIsNone(self.contact5.current_session_uuid) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
temba/flows/migrations/0379_remove_flowrun_flows_run_active_or_waiting_has_session.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 5.1.4 on 2025-02-26 20:32 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("flows", "0378_alter_flowrun_session_alter_flowsession_status"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveConstraint( | ||
model_name="flowrun", | ||
name="flows_run_active_or_waiting_has_session", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.