def stop_notification(flask_report, id_): jobs = flask_report.sched.get_jobs() for job in jobs: if job.name == 'flask_report_notification' + str(id_): notification = Notification(flask_report, id_) notification.enabled = False notification.dump() flask_report.sched.unschedule_job(job) return 'ok' else: return 'unknown notifiaction:' + str(id_), 404
def start_notification(self, id_): notification = Notification(self, id_) def _closure(environ): def _push_notification(): with self.app.request_context(environ): self.push_notification(id_) return _push_notification job = self.sched.add_cron_job(_closure(request.environ), name='flask_report_notification' + str(id_), **notification.crontab._asdict()) notification.enabled = True notification.dump() return 'ok'
def notifications(self): if os.path.exists(self.notification_dir): return [ Notification(self, id_) for id_ in os.listdir(self.notification_dir) ] else: return []
def start_notification(flask_report, id_): notification = Notification(flask_report, id_) def _closure(environ): def _push_notification(): with flask_report.app.request_context(environ): flask_report.push_notification(id_) return _push_notification flask_report.sched.add_cron_job(_closure(request.environ), name='flask_report_notification' + str(id_), **notification.crontab._asdict()) notification.enabled = True notification.dump() return 'ok'
def notification_list(flask_report): notifications = [ Notification(flask_report, int(dir_name)) for dir_name in os.listdir(flask_report.notification_dir) if dir_name.isdigit() and dir_name != '0' ] params = dict(notification_list=notifications) extra_params = flask_report.notification_list_template_param(notifications) if extra_params: params.update(extra_params) return render_template("report____/notification-list.html", **params)
def notification(flask_report, id_=None): flask_report.try_edit_notification() if id_ is not None: notification = Notification(flask_report, id_) if request.method == "POST": if request.form.get('action') == _('Enable'): flask_report.start_notification(id_) elif request.form.get("action") == _("Disable"): flask_report.stop_notification(id_) else: _save_notification(flask_report, request.form, id_) flash(_("Update Successful!")) return redirect(url_for(".notification", id_=id_, _method="GET")) else: params = dict(notification=notification, report_list=flask_report.report_list) extra_params = flask_report.notification_template_param( notification) if extra_params: params.update(extra_params) return render_template("report____/notification.html", **params) else: if request.method == "POST": id_ = max([ int(dir_name) for dir_name in os.listdir(flask_report.notification_dir) if dir_name.isdigit() and dir_name != '0' ]) + 1 new_dir = os.path.join(flask_report.notification_dir, str(id_)) if not os.path.exists(new_dir): os.mkdir(new_dir) _save_notification(flask_report, request.form, id_) flash(_("Save Successful!")) return redirect(url_for(".notification", id_=id_)) else: # TODO why no id_? params = dict(report_list=flask_report.report_list) extra_params = flask_report.notification_template_param(None) if extra_params: params.update(extra_params) return render_template("report____/notification.html", **params)
def push_notification(flask_report, id_): to = request.args.get('to') notification = Notification(flask_report, id_) # don't use sender, use recipient instead if not to: senders = notification.senders else: senders = [to] for sender in senders: if sender not in senders: return _('notification %(id)s is not allowed to send to %(to)s', id=id_, to=sender), 403 html = notification.template.render(notification=notification) msg = Message( subject=notification.subject, html=html, # TODO where sender come from sender="*****@*****.**", recipients=senders) flask_report.mail.send(msg) return 'ok'