notification_template.py 16 KB

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