2024-07-06 08:14:01 +00:00
|
|
|
from flask import request, render_template, g, jsonify
|
2024-08-02 09:50:02 +00:00
|
|
|
from flask_jwt_extended import jwt_required
|
2024-03-17 06:38:00 +00:00
|
|
|
|
|
|
|
from app.routes.channel import bp
|
|
|
|
from app.middleware import get_user_params
|
|
|
|
import app.modules.tools.alerting as alerting
|
|
|
|
import app.modules.roxywi.common as roxywi_common
|
2024-08-02 09:50:02 +00:00
|
|
|
from app.views.channel.views import ChannelView
|
|
|
|
|
|
|
|
def register_api_channel(view, endpoint, url_beg, pk='receiver', pk_type='int', pk_end='channel_id', pk_type_end='int'):
|
|
|
|
view_func = view.as_view(endpoint, False)
|
|
|
|
bp.add_url_rule(f'/{url_beg}/<any(telegram, slack, pd, mm):{pk}>', view_func=view_func, methods=['POST'])
|
|
|
|
bp.add_url_rule(f'/{url_beg}/<any(telegram, slack, pd, mm):{pk}>/<{pk_type_end}:{pk_end}>', view_func=view_func, methods=['PUT', 'DELETE', 'GET', 'PATCH'])
|
|
|
|
|
|
|
|
|
|
|
|
register_api_channel(ChannelView, 'channel', '')
|
2024-03-17 06:38:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.before_request
|
2024-08-02 09:50:02 +00:00
|
|
|
@jwt_required()
|
2024-03-17 06:38:00 +00:00
|
|
|
def before_request():
|
|
|
|
""" Protect all the admin endpoints. """
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('')
|
|
|
|
@get_user_params()
|
|
|
|
def channels():
|
|
|
|
roxywi_common.check_user_group_for_flask()
|
2024-07-06 08:14:01 +00:00
|
|
|
lang = g.user_params['lang']
|
2024-03-17 06:38:00 +00:00
|
|
|
|
2024-07-06 08:14:01 +00:00
|
|
|
return render_template('channel.html', lang=lang)
|
2024-03-17 06:38:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/load')
|
|
|
|
@get_user_params()
|
|
|
|
def load_channels():
|
|
|
|
try:
|
|
|
|
return alerting.load_channels()
|
|
|
|
except Exception as e:
|
|
|
|
return f'{e}'
|
|
|
|
|
|
|
|
|
2024-06-19 17:52:24 +00:00
|
|
|
@bp.post('/check')
|
2024-08-19 08:49:19 +00:00
|
|
|
@get_user_params()
|
2024-06-19 17:52:24 +00:00
|
|
|
def check_sender():
|
|
|
|
json_data = request.get_json()
|
|
|
|
sender = json_data.get('sender')
|
|
|
|
send_function = {
|
|
|
|
'email': alerting.check_email_alert,
|
|
|
|
'web': alerting.check_rabbit_alert
|
|
|
|
}
|
2024-06-17 13:09:34 +00:00
|
|
|
try:
|
2024-06-19 17:52:24 +00:00
|
|
|
send_function[sender]()
|
2024-06-17 13:09:34 +00:00
|
|
|
return jsonify({'status': 'success'})
|
|
|
|
except Exception as e:
|
2024-06-19 17:52:24 +00:00
|
|
|
return roxywi_common.handle_json_exceptions(e, f'Cannot send message via {sender.title()}')
|