@require_setting('SLACK_CLIENT_ID')
@login_required
def add_slack_complete(request):
    if ('add_slack' not in request.session):
        return HttpResponseForbidden()
    (state, code) = request.session.pop('add_slack')
    project = _get_rw_project_for_user(request, code)
    if (request.GET.get('error') == 'access_denied'):
        messages.warning(request, 'Slack setup was cancelled.')
        return redirect('hc-channels', project.code)
    if (request.GET.get('state') != state):
        return HttpResponseForbidden()
    result = requests.post('https://slack.com/api/oauth.v2.access', {'client_id': settings.SLACK_CLIENT_ID, 'client_secret': settings.SLACK_CLIENT_SECRET, 'code': request.GET.get('code')})
    doc = result.json()
    if doc.get('ok'):
        channel = Channel(kind='slack', project=project)
        channel.value = result.text
        channel.save()
        channel.assign_all_checks()
        messages.success(request, 'The Slack integration has been added!')
    else:
        s = doc.get('error')
        messages.warning(request, ('Error message from slack: %s' % s))
    return redirect('hc-channels', project.code)
@require_setting('SLACK_CLIENT_ID')
@login_required
def add_slack_complete(request):
    if ('add_slack' not in request.session):
        return HttpResponseForbidden()
    (state, code) = request.session.pop('add_slack')
    project = _get_rw_project_for_user(request, code)
    if (request.GET.get('error') == 'access_denied'):
        messages.warning(request, 'Slack setup was cancelled.')
        return redirect('hc-channels', project.code)
    if (request.GET.get('state') != state):
        return HttpResponseForbidden()
    result = requests.post('https://slack.com/api/oauth.v2.access', {'client_id': settings.SLACK_CLIENT_ID, 'client_secret': settings.SLACK_CLIENT_SECRET, 'code': request.GET.get('code')})
    doc = result.json()
    if doc.get('ok'):
        channel = Channel(kind='slack', project=project)
        channel.value = result.text
        channel.save()
        channel.assign_all_checks()
        messages.success(request, 'The Slack integration has been added!')
    else:
        s = doc.get('error')
        messages.warning(request, ('Error message from slack: %s' % s))
    return redirect('hc-channels', project.code)