notification_template.py 5.6 KB

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