notification_template.py 9.1 KB

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