notification_template.py 17 KB

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