def get(self, alert_id): alert_id = int(alert_id) alert = Alert.get_by_id_and_org(alert_id, self.current_org) require_access(alert.groups, self.current_user, view_only) subscriptions = AlertSubscription.all(alert_id) return [s.to_dict() for s in subscriptions]
def post(self, alert_id): req = request.get_json(True) alert = Alert.get_by_id_and_org(alert_id, self.current_org) require_access(alert.groups, self.current_user, view_only) kwargs = {'alert': alert, 'user': self.current_user} if 'destination_id' in req: destination = NotificationDestination.get_by_id_and_org( req['destination_id'], self.current_org) kwargs['destination'] = destination # 用提供外键对应的列 # kwargs = {'alert': alert, 'user': self.current_user, 'destination': destination} # AlertSubscription. 一个alert有对应多个订阅者,一订阅者订阅多个alert,多对多 # alert # user # query # notification subscription = AlertSubscription(**kwargs) session.add(subscription) session.commit() self.record_event({ 'action': 'subscribe', 'timestamp': int(time.time()), 'object_id': alert_id, 'object_type': 'alert', 'destination': req.get('destination_id') }) d = subscription.to_dict() return d