Example #1
0
def slack_connect_callback(request):
    code = request.GET.get('code', None)
    redirect_uri = '%s://%s%s' % (request.scheme, request.get_host(),
                                  reverse(request.resolver_match.url_name))
    r = requests.post(url=slack_utils.get_token_url(),
                      data={
                          'code': code,
                          'client_id': SLACK_CLIENT_ID,
                          'client_secret': SLACK_CLIENT_SECRET,
                          'redirect_uri': redirect_uri
                      })

    if r.status_code == 200:
        response = r.json()
        defaults = {
            'token': response['access_token'],
            'extra': json.dumps(response)
        }
        AppIntegration.objects.update_or_create(
            user=request.user,
            provider=APP_INTEGRATION_PROVIDER_SLACK,
            defaults=defaults)

        task_id = get_session_task(request)
        if task_id:
            token_info = {
                'token': response['access_token'],
                'token_extra': json.dumps(response)
            }
            if 'bot' in response:
                token_info['bot_access_token'] = response['bot'].get(
                    'bot_access_token')
                token_info['bot_user_id'] = response['bot'].get('bot_user_id')
            save_task_integration_meta(task_id, APP_INTEGRATION_PROVIDER_SLACK,
                                       token_info)
    return redirect(
        get_session_next_url(request, provider=APP_INTEGRATION_PROVIDER_SLACK))
Example #2
0
def slack_connect_callback(request):
    code = request.GET.get('code', None)
    redirect_uri = '%s://%s%s' % (request.scheme, request.get_host(),
                                  reverse(request.resolver_match.url_name))
    r = requests.post(url=slack_utils.get_token_url(),
                      data={
                          'code': code,
                          'client_id': SLACK_CLIENT_ID,
                          'client_secret': SLACK_CLIENT_SECRET,
                          'redirect_uri': redirect_uri
                      })

    if r.status_code == 200:
        response = r.json()
        defaults = {
            'token': response['access_token'],
            'extra': json.dumps(response)
        }
        AppIntegration.objects.update_or_create(
            user=request.user,
            provider=APP_INTEGRATION_PROVIDER_SLACK,
            defaults=defaults)

        project_id = get_session_project(request)
        task_id = get_session_task(request)
        if project_id or task_id:
            token_info = {
                'token': response['access_token'],
                'token_extra': json.dumps(response),
                'team_name': response['team_name'],
                'team_id': response['team_id'],
            }

            if 'bot' in response:
                token_info['bot_access_token'] = response['bot'].get(
                    'bot_access_token')
                token_info['bot_user_id'] = response['bot'].get('bot_user_id')

            if project_id:
                meta_info = dict()

                slack_client = Slacker(response['access_token'])
                channel_response = slack_client.channels.list(
                    exclude_archived=True)
                if channel_response.successful:
                    channels = channel_response.body.get(
                        slack_utils.KEY_CHANNELS, None)

                    if channels:
                        simple_channels = []

                        for channel in channels:
                            simple_channels.append(
                                dict(id=channel.get('id', None),
                                     name=channel.get('name', None)))

                        meta_info['slack_channels'] = json.dumps(
                            simple_channels)

                groups_response = slack_client.groups.list(
                    exclude_archived=True)
                if groups_response.successful:
                    groups = groups_response.body.get(slack_utils.KEY_GROUPS,
                                                      None)

                    if groups:
                        simple_groups = []

                        for group in groups:
                            simple_groups.append(
                                dict(id=group.get('id', None),
                                     name=group.get('name', None)))

                        meta_info['slack_groups'] = json.dumps(simple_groups)

                for meta_key in token_info:
                    meta_info['slack_{}'.format(
                        meta_key)] = token_info[meta_key]
                save_project_metadata(project_id, meta_info)
            if task_id:
                save_task_integration_meta(task_id,
                                           APP_INTEGRATION_PROVIDER_SLACK,
                                           token_info)

    return redirect(
        get_session_next_url(request, provider=APP_INTEGRATION_PROVIDER_SLACK))
Example #3
0
def slack_connect_callback(request):
    code = request.GET.get('code', None)
    redirect_uri = '%s://%s%s' % (request.scheme, request.get_host(), reverse(request.resolver_match.url_name))
    r = requests.post(url=slack_utils.get_token_url(), data={
        'code': code,
        'client_id': SLACK_CLIENT_ID,
        'client_secret': SLACK_CLIENT_SECRET,
        'redirect_uri': redirect_uri
    })

    if r.status_code == 200:
        response = r.json()
        defaults = {
            'token': response['access_token'],
            'extra': json.dumps(response)
        }
        AppIntegration.objects.update_or_create(
            user=request.user, provider=APP_INTEGRATION_PROVIDER_SLACK, defaults=defaults
        )

        project_id = get_session_project(request)
        task_id = get_session_task(request)
        if project_id or task_id:
            token_info = {
                'token': response['access_token'],
                'token_extra': json.dumps(response),
                'team_name': response['team_name'],
                'team_id': response['team_id'],
            }

            if 'bot' in response:
                token_info['bot_access_token'] = response['bot'].get('bot_access_token')
                token_info['bot_user_id'] = response['bot'].get('bot_user_id')

            if project_id:
                meta_info = dict()

                slack_client = Slacker(response['access_token'])
                channel_response = slack_client.channels.list(exclude_archived=True)
                if channel_response.successful:
                    channels = channel_response.body.get(slack_utils.KEY_CHANNELS, None)

                    if channels:
                        simple_channels = []

                        for channel in channels:
                            simple_channels.append(
                                dict(
                                    id=channel.get('id', None),
                                    name=channel.get('name', None)
                                )
                            )

                        meta_info['slack_channels'] = json.dumps(simple_channels)

                groups_response = slack_client.groups.list(exclude_archived=True)
                if groups_response.successful:
                    groups = groups_response.body.get(slack_utils.KEY_GROUPS, None)

                    if groups:
                        simple_groups = []

                        for group in groups:
                            simple_groups.append(
                                dict(
                                    id=group.get('id', None),
                                    name=group.get('name', None)
                                )
                            )

                        meta_info['slack_groups'] = json.dumps(simple_groups)

                for meta_key in token_info:
                    meta_info['slack_{}'.format(meta_key)] = token_info[meta_key]
                save_project_metadata(project_id, meta_info)
            if task_id:
                save_task_integration_meta(task_id, APP_INTEGRATION_PROVIDER_SLACK, token_info)

    return redirect(get_session_next_url(request, provider=APP_INTEGRATION_PROVIDER_SLACK))