notification_template.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. """Pydantic schemas for notification templates."""
  2. from datetime import datetime
  3. from pydantic import BaseModel, Field
  4. from backend.app.core.compat import StrEnum
  5. from backend.app.schemas.not_null import IgnoresNullForRequiredColumns
  6. class EventType(StrEnum):
  7. """Supported notification event types."""
  8. PRINT_START = "print_start"
  9. PRINT_COMPLETE = "print_complete"
  10. PRINT_FAILED = "print_failed"
  11. PRINT_STOPPED = "print_stopped"
  12. PRINT_PROGRESS = "print_progress"
  13. PRINT_MISSING_SPOOL_ASSIGNMENT = "print_missing_spool_assignment"
  14. BILLING_CHARGE_FAILED = "billing_charge_failed"
  15. PRINTER_OFFLINE = "printer_offline"
  16. PRINTER_ERROR = "printer_error"
  17. FILAMENT_LOW = "filament_low"
  18. MAINTENANCE_DUE = "maintenance_due"
  19. AMS_HUMIDITY_HIGH = "ams_humidity_high"
  20. AMS_TEMPERATURE_HIGH = "ams_temperature_high"
  21. AMS_DRYING_SUSPENDED = "ams_drying_suspended"
  22. BED_COOLED = "bed_cooled"
  23. HA_SENSOR_ALERT = "ha_sensor_alert"
  24. TEST = "test"
  25. # Available variables for each event type
  26. EVENT_VARIABLES: dict[str, list[str]] = {
  27. # Post-print outcome request (#1898): good/reject are the one-tap links,
  28. # confirm_url opens the archive's confirmation dialog.
  29. "print_confirm_request": [
  30. "printer",
  31. "filename",
  32. "confirm_url",
  33. "good_url",
  34. "reject_url",
  35. "archive_id",
  36. "finish_photo_url",
  37. "timestamp",
  38. "app_name",
  39. ],
  40. "print_start": ["printer", "filename", "estimated_time", "eta", "timestamp", "app_name"],
  41. "print_complete": [
  42. "printer",
  43. "filename",
  44. "duration",
  45. "filament_grams",
  46. "filament_details",
  47. "finish_photo_url",
  48. "timestamp",
  49. "app_name",
  50. ],
  51. "print_failed": [
  52. "printer",
  53. "filename",
  54. "duration",
  55. "filament_grams",
  56. "filament_details",
  57. "progress",
  58. "reason",
  59. "finish_photo_url",
  60. "timestamp",
  61. "app_name",
  62. ],
  63. "print_stopped": [
  64. "printer",
  65. "filename",
  66. "duration",
  67. "filament_grams",
  68. "filament_details",
  69. "progress",
  70. "finish_photo_url",
  71. "timestamp",
  72. "app_name",
  73. ],
  74. "print_progress": ["printer", "filename", "progress", "remaining_time", "eta", "timestamp", "app_name"],
  75. "print_missing_spool_assignment": [
  76. "printer",
  77. "missing_slots",
  78. "missing_slot_details",
  79. "timestamp",
  80. "app_name",
  81. ],
  82. "billing_charge_failed": ["printer", "filename", "archive_id", "error", "timestamp", "app_name"],
  83. "printer_offline": ["printer", "timestamp", "app_name"],
  84. "printer_error": ["printer", "error_type", "error_detail", "timestamp", "app_name"],
  85. "ai_failure_detection": ["printer", "task_name", "confidence", "action", "timestamp", "app_name"],
  86. "plate_not_empty": ["printer", "difference_percent", "timestamp", "app_name"],
  87. "plate_clear_required": ["printer", "timestamp", "app_name"],
  88. "filament_low": ["printer", "slot", "remaining_percent", "color", "timestamp", "app_name"],
  89. "maintenance_due": ["printer", "items", "timestamp", "app_name"],
  90. "ams_humidity_high": ["printer", "ams_label", "humidity", "threshold", "timestamp", "app_name"],
  91. "ams_temperature_high": ["printer", "ams_label", "temperature", "threshold", "timestamp", "app_name"],
  92. "ams_drying_suspended": [
  93. "printer",
  94. "ams_label",
  95. "humidity",
  96. "threshold",
  97. "cycles",
  98. "timestamp",
  99. "app_name",
  100. ],
  101. "bed_cooled": ["printer", "bed_temp", "threshold", "filename", "timestamp", "app_name"],
  102. "ha_sensor_alert": ["printer", "sensor", "state", "timestamp", "app_name"],
  103. "location_ha_sensor_alert": ["location", "sensor", "state", "timestamp", "app_name"],
  104. "first_layer_complete": ["printer", "filename", "total_layers", "timestamp", "app_name"],
  105. "test": ["app_name", "timestamp"],
  106. # Queue notifications
  107. "queue_job_added": ["job_name", "target", "timestamp", "app_name"],
  108. "queue_job_assigned": ["job_name", "printer", "target_model", "timestamp", "app_name"],
  109. "queue_job_started": ["printer", "job_name", "estimated_time", "eta", "timestamp", "app_name"],
  110. "queue_job_waiting": ["job_name", "target_model", "waiting_reason", "timestamp", "app_name"],
  111. "queue_job_skipped": ["printer", "job_name", "reason", "timestamp", "app_name"],
  112. "queue_job_failed": ["printer", "job_name", "reason", "timestamp", "app_name"],
  113. "queue_completed": ["completed_count", "timestamp", "app_name"],
  114. "stock_reorder_alert": [
  115. "material",
  116. "subtype",
  117. "brand",
  118. "color",
  119. "stock_g",
  120. "rate_g_day",
  121. "days_left",
  122. "timestamp",
  123. "app_name",
  124. ],
  125. "stock_break_alert": [
  126. "material",
  127. "subtype",
  128. "brand",
  129. "color",
  130. "stock_g",
  131. "rate_g_day",
  132. "days_left",
  133. "lead_time_days",
  134. "timestamp",
  135. "app_name",
  136. ],
  137. # User management notifications
  138. "user_created": ["username", "login_url", "app_name", "timestamp"],
  139. "password_reset": ["username", "app_name", "timestamp"],
  140. # User email print notifications
  141. "user_print_start": ["username", "printer", "filename", "timestamp", "app_name"],
  142. "user_print_complete": ["username", "printer", "filename", "finish_photo_url", "timestamp", "app_name"],
  143. "user_print_failed": ["username", "printer", "filename", "finish_photo_url", "timestamp", "app_name"],
  144. "user_print_stopped": ["username", "printer", "filename", "finish_photo_url", "timestamp", "app_name"],
  145. }
  146. # Events whose real notification can carry a camera snapshot — mirrors which
  147. # NotificationService.on_* methods accept image_data.
  148. PHOTO_CAPABLE_EVENTS: set[str] = {
  149. "print_confirm_request",
  150. "print_start",
  151. "print_complete",
  152. "print_failed",
  153. "print_stopped",
  154. "print_progress",
  155. "printer_error",
  156. "ai_failure_detection",
  157. "plate_not_empty",
  158. "first_layer_complete",
  159. "user_print_complete",
  160. "user_print_failed",
  161. "user_print_stopped",
  162. "test",
  163. }
  164. # Sample data for previewing templates
  165. SAMPLE_DATA: dict[str, dict[str, str]] = {
  166. "print_start": {
  167. "printer": "Bambu X1C",
  168. "filename": "Benchy.3mf",
  169. "estimated_time": "1h 23m",
  170. "eta": "15:53",
  171. "timestamp": "2024-01-15 14:30",
  172. "app_name": "Bambuddy",
  173. },
  174. "print_complete": {
  175. "printer": "Bambu X1C",
  176. "filename": "Benchy.3mf",
  177. "duration": "1h 18m",
  178. "filament_grams": "15.2",
  179. "filament_details": "AMS-A T1 PLA: 12.4g | AMS-A T3 PETG: 2.8g",
  180. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_154800_abc12345.jpg",
  181. "timestamp": "2024-01-15 15:48",
  182. "app_name": "Bambuddy",
  183. },
  184. "print_failed": {
  185. "printer": "Bambu X1C",
  186. "filename": "Benchy.3mf",
  187. "duration": "0h 45m",
  188. "filament_grams": "7.6",
  189. "filament_details": "AMS-A T1 PLA: 7.6g",
  190. "progress": "50",
  191. "reason": "Filament runout",
  192. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_151500_def67890.jpg",
  193. "timestamp": "2024-01-15 15:15",
  194. "app_name": "Bambuddy",
  195. },
  196. "print_stopped": {
  197. "printer": "Bambu X1C",
  198. "filename": "Benchy.3mf",
  199. "duration": "0h 30m",
  200. "filament_grams": "4.6",
  201. "filament_details": "AMS-A T2 PLA: 4.6g",
  202. "progress": "30",
  203. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_150000_ghi11223.jpg",
  204. "timestamp": "2024-01-15 15:00",
  205. "app_name": "Bambuddy",
  206. },
  207. "print_progress": {
  208. "printer": "Bambu X1C",
  209. "filename": "Benchy.3mf",
  210. "progress": "50",
  211. "remaining_time": "0h 41m",
  212. "eta": "15:41",
  213. "timestamp": "2024-01-15 15:00",
  214. "app_name": "Bambuddy",
  215. },
  216. "print_missing_spool_assignment": {
  217. "printer": "Bambu X1C",
  218. "missing_slots": "A1, A3",
  219. "missing_slot_details": "- A1: PLA Basic\n- A3: PETG HF",
  220. "timestamp": "2024-01-15 14:30",
  221. "app_name": "Bambuddy",
  222. },
  223. "billing_charge_failed": {
  224. "printer": "Bambu X1C",
  225. "filename": "Benchy.3mf",
  226. "archive_id": "123",
  227. "error": "The transaction could not be persisted",
  228. "timestamp": "2024-01-15 15:48",
  229. "app_name": "Bambuddy",
  230. },
  231. "printer_offline": {
  232. "printer": "Bambu X1C",
  233. "timestamp": "2024-01-15 14:30",
  234. "app_name": "Bambuddy",
  235. },
  236. "printer_error": {
  237. "printer": "Bambu X1C",
  238. "error_type": "AMS Error",
  239. "error_detail": "Filament slot 1 jammed",
  240. "timestamp": "2024-01-15 14:30",
  241. "app_name": "Bambuddy",
  242. },
  243. "ai_failure_detection": {
  244. "printer": "Bambu X1C",
  245. "task_name": "Benchy.3mf",
  246. "confidence": "0.87",
  247. "action": "pause",
  248. "timestamp": "2024-01-15 15:10",
  249. "app_name": "Bambuddy",
  250. },
  251. "print_confirm_request": {
  252. "printer": "Bambu X1C",
  253. "filename": "Benchy",
  254. "confirm_url": "http://bambuddy.local:8000/archives?confirm=42",
  255. "good_url": "http://bambuddy.local:8000/api/v1/archives/confirm/sample-token/good",
  256. "reject_url": "http://bambuddy.local:8000/api/v1/archives/confirm/sample-token/reject",
  257. "archive_id": "42",
  258. "finish_photo_url": "/api/v1/archives/42/photos/finish_20240115_143500_abc12345.jpg",
  259. "timestamp": "2024-01-15 14:35",
  260. "app_name": "Bambuddy",
  261. },
  262. "plate_not_empty": {
  263. "printer": "Bambu X1C",
  264. "difference_percent": "12.4",
  265. "timestamp": "2024-01-15 09:00",
  266. "app_name": "Bambuddy",
  267. },
  268. "plate_clear_required": {
  269. "printer": "Bambu X1C",
  270. "timestamp": "2024-01-15 15:48",
  271. "app_name": "Bambuddy",
  272. },
  273. "filament_low": {
  274. "printer": "Bambu X1C",
  275. "slot": "1",
  276. "remaining_percent": "15",
  277. "color": "Black PLA",
  278. "timestamp": "2024-01-15 14:30",
  279. "app_name": "Bambuddy",
  280. },
  281. "maintenance_due": {
  282. "printer": "Bambu X1C",
  283. "items": "• Nozzle cleaning (OVERDUE)\n• Carbon rod lubrication (Soon)",
  284. "timestamp": "2024-01-15 14:30",
  285. "app_name": "Bambuddy",
  286. },
  287. "ams_humidity_high": {
  288. "printer": "Bambu X1C",
  289. "ams_label": "AMS-A",
  290. "humidity": "75",
  291. "threshold": "60",
  292. "timestamp": "2024-01-15 14:30",
  293. "app_name": "Bambuddy",
  294. },
  295. "ams_temperature_high": {
  296. "printer": "Bambu X1C",
  297. "ams_label": "AMS-A",
  298. "temperature": "42",
  299. "threshold": "35",
  300. "timestamp": "2024-01-15 14:30",
  301. "app_name": "Bambuddy",
  302. },
  303. "ams_drying_suspended": {
  304. "printer": "Bambu X1C",
  305. "ams_label": "AMS-A",
  306. "humidity": "16",
  307. "threshold": "14",
  308. "cycles": "2",
  309. "timestamp": "2024-01-15 14:30",
  310. "app_name": "Bambuddy",
  311. },
  312. "bed_cooled": {
  313. "printer": "Bambu X1C",
  314. "bed_temp": "34",
  315. "threshold": "35",
  316. "filename": "Benchy",
  317. "timestamp": "2024-01-15 14:30",
  318. "app_name": "Bambuddy",
  319. },
  320. "ha_sensor_alert": {
  321. "printer": "Bambu X1C",
  322. "sensor": "Enclosure Door",
  323. "state": "open",
  324. "timestamp": "2024-01-15 14:30",
  325. "app_name": "Bambuddy",
  326. },
  327. "location_ha_sensor_alert": {
  328. "location": "Drybox 1",
  329. "sensor": "Drybox 1 Humidity",
  330. "state": "68.00 %",
  331. "timestamp": "2024-01-15 14:30",
  332. "app_name": "Bambuddy",
  333. },
  334. "first_layer_complete": {
  335. "printer": "Bambu X1C",
  336. "filename": "Benchy.3mf",
  337. "total_layers": "184",
  338. "timestamp": "2024-01-15 14:35",
  339. "app_name": "Bambuddy",
  340. },
  341. "test": {
  342. "app_name": "Bambuddy",
  343. "timestamp": "2024-01-15 14:30",
  344. },
  345. # Queue notifications
  346. "queue_job_added": {
  347. "job_name": "Benchy.3mf",
  348. "target": "Bambu X1C",
  349. "timestamp": "2024-01-15 14:30",
  350. "app_name": "Bambuddy",
  351. },
  352. "queue_job_assigned": {
  353. "job_name": "Benchy.3mf",
  354. "printer": "Bambu X1C #1",
  355. "target_model": "X1C",
  356. "timestamp": "2024-01-15 14:30",
  357. "app_name": "Bambuddy",
  358. },
  359. "queue_job_started": {
  360. "printer": "Bambu X1C",
  361. "job_name": "Benchy.3mf",
  362. "estimated_time": "1h 23m",
  363. "eta": "15:53",
  364. "timestamp": "2024-01-15 14:30",
  365. "app_name": "Bambuddy",
  366. },
  367. "queue_job_waiting": {
  368. "job_name": "Benchy.3mf",
  369. "target_model": "X1C",
  370. "waiting_reason": "Printer1 (needs PLA)",
  371. "timestamp": "2024-01-15 14:30",
  372. "app_name": "Bambuddy",
  373. },
  374. "queue_job_skipped": {
  375. "printer": "Bambu X1C",
  376. "job_name": "Benchy.3mf",
  377. "reason": "Previous print failed",
  378. "timestamp": "2024-01-15 14:30",
  379. "app_name": "Bambuddy",
  380. },
  381. "queue_job_failed": {
  382. "printer": "Bambu X1C",
  383. "job_name": "Benchy.3mf",
  384. "reason": "Upload failed: connection timeout",
  385. "timestamp": "2024-01-15 14:30",
  386. "app_name": "Bambuddy",
  387. },
  388. "queue_completed": {
  389. "completed_count": "5",
  390. "timestamp": "2024-01-15 18:30",
  391. "app_name": "Bambuddy",
  392. },
  393. "stock_reorder_alert": {
  394. "material": "PLA",
  395. "subtype": "Matte",
  396. "brand": "Bambu Lab",
  397. "color": "Charcoal",
  398. "stock_g": "420",
  399. "rate_g_day": "18.5",
  400. "days_left": "22",
  401. "timestamp": "2024-01-15 14:30",
  402. "app_name": "Bambuddy",
  403. },
  404. "stock_break_alert": {
  405. "material": "PLA",
  406. "subtype": "Matte",
  407. "brand": "Bambu Lab",
  408. "color": "Charcoal",
  409. "stock_g": "160",
  410. "rate_g_day": "18.5",
  411. "days_left": "8",
  412. "lead_time_days": "14",
  413. "timestamp": "2024-01-15 14:30",
  414. "app_name": "Bambuddy",
  415. },
  416. # User management notifications
  417. "user_created": {
  418. "username": "john_doe",
  419. "login_url": "https://bambuddy.example.com/login",
  420. "app_name": "Bambuddy",
  421. "timestamp": "2024-01-15 14:30",
  422. },
  423. "password_reset": {
  424. "username": "john_doe",
  425. "app_name": "Bambuddy",
  426. "timestamp": "2024-01-15 14:30",
  427. },
  428. # User email print notifications
  429. "user_print_start": {
  430. "username": "john_doe",
  431. "printer": "Bambu X1C",
  432. "filename": "Benchy.3mf",
  433. "timestamp": "2024-01-15 14:30",
  434. "app_name": "Bambuddy",
  435. },
  436. "user_print_complete": {
  437. "username": "john_doe",
  438. "printer": "Bambu X1C",
  439. "filename": "Benchy.3mf",
  440. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_154800_abc12345.jpg",
  441. "timestamp": "2024-01-15 15:48",
  442. "app_name": "Bambuddy",
  443. },
  444. "user_print_failed": {
  445. "username": "john_doe",
  446. "printer": "Bambu X1C",
  447. "filename": "Benchy.3mf",
  448. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_151500_def67890.jpg",
  449. "timestamp": "2024-01-15 15:15",
  450. "app_name": "Bambuddy",
  451. },
  452. "user_print_stopped": {
  453. "username": "john_doe",
  454. "printer": "Bambu X1C",
  455. "filename": "Benchy.3mf",
  456. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_150000_ghi11223.jpg",
  457. "timestamp": "2024-01-15 15:15",
  458. "app_name": "Bambuddy",
  459. },
  460. }
  461. class NotificationTemplateBase(BaseModel):
  462. """Base schema for notification templates."""
  463. title_template: str = Field(..., min_length=1, max_length=200)
  464. body_template: str = Field(..., min_length=1, max_length=2000)
  465. class NotificationTemplateUpdate(IgnoresNullForRequiredColumns):
  466. """Schema for updating a notification template."""
  467. __orm_table__ = "notification_templates"
  468. title_template: str | None = Field(default=None, min_length=1, max_length=200)
  469. body_template: str | None = Field(default=None, min_length=1, max_length=2000)
  470. class NotificationTemplateResponse(NotificationTemplateBase):
  471. """Schema for notification template API responses."""
  472. id: int
  473. event_type: str
  474. name: str
  475. is_default: bool
  476. created_at: datetime
  477. updated_at: datetime
  478. class Config:
  479. from_attributes = True
  480. class EventVariablesResponse(BaseModel):
  481. """Response for available variables per event type."""
  482. event_type: str
  483. event_name: str
  484. variables: list[str]
  485. supports_photo: bool = False
  486. class TemplatePreviewRequest(BaseModel):
  487. """Request to preview a template with sample data."""
  488. event_type: str
  489. title_template: str
  490. body_template: str
  491. class TemplatePreviewResponse(BaseModel):
  492. """Response with rendered template preview."""
  493. title: str
  494. body: str