notification_template.py 4.8 KB

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