notification_template.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. "filament_low": ["printer", "slot", "remaining_percent", "color", "timestamp", "app_name"],
  72. "maintenance_due": ["printer", "items", "timestamp", "app_name"],
  73. "ams_humidity_high": ["printer", "ams_label", "humidity", "threshold", "timestamp", "app_name"],
  74. "ams_temperature_high": ["printer", "ams_label", "temperature", "threshold", "timestamp", "app_name"],
  75. "ams_drying_suspended": [
  76. "printer",
  77. "ams_label",
  78. "humidity",
  79. "threshold",
  80. "cycles",
  81. "timestamp",
  82. "app_name",
  83. ],
  84. "bed_cooled": ["printer", "bed_temp", "threshold", "filename", "timestamp", "app_name"],
  85. "ha_sensor_alert": ["printer", "sensor", "state", "timestamp", "app_name"],
  86. "location_ha_sensor_alert": ["location", "sensor", "state", "timestamp", "app_name"],
  87. "test": ["app_name", "timestamp"],
  88. # Queue notifications
  89. "queue_job_added": ["job_name", "target", "timestamp", "app_name"],
  90. "queue_job_assigned": ["job_name", "printer", "target_model", "timestamp", "app_name"],
  91. "queue_job_started": ["printer", "job_name", "estimated_time", "eta", "timestamp", "app_name"],
  92. "queue_job_waiting": ["job_name", "target_model", "waiting_reason", "timestamp", "app_name"],
  93. "queue_job_skipped": ["printer", "job_name", "reason", "timestamp", "app_name"],
  94. "queue_job_failed": ["printer", "job_name", "reason", "timestamp", "app_name"],
  95. "queue_completed": ["completed_count", "timestamp", "app_name"],
  96. # User management notifications
  97. "user_created": ["username", "password", "login_url", "app_name", "timestamp"],
  98. "password_reset": ["username", "password", "login_url", "app_name", "timestamp"],
  99. # User email print notifications
  100. "user_print_start": ["username", "printer", "filename", "timestamp", "app_name"],
  101. "user_print_complete": ["username", "printer", "filename", "timestamp", "app_name"],
  102. "user_print_failed": ["username", "printer", "filename", "timestamp", "app_name"],
  103. "user_print_stopped": ["username", "printer", "filename", "timestamp", "app_name"],
  104. }
  105. # Sample data for previewing templates
  106. SAMPLE_DATA: dict[str, dict[str, str]] = {
  107. "print_start": {
  108. "printer": "Bambu X1C",
  109. "filename": "Benchy.3mf",
  110. "estimated_time": "1h 23m",
  111. "eta": "15:53",
  112. "timestamp": "2024-01-15 14:30",
  113. "app_name": "Bambuddy",
  114. },
  115. "print_complete": {
  116. "printer": "Bambu X1C",
  117. "filename": "Benchy.3mf",
  118. "duration": "1h 18m",
  119. "filament_grams": "15.2",
  120. "filament_details": "AMS-A T1 PLA: 12.4g | AMS-A T3 PETG: 2.8g",
  121. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_154800_abc12345.jpg",
  122. "timestamp": "2024-01-15 15:48",
  123. "app_name": "Bambuddy",
  124. },
  125. "print_failed": {
  126. "printer": "Bambu X1C",
  127. "filename": "Benchy.3mf",
  128. "duration": "0h 45m",
  129. "filament_grams": "7.6",
  130. "filament_details": "AMS-A T1 PLA: 7.6g",
  131. "progress": "50",
  132. "reason": "Filament runout",
  133. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_151500_def67890.jpg",
  134. "timestamp": "2024-01-15 15:15",
  135. "app_name": "Bambuddy",
  136. },
  137. "print_stopped": {
  138. "printer": "Bambu X1C",
  139. "filename": "Benchy.3mf",
  140. "duration": "0h 30m",
  141. "filament_grams": "4.6",
  142. "filament_details": "AMS-A T2 PLA: 4.6g",
  143. "progress": "30",
  144. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_150000_ghi11223.jpg",
  145. "timestamp": "2024-01-15 15:00",
  146. "app_name": "Bambuddy",
  147. },
  148. "print_progress": {
  149. "printer": "Bambu X1C",
  150. "filename": "Benchy.3mf",
  151. "progress": "50",
  152. "remaining_time": "0h 41m",
  153. "eta": "15:41",
  154. "timestamp": "2024-01-15 15:00",
  155. "app_name": "Bambuddy",
  156. },
  157. "print_missing_spool_assignment": {
  158. "printer": "Bambu X1C",
  159. "missing_slots": "A1, A3",
  160. "missing_slot_details": "- A1: PLA Basic\n- A3: PETG HF",
  161. "timestamp": "2024-01-15 14:30",
  162. "app_name": "Bambuddy",
  163. },
  164. "billing_charge_failed": {
  165. "printer": "Bambu X1C",
  166. "filename": "Benchy.3mf",
  167. "archive_id": "123",
  168. "error": "The transaction could not be persisted",
  169. "timestamp": "2024-01-15 15:48",
  170. "app_name": "Bambuddy",
  171. },
  172. "printer_offline": {
  173. "printer": "Bambu X1C",
  174. "timestamp": "2024-01-15 14:30",
  175. "app_name": "Bambuddy",
  176. },
  177. "printer_error": {
  178. "printer": "Bambu X1C",
  179. "error_type": "AMS Error",
  180. "error_detail": "Filament slot 1 jammed",
  181. "timestamp": "2024-01-15 14:30",
  182. "app_name": "Bambuddy",
  183. },
  184. "filament_low": {
  185. "printer": "Bambu X1C",
  186. "slot": "1",
  187. "remaining_percent": "15",
  188. "color": "Black PLA",
  189. "timestamp": "2024-01-15 14:30",
  190. "app_name": "Bambuddy",
  191. },
  192. "maintenance_due": {
  193. "printer": "Bambu X1C",
  194. "items": "• Nozzle cleaning (OVERDUE)\n• Carbon rod lubrication (Soon)",
  195. "timestamp": "2024-01-15 14:30",
  196. "app_name": "Bambuddy",
  197. },
  198. "ams_humidity_high": {
  199. "printer": "Bambu X1C",
  200. "ams_label": "AMS-A",
  201. "humidity": "75",
  202. "threshold": "60",
  203. "timestamp": "2024-01-15 14:30",
  204. "app_name": "Bambuddy",
  205. },
  206. "ams_temperature_high": {
  207. "printer": "Bambu X1C",
  208. "ams_label": "AMS-A",
  209. "temperature": "42",
  210. "threshold": "35",
  211. "timestamp": "2024-01-15 14:30",
  212. "app_name": "Bambuddy",
  213. },
  214. "ams_drying_suspended": {
  215. "printer": "Bambu X1C",
  216. "ams_label": "AMS-A",
  217. "humidity": "16",
  218. "threshold": "14",
  219. "cycles": "2",
  220. "timestamp": "2024-01-15 14:30",
  221. "app_name": "Bambuddy",
  222. },
  223. "bed_cooled": {
  224. "printer": "Bambu X1C",
  225. "bed_temp": "34",
  226. "threshold": "35",
  227. "filename": "Benchy",
  228. "timestamp": "2024-01-15 14:30",
  229. "app_name": "Bambuddy",
  230. },
  231. "ha_sensor_alert": {
  232. "printer": "Bambu X1C",
  233. "sensor": "Enclosure Door",
  234. "state": "open",
  235. "timestamp": "2024-01-15 14:30",
  236. "app_name": "Bambuddy",
  237. },
  238. "location_ha_sensor_alert": {
  239. "location": "Drybox 1",
  240. "sensor": "Drybox 1 Humidity",
  241. "state": "68.00 %",
  242. "timestamp": "2024-01-15 14:30",
  243. "app_name": "Bambuddy",
  244. },
  245. "test": {
  246. "app_name": "Bambuddy",
  247. "timestamp": "2024-01-15 14:30",
  248. },
  249. # Queue notifications
  250. "queue_job_added": {
  251. "job_name": "Benchy.3mf",
  252. "target": "Bambu X1C",
  253. "timestamp": "2024-01-15 14:30",
  254. "app_name": "Bambuddy",
  255. },
  256. "queue_job_assigned": {
  257. "job_name": "Benchy.3mf",
  258. "printer": "Bambu X1C #1",
  259. "target_model": "X1C",
  260. "timestamp": "2024-01-15 14:30",
  261. "app_name": "Bambuddy",
  262. },
  263. "queue_job_started": {
  264. "printer": "Bambu X1C",
  265. "job_name": "Benchy.3mf",
  266. "estimated_time": "1h 23m",
  267. "eta": "15:53",
  268. "timestamp": "2024-01-15 14:30",
  269. "app_name": "Bambuddy",
  270. },
  271. "queue_job_waiting": {
  272. "job_name": "Benchy.3mf",
  273. "target_model": "X1C",
  274. "waiting_reason": "Printer1 (needs PLA)",
  275. "timestamp": "2024-01-15 14:30",
  276. "app_name": "Bambuddy",
  277. },
  278. "queue_job_skipped": {
  279. "printer": "Bambu X1C",
  280. "job_name": "Benchy.3mf",
  281. "reason": "Previous print failed",
  282. "timestamp": "2024-01-15 14:30",
  283. "app_name": "Bambuddy",
  284. },
  285. "queue_job_failed": {
  286. "printer": "Bambu X1C",
  287. "job_name": "Benchy.3mf",
  288. "reason": "Upload failed: connection timeout",
  289. "timestamp": "2024-01-15 14:30",
  290. "app_name": "Bambuddy",
  291. },
  292. "queue_completed": {
  293. "completed_count": "5",
  294. "timestamp": "2024-01-15 18:30",
  295. "app_name": "Bambuddy",
  296. },
  297. # User management notifications
  298. "user_created": {
  299. "username": "john_doe",
  300. "password": "<generated-password>",
  301. "login_url": "https://bambuddy.example.com/login",
  302. "app_name": "Bambuddy",
  303. "timestamp": "2024-01-15 14:30",
  304. },
  305. "password_reset": {
  306. "username": "john_doe",
  307. "password": "<new-password>",
  308. "login_url": "https://bambuddy.example.com/login",
  309. "app_name": "Bambuddy",
  310. "timestamp": "2024-01-15 14:30",
  311. },
  312. # User email print notifications
  313. "user_print_start": {
  314. "username": "john_doe",
  315. "printer": "Bambu X1C",
  316. "filename": "Benchy.3mf",
  317. "timestamp": "2024-01-15 14:30",
  318. "app_name": "Bambuddy",
  319. },
  320. "user_print_complete": {
  321. "username": "john_doe",
  322. "printer": "Bambu X1C",
  323. "filename": "Benchy.3mf",
  324. "timestamp": "2024-01-15 15:48",
  325. "app_name": "Bambuddy",
  326. },
  327. "user_print_failed": {
  328. "username": "john_doe",
  329. "printer": "Bambu X1C",
  330. "filename": "Benchy.3mf",
  331. "timestamp": "2024-01-15 15:15",
  332. "app_name": "Bambuddy",
  333. },
  334. "user_print_stopped": {
  335. "username": "john_doe",
  336. "printer": "Bambu X1C",
  337. "filename": "Benchy.3mf",
  338. "timestamp": "2024-01-15 15:15",
  339. "app_name": "Bambuddy",
  340. },
  341. }
  342. class NotificationTemplateBase(BaseModel):
  343. """Base schema for notification templates."""
  344. title_template: str = Field(..., min_length=1, max_length=200)
  345. body_template: str = Field(..., min_length=1, max_length=2000)
  346. class NotificationTemplateUpdate(BaseModel):
  347. """Schema for updating a notification template."""
  348. title_template: str | None = Field(default=None, min_length=1, max_length=200)
  349. body_template: str | None = Field(default=None, min_length=1, max_length=2000)
  350. class NotificationTemplateResponse(NotificationTemplateBase):
  351. """Schema for notification template API responses."""
  352. id: int
  353. event_type: str
  354. name: str
  355. is_default: bool
  356. created_at: datetime
  357. updated_at: datetime
  358. class Config:
  359. from_attributes = True
  360. class TemplateVariableInfo(BaseModel):
  361. """Information about a template variable."""
  362. name: str
  363. description: str
  364. class EventVariablesResponse(BaseModel):
  365. """Response for available variables per event type."""
  366. event_type: str
  367. event_name: str
  368. variables: list[str]
  369. class TemplatePreviewRequest(BaseModel):
  370. """Request to preview a template with sample data."""
  371. event_type: str
  372. title_template: str
  373. body_template: str
  374. class TemplatePreviewResponse(BaseModel):
  375. """Response with rendered template preview."""
  376. title: str
  377. body: str