notification_template.py 11 KB

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