notification_template.py 12 KB

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