fix some bugs

This commit is contained in:
javad committed 2026-09-15 09:50:17 +03:30
1 parent b8b3aba855
commit 963fd839dc
22 files changed
+570 -33

No files matched your search

@@ -23,10 +23,15 @@ def _magic_link(job, reason):
token,link = TicketMagicLink.issue_with_record(job.ticket, reason=reason, created_by=job.staff_user or job.ticket.owner or job.ticket.created_by)
return link, f"{settings.PUBLIC_BASE_URL}/t/{token}/"
def _sms_context(job,magic_link):
profile=getattr(job.customer,"customer_profile",None)
def _sms_context(job,magic_link=""):
recipient = job.customer or job.ticket.customer
profile=getattr(recipient,"customer_profile",None)
unit=job.ticket.owning_unit
return {"magic_link":magic_link,"first_name":getattr(profile,"first_name","") or "","last_name":getattr(profile,"last_name","") or "","recipient_name":" ".join(filter(None,[getattr(profile,"first_name","") or "",getattr(profile,"last_name","") or ""])),"unit_name":unit.name if unit else "","company_name":getattr(settings,"COMPANY_NAME","لوله گستر گلپایگان"),"ticket_number":job.ticket.ticket_number,"subject":job.ticket.subject,"province":job.ticket.province or getattr(profile,"province","") or "","city":job.ticket.city or getattr(profile,"city","") or "","expert_name":job.ticket.assigned_expert_name}
customer_profile=getattr(job.ticket.customer,"customer_profile",None)
first_name=getattr(profile,"first_name","") or ""
last_name=getattr(profile,"last_name","") or ""
recipient_name=" ".join(filter(None,[first_name,last_name])) or "ثبت نشده"
return {"magic_link":magic_link,"first_name":first_name,"last_name":last_name,"recipient_name":recipient_name,"unit_name":unit.name if unit else "","company_name":getattr(settings,"COMPANY_NAME","لوله گستر گلپایگان"),"ticket_number":job.ticket.ticket_number,"subject":job.ticket.subject,"province":job.ticket.province or getattr(customer_profile,"province","") or "","city":job.ticket.city or getattr(customer_profile,"city","") or "","expert_name":job.ticket.assigned_expert_name,"customer_mobile":job.ticket.customer.local_mobile or "ثبت نشده","category_name":job.ticket.category.title}
def _render_sms(event_type,context,unit=None):
from string import Formatter
@@ -65,10 +70,7 @@ def _send_sms(job):
recipient = job.payload.get("recipient_mobile", "")
if not recipient:
raise ValueError("Unit supervisor mobile is not configured")
subject = " ".join(job.ticket.subject.split())[:80] or "بدون موضوع"
category = " ".join(job.ticket.category.title.split())[:50]
customer_mobile = job.ticket.customer.local_mobile or "ثبت نشده"
message = f"تیکت جدید {job.ticket.ticket_number}\nتلفن: {customer_mobile}\nموضوع: {subject}\nدسته: {category}"
message = _render_sms(job.event_type, _sms_context(job), job.ticket.owning_unit)
return get_sms_provider().send_message(recipient, message), issued
if job.event_type == "CUSTOMER_TICKET_CREATED":
issued, url = _magic_link(job, "customer_created_ticket")
@@ -0,0 +1,56 @@
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),
]
+2 -1
View File
@@ -5,7 +5,7 @@ from apps.core.models import PersianTextNormalizationMixin
def sms_template_tags():
return ["magic_link", "first_name", "last_name", "recipient_name", "unit_name", "company_name", "ticket_number", "subject", "province", "city", "expert_name"]
return ["magic_link", "first_name", "last_name", "recipient_name", "unit_name", "company_name", "ticket_number", "subject", "province", "city", "expert_name", "customer_mobile", "category_name"]
class SMSTemplate(PersianTextNormalizationMixin, models.Model):
persian_text_fields={"title":False,"body":True}
@@ -13,6 +13,7 @@ class SMSTemplate(PersianTextNormalizationMixin, models.Model):
CUSTOMER_TICKET_CREATED="CUSTOMER_TICKET_CREATED","ثبت تیکت توسط مشتری"
STAFF_TICKET_CREATED="STAFF_TICKET_CREATED","ثبت تیکت توسط کارشناس"
STAFF_REPLY="STAFF_REPLY","پاسخ کارشناس"
UNIT_SUPERVISOR_NEW_TICKET="UNIT_SUPERVISOR_NEW_TICKET","اطلاع تیکت جدید به سرپرستان واحد"
unit=models.ForeignKey("staff.OrganizationalUnit",on_delete=models.CASCADE,null=True,blank=True,related_name="sms_templates",help_text="خالی یعنی الگوی عمومی و fallback.")
event_type=models.CharField(max_length=50,choices=Event.choices)
title=models.CharField(max_length=120)