notification_template.py 17 KB

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