Skip to content

Commit

Permalink
Merge pull request #5444 from nyaruka/note_vs_body_pt3
Browse files Browse the repository at this point in the history
Add data migration to copy body to ticket on open ticket event
  • Loading branch information
rowanseymour authored Aug 8, 2024
2 parents e0c041d + d7f6810 commit 7d5e552
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions temba/api/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1596,10 +1596,14 @@ class TicketReadSerializer(ReadSerializer):
opened_in = fields.FlowField()
modified_on = serializers.DateTimeField(default_timezone=tzone.utc)
closed_on = serializers.DateTimeField(default_timezone=tzone.utc)
body = serializers.SerializerMethodField() # deprecated

def get_status(self, obj):
return self.STATUSES.get(obj.status)

def get_body(self, obj):
return None

class Meta:
model = Ticket
fields = (
Expand Down
2 changes: 1 addition & 1 deletion temba/mailroom/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def from_ticket_event(cls, org: Org, user: User, obj: TicketEvent) -> dict:
"closed_on": ticket.closed_on.isoformat() if ticket.closed_on else None,
"topic": _topic(ticket.topic) if ticket.topic else None,
"status": ticket.status,
"body": ticket.body,
"body": None,
},
"created_on": get_event_time(obj).isoformat(),
"created_by": _user(obj.created_by) if obj.created_by else None,
Expand Down
27 changes: 27 additions & 0 deletions temba/tickets/migrations/0062_move_body_to_open_note.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.0.8 on 2024-08-08 14:58

from django.db import migrations


def move_body_to_open_note(apps, schema_editor):
Ticket = apps.get_model("tickets", "Ticket")

num_updated = 0

for ticket in Ticket.objects.exclude(body=None).exclude(body=""):
ticket.events.filter(event_type="O").update(note=ticket.body)

num_updated += 1

if num_updated % 1000 == 0: # pragma: no cover
print(f" > updated {num_updated} tickets")

if num_updated:
print(f" > Updated {num_updated} tickets")


class Migration(migrations.Migration):

dependencies = [("tickets", "0061_alter_ticket_body_alter_ticketevent_note")]

operations = [migrations.RunPython(move_body_to_open_note, migrations.RunPython.noop)]
27 changes: 26 additions & 1 deletion temba/tickets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from temba.contacts.models import Contact, ContactField, ContactURN
from temba.orgs.models import Export
from temba.tests import CRUDLTestMixin, TembaTest, matchers, mock_mailroom
from temba.tests import CRUDLTestMixin, MigrationTest, TembaTest, matchers, mock_mailroom
from temba.utils.dates import datetime_to_timestamp
from temba.utils.uuid import uuid4

Expand Down Expand Up @@ -1396,3 +1396,28 @@ def _record_last_close(self, org, d: date, seconds: int, undo: bool = False):
TicketDailyTiming.objects.create(
count_type=TicketDailyTiming.TYPE_LAST_CLOSE, scope=f"o:{org.id}", day=d, count=count, seconds=seconds
)


class MoveBodyToOpenNoteTest(MigrationTest):
app = "tickets"
migrate_from = "0061_alter_ticket_body_alter_ticketevent_note"
migrate_to = "0062_move_body_to_open_note"

def setUpBeforeMigration(self, apps):
contact = self.create_contact("Bob", urns=["twitter:bobby"])

self.ticket1 = self.create_ticket(contact)
self.ticket2 = self.create_ticket(contact)
self.ticket2.body = "I was a body"
self.ticket2.save(update_fields=("body",))

def test_migration(self):
self.ticket1.refresh_from_db()
self.ticket2.refresh_from_db()

# body unchanged
self.assertIsNone(self.ticket1.body)
self.assertEqual("I was a body", self.ticket2.body)

self.assertEqual(None, self.ticket1.events.get(event_type="O").note)
self.assertEqual("I was a body", self.ticket2.events.get(event_type="O").note)

0 comments on commit 7d5e552

Please sign in to comment.