notification_template.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """Notification template model for customizable notification messages."""
  2. from datetime import datetime
  3. from sqlalchemy import Boolean, DateTime, Integer, String, Text, func
  4. from sqlalchemy.orm import Mapped, mapped_column
  5. from backend.app.core.database import Base
  6. class NotificationTemplate(Base):
  7. """Model for notification message templates."""
  8. __tablename__ = "notification_templates"
  9. id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
  10. event_type: Mapped[str] = mapped_column(String(50), nullable=False, unique=True)
  11. name: Mapped[str] = mapped_column(String(100), nullable=False)
  12. title_template: Mapped[str] = mapped_column(Text, nullable=False)
  13. body_template: Mapped[str] = mapped_column(Text, nullable=False)
  14. is_default: Mapped[bool] = mapped_column(Boolean, default=True)
  15. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  16. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  17. # Default templates for seeding
  18. DEFAULT_TEMPLATES = [
  19. {
  20. "event_type": "print_start",
  21. "name": "Print Started",
  22. "title_template": "Print Started",
  23. "body_template": "{printer}: {filename}\nEstimated: {estimated_time}",
  24. },
  25. {
  26. "event_type": "print_complete",
  27. "name": "Print Completed",
  28. "title_template": "Print Completed",
  29. "body_template": "{printer}: {filename}\nTime: {duration}\nFilament: {filament_grams}g",
  30. },
  31. {
  32. "event_type": "print_failed",
  33. "name": "Print Failed",
  34. "title_template": "Print Failed",
  35. "body_template": "{printer}: {filename}\nTime: {duration}\nReason: {reason}",
  36. },
  37. {
  38. "event_type": "print_stopped",
  39. "name": "Print Stopped",
  40. "title_template": "Print Stopped",
  41. "body_template": "{printer}: {filename}\nTime: {duration}",
  42. },
  43. {
  44. "event_type": "print_progress",
  45. "name": "Print Progress",
  46. "title_template": "Print {progress}% Complete",
  47. "body_template": "{printer}: {filename}\nRemaining: {remaining_time}",
  48. },
  49. {
  50. "event_type": "printer_offline",
  51. "name": "Printer Offline",
  52. "title_template": "Printer Offline",
  53. "body_template": "{printer} has disconnected",
  54. },
  55. {
  56. "event_type": "printer_error",
  57. "name": "Printer Error",
  58. "title_template": "Printer Error: {error_type}",
  59. "body_template": "{printer}\n{error_detail}",
  60. },
  61. {
  62. "event_type": "plate_not_empty",
  63. "name": "Plate Not Empty",
  64. "title_template": "Plate Not Empty - Print Paused",
  65. "body_template": "{printer}: Objects detected on build plate. Print has been paused. Clear plate and resume.",
  66. },
  67. {
  68. "event_type": "filament_low",
  69. "name": "Filament Low",
  70. "title_template": "Filament Low",
  71. "body_template": "{printer}: Slot {slot} at {remaining_percent}%",
  72. },
  73. {
  74. "event_type": "maintenance_due",
  75. "name": "Maintenance Due",
  76. "title_template": "Maintenance Due",
  77. "body_template": "{printer}:\n{items}",
  78. },
  79. {
  80. "event_type": "ams_humidity_high",
  81. "name": "AMS Humidity High",
  82. "title_template": "AMS Humidity Alert",
  83. "body_template": "{printer} {ams_label}: Humidity {humidity}% exceeds {threshold}% threshold",
  84. },
  85. {
  86. "event_type": "ams_temperature_high",
  87. "name": "AMS Temperature High",
  88. "title_template": "AMS Temperature Alert",
  89. "body_template": "{printer} {ams_label}: Temperature {temperature}°C exceeds {threshold}°C threshold",
  90. },
  91. {
  92. "event_type": "bed_cooled",
  93. "name": "Bed Cooled",
  94. "title_template": "Bed Cooled",
  95. "body_template": "{printer}: Bed cooled to {bed_temp}°C (threshold: {threshold}°C)",
  96. },
  97. {
  98. "event_type": "test",
  99. "name": "Test Notification",
  100. "title_template": "Bambuddy Test",
  101. "body_template": "This is a test notification. If you see this, notifications are working!",
  102. },
  103. # Queue notifications
  104. {
  105. "event_type": "queue_job_added",
  106. "name": "Queue Job Added",
  107. "title_template": "Job Queued",
  108. "body_template": "{job_name} added to queue for {target}",
  109. },
  110. {
  111. "event_type": "queue_job_assigned",
  112. "name": "Queue Job Assigned",
  113. "title_template": "Job Assigned",
  114. "body_template": "{job_name} assigned to {printer} (from Any {target_model} queue)",
  115. },
  116. {
  117. "event_type": "queue_job_started",
  118. "name": "Queue Job Started",
  119. "title_template": "Queue Job Started",
  120. "body_template": "{printer}: {job_name}\nEstimated: {estimated_time}",
  121. },
  122. {
  123. "event_type": "queue_job_waiting",
  124. "name": "Queue Job Waiting",
  125. "title_template": "Job Waiting for Filament",
  126. "body_template": "{job_name} waiting for {target_model}\n{waiting_reason}",
  127. },
  128. {
  129. "event_type": "queue_job_skipped",
  130. "name": "Queue Job Skipped",
  131. "title_template": "Job Skipped",
  132. "body_template": "{printer}: {job_name}\nReason: {reason}",
  133. },
  134. {
  135. "event_type": "queue_job_failed",
  136. "name": "Queue Job Failed",
  137. "title_template": "Job Failed to Start",
  138. "body_template": "{printer}: {job_name}\nReason: {reason}",
  139. },
  140. {
  141. "event_type": "queue_completed",
  142. "name": "Queue Completed",
  143. "title_template": "Queue Complete",
  144. "body_template": "All {completed_count} queued jobs have finished",
  145. },
  146. {
  147. "event_type": "user_created",
  148. "name": "Welcome Email",
  149. "title_template": "Welcome to {app_name}",
  150. "body_template": "Welcome {username}!\n\nYour account has been created.\nUsername: {username}\nPassword: {password}\n\nLogin at: {login_url}",
  151. },
  152. {
  153. "event_type": "password_reset",
  154. "name": "Password Reset",
  155. "title_template": "{app_name} - Password Reset",
  156. "body_template": "Hello {username},\n\nYour password has been reset.\nNew Password: {password}\n\nLogin at: {login_url}",
  157. },
  158. ]