notification_template.py 13 KB

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