Skip to content

Commit

Permalink
Remove deleting of flow runs
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Feb 10, 2025
1 parent 00e1244 commit bd18df3
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 238 deletions.
2 changes: 1 addition & 1 deletion temba/contacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ def _full_release(self):
delete_in_batches(self.channel_events.all())

for run in self.runs.all():
run.delete(interrupt=False) # don't try interrupting sessions that are about to be deleted
run.delete()

for session in self.sessions.all():
session.delete()
Expand Down
13 changes: 0 additions & 13 deletions temba/flows/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,19 +1197,6 @@ def convert_result(result):
"exit_type": FlowRunReadSerializer.EXIT_TYPES.get(self.status),
}

def delete(self, interrupt: bool = True):
"""
Deletes this run, decrementing it from result category counts
"""
with transaction.atomic():
self.delete_from_results = True
self.save(update_fields=("delete_from_results",))

if interrupt and self.session and self.session.status == FlowSession.STATUS_WAITING:
mailroom.queue_interrupt(self.org, sessions=[self.session])

super().delete()

def __repr__(self): # pragma: no cover
return f"<FlowRun: id={self.id} flow={self.flow.name}>"

Expand Down
143 changes: 1 addition & 142 deletions temba/flows/tests/test_run.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from datetime import datetime, timezone as tzone
from unittest.mock import patch
from uuid import UUID

from django.utils import timezone

from temba.flows.models import FlowRun, FlowSession, FlowStart, FlowStartCount
from temba.flows.models import FlowRun, FlowSession
from temba.tests import TembaTest, matchers
from temba.tests.engine import MockSessionWriter
from temba.utils.uuid import uuid4
Expand Down Expand Up @@ -175,146 +174,6 @@ def test_as_archive_json(self):
self.assertIsNone(run_json["exit_type"])
self.assertIsNone(run_json["exited_on"])

def _check_deletion(self, by_archiver: bool, expected: dict, session_completed=True):
"""
Runs our favorites flow, then deletes the run and asserts our final state
"""

flow = self.get_flow("favorites_v13")
flow_nodes = flow.get_definition()["nodes"]
color_prompt = flow_nodes[0]
color_split = flow_nodes[2]
beer_prompt = flow_nodes[3]
beer_split = flow_nodes[5]
name_prompt = flow_nodes[6]
name_split = flow_nodes[7]
end_prompt = flow_nodes[8]

start = FlowStart.create(flow, self.admin, contacts=[self.contact])
if session_completed:
(
MockSessionWriter(self.contact, flow, start)
.visit(color_prompt)
.send_msg("What is your favorite color?", self.channel)
.visit(color_split)
.wait()
.resume(msg=self.create_incoming_msg(self.contact, "blue"))
.set_result("Color", "blue", "Blue", "blue")
.visit(beer_prompt, exit_index=2)
.send_msg("Good choice, I like Blue too! What is your favorite beer?")
.visit(beer_split)
.wait()
.resume(msg=self.create_incoming_msg(self.contact, "primus"))
.set_result("Beer", "primus", "Primus", "primus")
.visit(name_prompt, exit_index=2)
.send_msg("Mmmmm... delicious Turbo King. Lastly, what is your name?")
.visit(name_split)
.wait()
.resume(msg=self.create_incoming_msg(self.contact, "Ryan Lewis"))
.visit(end_prompt)
.complete()
.save()
)
else:
(
MockSessionWriter(self.contact, flow, start)
.visit(color_prompt)
.send_msg("What is your favorite color?", self.channel)
.visit(color_split)
.wait()
.resume(msg=self.create_incoming_msg(self.contact, "blue"))
.set_result("Color", "blue", "Blue", "blue")
.visit(beer_prompt, exit_index=2)
.send_msg("Good choice, I like Blue too! What is your favorite beer?")
.visit(beer_split)
.wait()
.resume(msg=self.create_incoming_msg(self.contact, "primus"))
.set_result("Beer", "primus", "Primus", "primus")
.visit(name_prompt, exit_index=2)
.send_msg("Mmmmm... delicious Turbo King. Lastly, what is your name?")
.visit(name_split)
.wait()
.save()
)

run = FlowRun.objects.get(contact=self.contact)
if by_archiver:
super(FlowRun, run).delete() # delete_from_counts left unset
else:
run.delete() # delete_from_counts updated to true

self.assertEqual(expected["start_count"], FlowStartCount.get_count(start))
self.assertEqual(expected["run_count"], flow.get_run_stats())

self.assertFalse(FlowRun.objects.filter(id=run.id).exists())

