# Path: auth/utils.py
# def has_role(*roles):
#     def func():
#         if current_user.is_authenticated:
#             for role in roles:
#                 if current_user.has_role(role):
#                     return True
#         return False
#     return func


from auth.utils import has_role
from flask import Blueprint
from flask_menu import register_menu
from redis import StrictRedis, ConnectionError
from gevent import monkey

monkey.patch_all()

bp = Blueprint('push', __name__)
redis = StrictRedis(host='127.0.0.1', port=6379, db=13)

def event_stream():
    channels = [ 'notifications' ]

    pubsub = redis.pubsub()
    pubsub.subscribe(channels)

    while True:
        try:
            for message in pubsub.listen():
                if message['type'] == 'message':
                    yield 'data:%s\n\n' % message['data']
        except ConnectionError:
            while True:
                print('lost connection; trying to reconnect...')

                try:
                    redis.ping()
                except ConnectionError:
                    time.sleep(10)
                else:
                    pubsub.subscribe(channels)
                    break

@bp.route('/broadcast', methods=[ 'GET', 'POST' ])

# Path: auth/utils.py
# def has_role(*roles):
#     def func():
#         if current_user.is_authenticated:
#             for role in roles:
#                 if current_user.has_role(role):
#                     return True
#         return False
#     return func


# Path: auth/push.py
from auth.utils import has_role
from flask import Blueprint
from flask_menu import register_menu
from redis import StrictRedis, ConnectionError
from gevent import monkey


monkey.patch_all()

bp = Blueprint('push', __name__)
redis = StrictRedis(host='127.0.0.1', port=6379, db=13)

def event_stream():
    channels = [ 'notifications' ]

    pubsub = redis.pubsub()
    pubsub.subscribe(channels)

    while True:
        try:
            for message in pubsub.listen():
                if message['type'] == 'message':
                    yield 'data:%s\n\n' % message['data']
        except ConnectionError:
            while True:
                print('lost connection; trying to reconnect...')

                try:
                    redis.ping()
                except ConnectionError:
                    time.sleep(10)
                else:
                    pubsub.subscribe(channels)
                    break

@bp.route('/broadcast', methods=[ 'GET', 'POST' ])

@register_menu(bp, '.broadcast', 'Broadcast', visible_when=has_role('super-admin'), order=5)