notification_template.py 8.6 KB

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