notification_template.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. """Pydantic schemas for notification templates."""
  2. from datetime import datetime
  3. from enum import Enum
  4. from pydantic import BaseModel, Field
  5. class EventType(str, Enum):
  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. }
  42. # Sample data for previewing templates
  43. SAMPLE_DATA: dict[str, dict[str, str]] = {
  44. "print_start": {
  45. "printer": "Bambu X1C",
  46. "filename": "Benchy.3mf",
  47. "estimated_time": "1h 23m",
  48. "timestamp": "2024-01-15 14:30",
  49. "app_name": "Bambuddy",
  50. },
  51. "print_complete": {
  52. "printer": "Bambu X1C",
  53. "filename": "Benchy.3mf",
  54. "duration": "1h 18m",
  55. "filament_grams": "15.2",
  56. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_154800_abc12345.jpg",
  57. "timestamp": "2024-01-15 15:48",
  58. "app_name": "Bambuddy",
  59. },
  60. "print_failed": {
  61. "printer": "Bambu X1C",
  62. "filename": "Benchy.3mf",
  63. "duration": "0h 45m",
  64. "reason": "Filament runout",
  65. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_151500_def67890.jpg",
  66. "timestamp": "2024-01-15 15:15",
  67. "app_name": "Bambuddy",
  68. },
  69. "print_stopped": {
  70. "printer": "Bambu X1C",
  71. "filename": "Benchy.3mf",
  72. "duration": "0h 30m",
  73. "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_150000_ghi11223.jpg",
  74. "timestamp": "2024-01-15 15:00",
  75. "app_name": "Bambuddy",
  76. },
  77. "print_progress": {
  78. "printer": "Bambu X1C",
  79. "filename": "Benchy.3mf",
  80. "progress": "50",
  81. "remaining_time": "0h 41m",
  82. "timestamp": "2024-01-15 15:00",
  83. "app_name": "Bambuddy",
  84. },
  85. "printer_offline": {
  86. "printer": "Bambu X1C",
  87. "timestamp": "2024-01-15 14:30",
  88. "app_name": "Bambuddy",
  89. },
  90. "printer_error": {
  91. "printer": "Bambu X1C",
  92. "error_type": "AMS Error",
  93. "error_detail": "Filament slot 1 jammed",
  94. "timestamp": "2024-01-15 14:30",
  95. "app_name": "Bambuddy",
  96. },
  97. "filament_low": {
  98. "printer": "Bambu X1C",
  99. "slot": "1",
  100. "remaining_percent": "15",
  101. "color": "Black PLA",
  102. "timestamp": "2024-01-15 14:30",
  103. "app_name": "Bambuddy",
  104. },
  105. "maintenance_due": {
  106. "printer": "Bambu X1C",
  107. "items": "• Nozzle cleaning (OVERDUE)\n• Carbon rod lubrication (Soon)",
  108. "timestamp": "2024-01-15 14:30",
  109. "app_name": "Bambuddy",
  110. },
  111. "ams_humidity_high": {
  112. "printer": "Bambu X1C",
  113. "ams_label": "AMS-A",
  114. "humidity": "75",
  115. "threshold": "60",
  116. "timestamp": "2024-01-15 14:30",
  117. "app_name": "Bambuddy",
  118. },
  119. "ams_temperature_high": {
  120. "printer": "Bambu X1C",
  121. "ams_label": "AMS-A",
  122. "temperature": "42",
  123. "threshold": "35",
  124. "timestamp": "2024-01-15 14:30",
  125. "app_name": "Bambuddy",
  126. },
  127. "test": {
  128. "app_name": "Bambuddy",
  129. "timestamp": "2024-01-15 14:30",
  130. },
  131. }
  132. class NotificationTemplateBase(BaseModel):
  133. """Base schema for notification templates."""
  134. title_template: str = Field(..., min_length=1, max_length=200)
  135. body_template: str = Field(..., min_length=1, max_length=2000)
  136. class NotificationTemplateUpdate(BaseModel):
  137. """Schema for updating a notification template."""
  138. title_template: str | None = Field(default=None, min_length=1, max_length=200)
  139. body_template: str | None = Field(default=None, min_length=1, max_length=2000)
  140. class NotificationTemplateResponse(NotificationTemplateBase):
  141. """Schema for notification template API responses."""
  142. id: int
  143. event_type: str
  144. name: str
  145. is_default: bool
  146. created_at: datetime
  147. updated_at: datetime
  148. class Config:
  149. from_attributes = True
  150. class TemplateVariableInfo(BaseModel):
  151. """Information about a template variable."""
  152. name: str
  153. description: str
  154. class EventVariablesResponse(BaseModel):
  155. """Response for available variables per event type."""
  156. event_type: str
  157. event_name: str
  158. variables: list[str]
  159. class TemplatePreviewRequest(BaseModel):
  160. """Request to preview a template with sample data."""
  161. event_type: str
  162. title_template: str
  163. body_template: str
  164. class TemplatePreviewResponse(BaseModel):
  165. """Response with rendered template preview."""
  166. title: str
  167. body: str