notification_template.py 16 KB

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