notification_template.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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": "print_missing_spool_assignment",
  51. "name": "Missing Spool Assignment",
  52. "title_template": "Missing Spool Assignment",
  53. "body_template": "{printer}: print started with missing spool assignments\nSlots: {missing_slots}\nExpected profile:\n{missing_slot_details}",
  54. },
  55. {
  56. "event_type": "printer_offline",
  57. "name": "Printer Offline",
  58. "title_template": "Printer Offline",
  59. "body_template": "{printer} has disconnected",
  60. },
  61. {
  62. "event_type": "printer_error",
  63. "name": "Printer Error",
  64. "title_template": "Printer Error: {error_type}",
  65. "body_template": "{printer}\n{error_detail}",
  66. },
  67. {
  68. "event_type": "ai_failure_detection",
  69. "name": "AI Failure Detection",
  70. "title_template": "Possible Print Failure Detected",
  71. "body_template": "{printer}: {task_name}\nConfidence: {confidence}\nAction taken: {action}",
  72. },
  73. {
  74. "event_type": "plate_not_empty",
  75. "name": "Plate Not Empty",
  76. "title_template": "Plate Not Empty - Print Paused",
  77. "body_template": "{printer}: Objects detected on build plate. Print has been paused. Clear plate and resume.",
  78. },
  79. {
  80. "event_type": "plate_clear_required",
  81. "name": "Plate Clear Required",
  82. "title_template": "Plate Clear Required",
  83. "body_template": "{printer}: print finished. Confirm the build plate is clear before the queue continues.",
  84. },
  85. {
  86. "event_type": "filament_low",
  87. "name": "Filament Low",
  88. "title_template": "Filament Low",
  89. "body_template": "{printer}: Slot {slot} at {remaining_percent}%",
  90. },
  91. {
  92. "event_type": "maintenance_due",
  93. "name": "Maintenance Due",
  94. "title_template": "Maintenance Due",
  95. "body_template": "{printer}:\n{items}",
  96. },
  97. {
  98. "event_type": "ams_humidity_high",
  99. "name": "AMS Humidity High",
  100. "title_template": "AMS Humidity Alert",
  101. "body_template": "{printer} {ams_label}: Humidity {humidity}% exceeds {threshold}% threshold",
  102. },
  103. {
  104. "event_type": "ams_temperature_high",
  105. "name": "AMS Temperature High",
  106. "title_template": "AMS Temperature Alert",
  107. "body_template": "{printer} {ams_label}: Temperature {temperature}°C exceeds {threshold}°C threshold",
  108. },
  109. {
  110. "event_type": "bed_cooled",
  111. "name": "Bed Cooled",
  112. "title_template": "Bed Cooled",
  113. "body_template": "{printer}: Bed cooled to {bed_temp}°C (threshold: {threshold}°C)",
  114. },
  115. {
  116. "event_type": "ha_sensor_alert",
  117. "name": "Home Assistant Sensor Alert",
  118. "title_template": "Sensor Alert",
  119. "body_template": "{printer}: {sensor} is {state}",
  120. },
  121. {
  122. "event_type": "first_layer_complete",
  123. "name": "First Layer Complete",
  124. "title_template": "First Layer Complete",
  125. "body_template": "{printer}: {filename}\nLayer 1/{total_layers} done",
  126. },
  127. {
  128. "event_type": "test",
  129. "name": "Test Notification",
  130. "title_template": "Bambuddy Test",
  131. "body_template": "This is a test notification. If you see this, notifications are working!",
  132. },
  133. # Queue notifications
  134. {
  135. "event_type": "queue_job_added",
  136. "name": "Queue Job Added",
  137. "title_template": "Job Queued",
  138. "body_template": "{job_name} added to queue for {target}",
  139. },
  140. {
  141. "event_type": "queue_job_assigned",
  142. "name": "Queue Job Assigned",
  143. "title_template": "Job Assigned",
  144. "body_template": "{job_name} assigned to {printer} (from Any {target_model} queue)",
  145. },
  146. {
  147. "event_type": "queue_job_started",
  148. "name": "Queue Job Started",
  149. "title_template": "Queue Job Started",
  150. "body_template": "{printer}: {job_name}\nEstimated: {estimated_time}",
  151. },
  152. {
  153. "event_type": "queue_job_waiting",
  154. "name": "Queue Job Waiting",
  155. "title_template": "Queue Job Waiting",
  156. "body_template": "{job_name} waiting for {target_model}\n{waiting_reason}",
  157. },
  158. {
  159. "event_type": "queue_job_skipped",
  160. "name": "Queue Job Skipped",
  161. "title_template": "Job Skipped",
  162. "body_template": "{printer}: {job_name}\nReason: {reason}",
  163. },
  164. {
  165. "event_type": "queue_job_failed",
  166. "name": "Queue Job Failed",
  167. "title_template": "Job Failed to Start",
  168. "body_template": "{printer}: {job_name}\nReason: {reason}",
  169. },
  170. {
  171. "event_type": "queue_completed",
  172. "name": "Queue Completed",
  173. "title_template": "Queue Complete",
  174. "body_template": "All {completed_count} queued jobs have finished",
  175. },
  176. {
  177. "event_type": "user_created",
  178. "name": "Welcome Email",
  179. "title_template": "Welcome to {app_name}",
  180. "body_template": "Welcome {username}!\n\nYour account has been created.\nUsername: {username}\nPassword: {password}\n\nLogin at: {login_url}",
  181. },
  182. {
  183. "event_type": "password_reset",
  184. "name": "Password Reset",
  185. "title_template": "{app_name} - Password Reset",
  186. "body_template": "Hello {username},\n\nYour password has been reset.\nNew Password: {password}\n\nLogin at: {login_url}",
  187. },
  188. # Inventory stock alert templates
  189. {
  190. "event_type": "stock_reorder_alert",
  191. "name": "Stock Reorder Alert",
  192. "title_template": "Reorder Alert: {material}",
  193. "body_template": "{material} ({brand}) has reached the reorder point.\nStock: {stock_g}g | Rate: {rate_g_day}g/day | Days left: {days_left}d\nReorder now to avoid a stock break.",
  194. },
  195. {
  196. "event_type": "stock_break_alert",
  197. "name": "Stock Break Alert",
  198. "title_template": "Stock Break Risk: {material}",
  199. "body_template": "{material} ({brand}) will run out before replenishment arrives.\nStock: {stock_g}g | Rate: {rate_g_day}g/day | Lead time: {lead_time_days}d\nOnly {days_left}d of stock remaining — order immediately.",
  200. },
  201. # User email notification templates (sent to the print job owner).
  202. # Names include " Email" so they aren't confused with the provider-level
  203. # `print_*` templates above, which share the same body shape but are
  204. # broadcast to admin-configured providers (ntfy/pushover/telegram/discord/
  205. # etc.) rather than mailed to a specific user.
  206. {
  207. "event_type": "user_print_start",
  208. "name": "User Print Started Email",
  209. "title_template": "Your Print Has Started",
  210. "body_template": "Hello {username},\n\nYour print job has started on {printer}.\n\nFile: {filename}\n\nYou will be notified when it completes.",
  211. },
  212. {
  213. "event_type": "user_print_complete",
  214. "name": "User Print Completed Email",
  215. "title_template": "Your Print Is Complete",
  216. "body_template": "Hello {username},\n\nYour print job has completed on {printer}.\n\nFile: {filename}",
  217. },
  218. {
  219. "event_type": "user_print_failed",
  220. "name": "User Print Failed Email",
  221. "title_template": "Your Print Has Failed",
  222. "body_template": "Hello {username},\n\nYour print job has failed on {printer}.\n\nFile: {filename}",
  223. },
  224. {
  225. "event_type": "user_print_stopped",
  226. "name": "User Print Stopped Email",
  227. "title_template": "Your Print Has Been Stopped",
  228. "body_template": "Hello {username},\n\nYour print job was stopped on {printer}.\n\nFile: {filename}",
  229. },
  230. ]