from django.db import migrations, models EVENT = "UNIT_SUPERVISOR_NEW_TICKET" TITLE = "اطلاع تیکت جدید به سرپرستان واحد" BODY = "تیکت جدید {ticket_number}\nتلفن: {customer_mobile}\nموضوع: {subject}\nدسته: {category_name}\nارجاع: {expert_name}" TAGS = [ "magic_link", "first_name", "last_name", "recipient_name", "unit_name", "company_name", "ticket_number", "subject", "province", "city", "expert_name", "customer_mobile", "category_name", ] def seed_supervisor_template(apps, schema_editor): sms_template = apps.get_model("notifications", "SMSTemplate") sms_template.objects.get_or_create( unit=None, event_type=EVENT, defaults={"title": TITLE, "body": BODY, "allowed_tags": TAGS, "is_active": True}, ) for template in sms_template.objects.all().iterator(): allowed_tags = list(template.allowed_tags or []) updated_tags = list(dict.fromkeys([*allowed_tags, "customer_mobile", "category_name"])) if updated_tags != allowed_tags: template.allowed_tags = updated_tags template.save(update_fields=["allowed_tags"]) def remove_supervisor_template(apps, schema_editor): apps.get_model("notifications", "SMSTemplate").objects.filter( unit__isnull=True, event_type=EVENT, title=TITLE, body=BODY ).delete() class Migration(migrations.Migration): dependencies = [ ("notifications", "0011_notification_delivery_tracking"), ("staff", "0011_organizationalunit_supervisor_mobile_4"), ] operations = [ migrations.AlterField( model_name="smstemplate", name="event_type", field=models.CharField( choices=[ ("CUSTOMER_TICKET_CREATED", "ثبت تیکت توسط مشتری"), ("STAFF_TICKET_CREATED", "ثبت تیکت توسط کارشناس"), ("STAFF_REPLY", "پاسخ کارشناس"), (EVENT, TITLE), ], max_length=50, ), ), migrations.RunPython(seed_supervisor_template, remove_supervisor_template), ]