notification_template.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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": "billing_charge_failed",
  57. "name": "Billing Charge Failed",
  58. "title_template": "Billing Charge Failed",
  59. "body_template": "{printer}: {filename}\nThe print charge could not be recorded. The budget reservation was retained.\nArchive: {archive_id}",
  60. },
  61. {
  62. "event_type": "printer_offline",
  63. "name": "Printer Offline",
  64. "title_template": "Printer Offline",
  65. "body_template": "{printer} has disconnected",
  66. },
  67. {
  68. "event_type": "printer_error",
  69. "name": "Printer Error",
  70. "title_template": "Printer Error: {error_type}",
  71. "body_template": "{printer}\n{error_detail}",
  72. },
  73. {
  74. "event_type": "ai_failure_detection",
  75. "name": "AI Failure Detection",
  76. "title_template": "Possible Print Failure Detected",
  77. "body_template": "{printer}: {task_name}\nConfidence: {confidence}\nAction taken: {action}",
  78. },
  79. {
  80. "event_type": "plate_not_empty",
  81. "name": "Plate Not Empty",
  82. "title_template": "Plate Not Empty - Print Paused",
  83. "body_template": "{printer}: Objects detected on build plate. Print has been paused. Clear plate and resume.",
  84. },
  85. {
  86. "event_type": "plate_clear_required",
  87. "name": "Plate Clear Required",
  88. "title_template": "Plate Clear Required",
  89. "body_template": "{printer}: print finished. Confirm the build plate is clear before the queue continues.",
  90. },
  91. {
  92. "event_type": "filament_low",
  93. "name": "Filament Low",
  94. "title_template": "Filament Low",
  95. "body_template": "{printer}: Slot {slot} at {remaining_percent}%",
  96. },
  97. {
  98. "event_type": "maintenance_due",
  99. "name": "Maintenance Due",
  100. "title_template": "Maintenance Due",
  101. "body_template": "{printer}:\n{items}",
  102. },
  103. {
  104. "event_type": "ams_humidity_high",
  105. "name": "AMS Humidity High",
  106. "title_template": "AMS Humidity Alert",
  107. "body_template": "{printer} {ams_label}: Humidity {humidity}% exceeds {threshold}% threshold",
  108. },
  109. {
  110. "event_type": "ams_temperature_high",
  111. "name": "AMS Temperature High",
  112. "title_template": "AMS Temperature Alert",
  113. "body_template": "{printer} {ams_label}: Temperature {temperature}°C exceeds {threshold}°C threshold",
  114. },
  115. {
  116. "event_type": "ams_drying_suspended",
  117. "name": "Auto-Drying Suspended",
  118. "title_template": "Auto-Drying Suspended",
  119. "body_template": (
  120. "{printer} {ams_label}: stopped automatic drying after {cycles} cycles left humidity at "
  121. "{humidity}%, still above the {threshold}% threshold. An AMS reads higher while it is warm, "
  122. "so raise the threshold or dry the spools off the printer."
  123. ),
  124. },
  125. {
  126. "event_type": "bed_cooled",
  127. "name": "Bed Cooled",
  128. "title_template": "Bed Cooled",
  129. "body_template": "{printer}: Bed cooled to {bed_temp}°C (threshold: {threshold}°C)",
  130. },
  131. {
  132. "event_type": "ha_sensor_alert",
  133. "name": "Printer Sensor Alert",
  134. "title_template": "Sensor Alert",
  135. "body_template": "{printer}: {sensor} is {state}",
  136. },
  137. {
  138. "event_type": "location_ha_sensor_alert",
  139. "name": "Storage Location Sensor Alert",
  140. "title_template": "Sensor Alert",
  141. "body_template": "{location}: {sensor} is {state}",
  142. },
  143. {
  144. "event_type": "first_layer_complete",
  145. "name": "First Layer Complete",
  146. "title_template": "First Layer Complete",
  147. "body_template": "{printer}: {filename}\nLayer 1/{total_layers} done",
  148. },
  149. {
  150. "event_type": "test",
  151. "name": "Test Notification",
  152. "title_template": "Bambuddy Test",
  153. "body_template": "This is a test notification. If you see this, notifications are working!",
  154. },
  155. # Queue notifications
  156. {
  157. "event_type": "queue_job_added",
  158. "name": "Queue Job Added",
  159. "title_template": "Job Queued",
  160. "body_template": "{job_name} added to queue for {target}",
  161. },
  162. {
  163. "event_type": "queue_job_assigned",
  164. "name": "Queue Job Assigned",
  165. "title_template": "Job Assigned",
  166. "body_template": "{job_name} assigned to {printer} (from Any {target_model} queue)",
  167. },
  168. {
  169. "event_type": "queue_job_started",
  170. "name": "Queue Job Started",
  171. "title_template": "Queue Job Started",
  172. "body_template": "{printer}: {job_name}\nEstimated: {estimated_time}",
  173. },
  174. {
  175. "event_type": "queue_job_waiting",
  176. "name": "Queue Job Waiting",
  177. "title_template": "Queue Job Waiting",
  178. "body_template": "{job_name} waiting for {target_model}\n{waiting_reason}",
  179. },
  180. {
  181. "event_type": "queue_job_skipped",
  182. "name": "Queue Job Skipped",
  183. "title_template": "Job Skipped",
  184. "body_template": "{printer}: {job_name}\nReason: {reason}",
  185. },
  186. {
  187. "event_type": "queue_job_failed",
  188. "name": "Queue Job Failed",
  189. "title_template": "Job Failed to Start",
  190. "body_template": "{printer}: {job_name}\nReason: {reason}",
  191. },
  192. {
  193. "event_type": "queue_completed",
  194. "name": "Queue Completed",
  195. "title_template": "Queue Complete",
  196. "body_template": "All {completed_count} queued jobs have finished",
  197. },
  198. {
  199. "event_type": "user_created",
  200. "name": "Welcome Email",
  201. "title_template": "Welcome to {app_name}",
  202. "body_template": "Welcome {username}!\n\nYour account has been created.\nUsername: {username}\nPassword: {password}\n\nLogin at: {login_url}",
  203. },
  204. {
  205. "event_type": "password_reset",
  206. "name": "Password Reset",
  207. "title_template": "{app_name} - Password Reset",
  208. "body_template": "Hello {username},\n\nYour password has been reset.\nNew Password: {password}\n\nLogin at: {login_url}",
  209. },
  210. # Inventory stock alert templates
  211. {
  212. "event_type": "stock_reorder_alert",
  213. "name": "Stock Reorder Alert",
  214. "title_template": "Reorder Alert: {material}",
  215. "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.",
  216. },
  217. {
  218. "event_type": "stock_break_alert",
  219. "name": "Stock Break Alert",
  220. "title_template": "Stock Break Risk: {material}",
  221. "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.",
  222. },
  223. # User email notification templates (sent to the print job owner).
  224. # Names include " Email" so they aren't confused with the provider-level
  225. # `print_*` templates above, which share the same body shape but are
  226. # broadcast to admin-configured providers (ntfy/pushover/telegram/discord/
  227. # etc.) rather than mailed to a specific user.
  228. {
  229. "event_type": "user_print_start",
  230. "name": "User Print Started Email",
  231. "title_template": "Your Print Has Started",
  232. "body_template": "Hello {username},\n\nYour print job has started on {printer}.\n\nFile: {filename}\n\nYou will be notified when it completes.",
  233. },
  234. {
  235. "event_type": "user_print_complete",
  236. "name": "User Print Completed Email",
  237. "title_template": "Your Print Is Complete",
  238. "body_template": "Hello {username},\n\nYour print job has completed on {printer}.\n\nFile: {filename}",
  239. },
  240. {
  241. "event_type": "user_print_failed",
  242. "name": "User Print Failed Email",
  243. "title_template": "Your Print Has Failed",
  244. "body_template": "Hello {username},\n\nYour print job has failed on {printer}.\n\nFile: {filename}",
  245. },
  246. {
  247. "event_type": "user_print_stopped",
  248. "name": "User Print Stopped Email",
  249. "title_template": "Your Print Has Been Stopped",
  250. "body_template": "Hello {username},\n\nYour print job was stopped on {printer}.\n\nFile: {filename}",
  251. },
  252. ]