@require_setting('PUSHBULLET_CLIENT_ID')
@login_required
def add_pushbullet_complete(request):
    if ('add_pushbullet' not in request.session):
        return HttpResponseForbidden()
    (state, code) = request.session.pop('add_pushbullet')
    project = _get_rw_project_for_user(request, code)
    if (request.GET.get('error') == 'access_denied'):
        messages.warning(request, 'Pushbullet setup was cancelled.')
        return redirect('hc-channels', project.code)
    if (request.GET.get('state') != state):
        return HttpResponseForbidden()
    result = requests.post('https://api.pushbullet.com/oauth2/token', {'client_id': settings.PUSHBULLET_CLIENT_ID, 'client_secret': settings.PUSHBULLET_CLIENT_SECRET, 'code': request.GET.get('code'), 'grant_type': 'authorization_code'})
    doc = result.json()
    if ('access_token' in doc):
        channel = Channel(kind='pushbullet', project=project)
        channel.value = doc['access_token']
        channel.save()
        channel.assign_all_checks()
        messages.success(request, 'The Pushbullet integration has been added!')
    else:
        messages.warning(request, 'Something went wrong')
    return redirect('hc-channels', project.code)
@require_setting('PUSHBULLET_CLIENT_ID')
@login_required
def add_pushbullet_complete(request):
    if ('add_pushbullet' not in request.session):
        return HttpResponseForbidden()
    (state, code) = request.session.pop('add_pushbullet')
    project = _get_rw_project_for_user(request, code)
    if (request.GET.get('error') == 'access_denied'):
        messages.warning(request, 'Pushbullet setup was cancelled.')
        return redirect('hc-channels', project.code)
    if (request.GET.get('state') != state):
        return HttpResponseForbidden()
    result = requests.post('https://api.pushbullet.com/oauth2/token', {'client_id': settings.PUSHBULLET_CLIENT_ID, 'client_secret': settings.PUSHBULLET_CLIENT_SECRET, 'code': request.GET.get('code'), 'grant_type': 'authorization_code'})
    doc = result.json()
    if ('access_token' in doc):
        channel = Channel(kind='pushbullet', project=project)
        channel.value = doc['access_token']
        channel.save()
        channel.assign_all_checks()
        messages.success(request, 'The Pushbullet integration has been added!')
    else:
        messages.warning(request, 'Something went wrong')
    return redirect('hc-channels', project.code)