notification_template.py 9.5 KB

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