Esempio n. 1
0
def utcnow_slack(event, **kwargs):
    """utcnow event handler for Slack webhook events
    """
    text = kwargs.get('text')
    command = kwargs.get('command')
    args = kwargs.get('args')

    if command == 'utcnow':
        from htk.utils import utcnow
        now = utcnow()
        webhook_settings = event['webhook_settings']
        user_id = webhook_settings['user']
        from htk.apps.accounts.utils import get_user_by_id
        user = get_user_by_id(user_id)
        slack_text = """*The time is now*:\n
*UTC*: %s
*%s*: %s""" % (
            now,
            user.profile.get_timezone(),
            user.profile.get_local_time(dt=now),
        )
    else:
        slack_text = 'Illegal command.'

    payload = {
        'text': slack_text,
    }
    return payload
Esempio n. 2
0
def zesty(event, **kwargs):
    """Zesty event handler for Slack webhook events
    """
    text = kwargs.get('text')
    command = kwargs.get('command')
    args = kwargs.get('args')

    zesty_slack_payload = {}
    if command == 'zesty':
        webhook_settings = event['webhook_settings']
        user_id = webhook_settings['user']
        from htk.apps.accounts.utils import get_user_by_id
        user = get_user_by_id(user_id)
        zesty_id = user.profile.get_attribute('zesty_id')
        if args:
            slack_text = '`zesty` does not take any arguments'
        elif zesty_id is None:
            slack_text = 'Error: Your account does not have a Zesty account id configured. Please check with your Slack admin.'
        else:
            from htk.lib.zesty.utils import get_zesty_lunch_menu
            dt = user.profile.get_local_time()
            slack_text = ''
            zesty_slack_payload = get_zesty_lunch_menu(zesty_id, dt)
    else:
        slack_text = 'Illegal command.'

    channel = event['channel_id']

    payload = {
        'text': slack_text,
        'unfurl_links': True,
        'unfurl_media': True,
    }
    payload.update(zesty_slack_payload)
    return payload
Esempio n. 3
0
def zesty(event, **kwargs):
    """Zesty event handler for Slack webhook events
    """
    text = kwargs.get('text')
    command = kwargs.get('command')
    args = kwargs.get('args')

    zesty_slack_payload = {}
    if command == 'zesty':
        webhook_settings = event['webhook_settings']
        user_id = webhook_settings['user']
        from htk.apps.accounts.utils import get_user_by_id
        user = get_user_by_id(user_id)
        zesty_id = user.profile.get_attribute('zesty_id')
        if args:
            slack_text = '`zesty` does not take any arguments'
        elif zesty_id is None:
            slack_text = 'Error: Your account does not have a Zesty account id configured. Please check with your Slack admin.'
        else:
            from htk.lib.zesty.utils import get_zesty_lunch_menu
            dt = user.profile.get_local_time()
            slack_text = ''
            zesty_slack_payload = get_zesty_lunch_menu(zesty_id, dt)
    else:
        slack_text = 'Illegal command.'

    channel = event['channel_id']

    payload = {
        'text' : slack_text,
        'unfurl_links' : True,
        'unfurl_media' : True,
    }
    payload.update(zesty_slack_payload)
    return payload
Esempio n. 4
0
def utcnow_slack(event, **kwargs):
    """utcnow event handler for Slack webhook events
    """
    text = kwargs.get('text')
    command = kwargs.get('command')
    args = kwargs.get('args')

    if command == 'utcnow':
        from htk.utils import utcnow
        now = utcnow()
        webhook_settings = event['webhook_settings']
        user_id = webhook_settings['user']
        from htk.apps.accounts.utils import get_user_by_id
        user = get_user_by_id(user_id)
        slack_text = """*The time is now*:\n
*UTC*: %s
*%s*: %s""" % (
    now,
    user.profile.get_timezone(),
    user.profile.get_local_time(dt=now),
)
    else:
        slack_text = 'Illegal command.'

    payload = {
        'text' : slack_text,
    }
    return payload
Esempio n. 5
0
    def process_request(self, request):
        """Replace the authenticated `request.user` if properly emulating
        """
        if is_allowed_to_emulate_users(request.user):
            from htk.apps.accounts.utils import get_user_by_id
            from htk.apps.accounts.utils import get_user_by_username

            user_id = request.COOKIES.get('emulate_user_id')
            username = request.COOKIES.get('emulate_user_username')

            is_attempting_to_emulate = user_id or username

            if is_attempting_to_emulate:
                if user_id:
                    targeted_user = get_user_by_id(user_id)
                elif username:
                    targeted_user = get_user_by_username(username)
                else:
                    rollbar.report_message('Impossible case: attempting to emulate another user but not specified')

                if targeted_user is None:
                    messages.error(request, 'Cannot Emulate: User does not exist', fail_silently=True)
                else:
                    if is_allowed_to_emulate(request.user, targeted_user):
                        request.original_user = request.user
                        request.user = targeted_user
                    else:
                        messages.error(request, 'Cannot Emulate: Not allowed to emulate that user', fail_silently=True)
            else:
                # not attempting to emulate
                pass
        else:
            # is not allowed or is not attempting to emulate users
            pass
