schedules.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Models for scheduled execution of jobs"""
  18. import enum
  19. from flask_appbuilder import Model
  20. from sqlalchemy import Boolean, Column, Enum, ForeignKey, Integer, String, Text
  21. from sqlalchemy.ext.declarative import declared_attr
  22. from sqlalchemy.orm import relationship
  23. from superset import security_manager
  24. from superset.models.helpers import AuditMixinNullable, ImportMixin
  25. metadata = Model.metadata # pylint: disable=no-member
  26. class ScheduleType(enum.Enum):
  27. slice = "slice"
  28. dashboard = "dashboard"
  29. class EmailDeliveryType(enum.Enum):
  30. attachment = "Attachment"
  31. inline = "Inline"
  32. class SliceEmailReportFormat(enum.Enum):
  33. visualization = "Visualization"
  34. data = "Raw data"
  35. class EmailSchedule:
  36. """Schedules for emailing slices / dashboards"""
  37. __tablename__ = "email_schedules"
  38. id = Column(Integer, primary_key=True) # pylint: disable=invalid-name
  39. active = Column(Boolean, default=True, index=True)
  40. crontab = Column(String(50))
  41. @declared_attr
  42. def user_id(self):
  43. return Column(Integer, ForeignKey("ab_user.id"))
  44. @declared_attr
  45. def user(self):
  46. return relationship(
  47. security_manager.user_model,
  48. backref=self.__tablename__,
  49. foreign_keys=[self.user_id],
  50. )
  51. recipients = Column(Text)
  52. deliver_as_group = Column(Boolean, default=False)
  53. delivery_type = Column(Enum(EmailDeliveryType))
  54. class DashboardEmailSchedule(Model, AuditMixinNullable, ImportMixin, EmailSchedule):
  55. __tablename__ = "dashboard_email_schedules"
  56. dashboard_id = Column(Integer, ForeignKey("dashboards.id"))
  57. dashboard = relationship(
  58. "Dashboard", backref="email_schedules", foreign_keys=[dashboard_id]
  59. )
  60. class SliceEmailSchedule(Model, AuditMixinNullable, ImportMixin, EmailSchedule):
  61. __tablename__ = "slice_email_schedules"
  62. slice_id = Column(Integer, ForeignKey("slices.id"))
  63. slice = relationship("Slice", backref="email_schedules", foreign_keys=[slice_id])
  64. email_format = Column(Enum(SliceEmailReportFormat))
  65. def get_scheduler_model(report_type):
  66. if report_type == ScheduleType.dashboard.value:
  67. return DashboardEmailSchedule
  68. elif report_type == ScheduleType.slice.value:
  69. return SliceEmailSchedule
  70. return None