notification_template.py 9.6 KB

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