Esempio n. 6
0
 def process_request(self, request):
     if self._can_emulate_another_user(request):
         from htk.apps.accounts.utils import get_user_by_id
         from htk.apps.accounts.utils import get_user_by_username
         user_id = request.COOKIES.get('emulate_user_id')
         username = request.COOKIES.get('emulate_user_username')
         if user_id or username:
             if user_id:
                 emulated_user = get_user_by_id(user_id)
             elif username:
                 emulated_user = get_user_by_username(username)
             if emulated_user:
                 request.original_user = request.user
                 request.user = emulated_user
Esempio n. 7
0
    def process_request(self, request):
        """Replace the authenticated `request.user` if properly emulating
        """
        if is_allowed_to_emulate_users(request.user):
            from htk.apps.accounts.utils import get_user_by_id
            from htk.apps.accounts.utils import get_user_by_username

            user_id = request.COOKIES.get('emulate_user_id')
            username = request.COOKIES.get('emulate_user_username')

            is_attempting_to_emulate = user_id or username

            if is_attempting_to_emulate:
                if user_id:
                    targeted_user = get_user_by_id(user_id)
                elif username:
                    targeted_user = get_user_by_username(username)
                else:
                    rollbar.report_message(
                        'Impossible case: attempting to emulate another user but not specified'
                    )

                if targeted_user is None:
                    messages.error(request,
                                   'Cannot Emulate: User does not exist',
                                   fail_silently=True)
                else:
                    if is_allowed_to_emulate(request.user, targeted_user):
                        request.original_user = request.user
                        request.user = targeted_user
                    else:
                        messages.error(
                            request,
                            'Cannot Emulate: Not allowed to emulate that user',
                            fail_silently=True)
            else:
                # not attempting to emulate
                pass
        else:
            # is not allowed or is not attempting to emulate users
            pass
Esempio n. 8
0
def ohmygreen(event, **kwargs):
    """OhMyGreen event handler for Slack webhook events
    """
    text = kwargs.get('text')
    command = kwargs.get('command')
    args = kwargs.get('args')

    ohmygreen_slack_payload = {}
    if command == 'ohmygreen':
        webhook_settings = event['webhook_settings']
        user_id = webhook_settings['user']
        from htk.apps.accounts.utils import get_user_by_id
        user = get_user_by_id(user_id)
        ohmygreen_id = user.profile.get_attribute('ohmygreen_id')
        ohmygreen_company = user.profile.get_attribute('ohmygreen_company')
        if args:
            slack_text = '`ohmygreen` does not take any arguments'
        elif ohmygreen_id is None or ohmygreen_company is None:
            slack_text = 'Error: Your account does not have an OhMyGreen account id configured. Please check with your Slack admin.'
        else:
            from htk.lib.ohmygreen.api import OhMyGreenAPI
            api = OhMyGreenAPI(ohmygreen_id, ohmygreen_company)
            dt = user.profile.get_local_time()
            menu = api.get_menu(dt)
            slack_text = menu.get_slack_message()
    else:
        slack_text = 'Illegal command.'

    channel = event['channel_id']

    payload = {
        'text': slack_text,
        'unfurl_links': True,
        'unfurl_media': True,
    }
    return payload
Esempio n. 9
0
def ohmygreen(event, **kwargs):
    """OhMyGreen event handler for Slack webhook events
    """
    text = kwargs.get('text')
    command = kwargs.get('command')
    args = kwargs.get('args')

    ohmygreen_slack_payload = {}
    if command == 'ohmygreen':
        webhook_settings = event['webhook_settings']
        user_id = webhook_settings['user']
        from htk.apps.accounts.utils import get_user_by_id
        user = get_user_by_id(user_id)
        ohmygreen_id = user.profile.get_attribute('ohmygreen_id')
        ohmygreen_company = user.profile.get_attribute('ohmygreen_company')
        if args:
            slack_text = '`ohmygreen` does not take any arguments'
        elif ohmygreen_id is None or ohmygreen_company is None:
            slack_text = 'Error: Your account does not have an OhMyGreen account id configured. Please check with your Slack admin.'
        else:
            from htk.lib.ohmygreen.api import OhMyGreenAPI
            api = OhMyGreenAPI(ohmygreen_id, ohmygreen_company)
            dt = user.profile.get_local_time()
            menu = api.get_menu(dt)
            slack_text = menu.get_slack_message()
    else:
        slack_text = 'Illegal command.'

    channel = event['channel_id']

    payload = {
        'text' : slack_text,
        'unfurl_links' : True,
        'unfurl_media' : True,
    }
    return payload