notification_template.py 11 KB

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