from string import Formatter from django.core.exceptions import ValidationError ALERT_TAGS = ('amount_rial', 'customer_name', 'customer_mobile', 'received_at', 'paid_date', 'receipt_number', 'receipt_link', 'revision_version', 'manager_name') DEFAULT_RECEIPT_SMS = 'فیش واریزی به مبلغ {amount_rial} ریال توسط {customer_name} به شماره تماس {customer_mobile} ارسال شد.' DEFAULT_RECEIPT_CAPTION = ('فیش واریزی جدید\nمشتری: {customer_name}\nشماره تماس: {customer_mobile}\n' 'مبلغ: {amount_rial} ریال\nزمان ارسال: {received_at}\nتاریخ واریز: {paid_date}\n' 'شماره فیش: {receipt_number} · نسخه {revision_version}\n{receipt_link}') ALERT_TEMPLATE_HELP = 'تگ‌های مجاز: ' + '، '.join('{' + tag + '}' for tag in ALERT_TAGS) def validate_alert_template(value): if not value.strip(): raise ValidationError('متن اعلان نمی‌تواند خالی باشد.') try: for _, field, spec, conversion in Formatter().parse(value): if field is not None and (field not in ALERT_TAGS or spec or conversion): raise ValueError except ValueError: raise ValidationError('قالب معتبر نیست؛ فقط تگ‌های معرفی‌شده و بدون قالب‌بندی اضافی مجازند.')