@patch("temba.mailroom.queue_interrupt")
def test_delete_by_user_with_complete_session(self, mock_queue_interrupt):
self._check_deletion(
by_archiver=False,
expected={
"start_count": 1, # unchanged
"run_count": {
"total": 0,
"status": {
"active": 0,
"waiting": 0,
"completed": 0,
"expired": 0,
"interrupted": 0,
"failed": 0,
},
"completion": 0,
},
},
)
self.assertFalse(mock_queue_interrupt.called)

@patch("temba.mailroom.queue_interrupt")
def test_delete_by_user_without_complete_session(self, mock_queue_interrupt):
self._check_deletion(
by_archiver=False,
expected={
"start_count": 1, # unchanged
"run_count": {
"total": 0,
"status": {
"active": 0,
"waiting": 0,
"completed": 0,
"expired": 0,
"interrupted": 0,
"failed": 0,
},
"completion": 0,
},
},
session_completed=False,
)
mock_queue_interrupt.assert_called_once()

@patch("temba.mailroom.queue_interrupt")
def test_delete_by_archiver(self, mock_queue_interrupt):
self._check_deletion(
by_archiver=True,
expected={
"start_count": 1, # unchanged
"run_count": { # unchanged
"total": 1,
"status": {
"active": 0,
"waiting": 0,
"completed": 1,
"expired": 0,
"interrupted": 0,
"failed": 0,
},
"completion": 100,
},
},
)
self.assertFalse(mock_queue_interrupt.called)

def test_big_ids(self):
# create a session and run with big ids
session = FlowSession.objects.create(
Expand Down
40 changes: 0 additions & 40 deletions temba/flows/tests/test_runcrudl.py

This file was deleted.

3 changes: 1 addition & 2 deletions temba/flows/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .views import FlowCRUDL, FlowLabelCRUDL, FlowRunCRUDL, FlowSessionCRUDL, FlowStartCRUDL
from .views import FlowCRUDL, FlowLabelCRUDL, FlowSessionCRUDL, FlowStartCRUDL

urlpatterns = FlowCRUDL().as_urlpatterns()
urlpatterns += FlowLabelCRUDL().as_urlpatterns()
urlpatterns += FlowRunCRUDL().as_urlpatterns()
urlpatterns += FlowSessionCRUDL().as_urlpatterns()
urlpatterns += FlowStartCRUDL().as_urlpatterns()
15 changes: 1 addition & 14 deletions temba/flows/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from temba import mailroom
from temba.channels.models import Channel
from temba.contacts.models import URN
from temba.flows.models import Flow, FlowRun, FlowSession, FlowStart
from temba.flows.models import Flow, FlowSession, FlowStart
from temba.ivr.models import Call
from temba.orgs.models import IntegrationType, Org
from temba.orgs.views.base import (
Expand Down Expand Up @@ -149,19 +149,6 @@ def get(self, request, *args, **kwargs):
return JsonResponse(output, json_dumps_params=dict(indent=2))


class FlowRunCRUDL(SmartCRUDL):
actions = ("delete",)
model = FlowRun

class Delete(ModalFormMixin, OrgObjPermsMixin, SmartDeleteView):
fields = ("id",)
success_message = None

def post(self, request, *args, **kwargs):
self.get_object().delete()
return HttpResponse()


class FlowCRUDL(SmartCRUDL):
model = Flow
actions = (
Expand Down
2 changes: 0 additions & 2 deletions temba/settings_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@
"csv_imports.importtask.*",
"flows.flow.*",
"flows.flowlabel.*",
"flows.flowrun_delete",
"flows.flowrun_list",
"flows.flowstart.*",
"globals.global.*",
Expand Down Expand Up @@ -532,7 +531,6 @@
"csv_imports.importtask.*",
"flows.flow.*",
"flows.flowlabel.*",
"flows.flowrun_delete",
"flows.flowrun_list",
"flows.flowstart_create",
"flows.flowstart_list",
Expand Down
24 changes: 0 additions & 24 deletions templates/flows/flow_results.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,6 @@
document.querySelector("temba-flow-details").refresh();
}

function deleteRun(id) {
var dialog = document.getElementById("confirm-run-delete");
dialog.runId = id;
dialog.open = true;
}

function performDelete(evt) {
var dialog = document.getElementById("confirm-run-delete");
dialog.open = false;

if (evt.detail.button.destructive) {
fetchAjax('/flowrun/delete/' + dialog.runId + '/', {
method: 'POST',
}).then(function(response) {
var runList = document.querySelector("temba-run-list");
if (runList) {
runList.removeRun(dialog.runId);
var details = document.querySelector("temba-flow-details");
details.refresh();
}
});
}
}

function getChart(key, name) {
var chart = charts[key];
if (!chart) {
Expand Down

0 comments on commit bd18df3

Please sign in to comment.