Esempio n. 1
0
def auphonic_callback(request, slug):
    token = request.GET["code"]

    show = Show.objects.get(slug=slug)
    if not request.user.has_perm('radioportal.change_show', show):
        return HttpResponse('Unauthorized', status=401)

    url = "https://auphonic.com/oauth2/token/"
    cb = reverse('admin-show-notification-auphonic-callback', kwargs={'slug': slug}, host='dashboard')
    data = {
        'client_id': settings.AUPHONIC_CLIENT_ID,
        'client_secret': settings.AUPHONIC_CLIENT_SECRET,
        'redirect_uri': cb,
        'grant_type': "authorization_code",
        'code': token
    }

    r = requests.post(url, data=data)
    if r.status_code == 200:
        res = r.json()

        token = res["access_token"]

        url = "https://auphonic.com/api/user.json"
        headers = {"Authorization": "Bearer %s" % token}
        r = requests.get(url, headers=headers)
        if r.status_code == 200:
            res = r.json()

            account, created = AuphonicAccount.objects.get_or_create(userid=res["data"]["user_id"], primarynotification__show__slug=slug)

            account.access_token = token
            account.username = res["data"]["username"]

            account.save()

            if created:
                start = NotificationTemplate()
                start.save()
                stop = NotificationTemplate()
                stop.save()
                rollover = NotificationTemplate()
                rollover.save()

                noti = PrimaryNotification(path=account, start=start, stop=stop,
                                           rollover=rollover, show=show)
                noti.save()

            kwargs = {'slug': slug, "path": "auphonic", "nslug": noti.id}

            url = reverse('admin-show-notification-edit',
                               kwargs=kwargs, host='dashboard')

    url = reverse('admin-show-notification',
                       kwargs={'slug': slug}, host='dashboard')
    return redirect(url)
Esempio n. 2
0
def twitter_callback(request, slug, path):
    """
        User authorized us at twitter and got redirected with the
        necessary tokens. Obtain final tokens and create objects
        for storage
    """
    if not "oauth_verifier" in request.GET:
        url = reverse('admin-show-notification',
                       kwargs={'slug': slug}, host='dashboard')
        return redirect(url)
    oauth_verifier = request.GET['oauth_verifier']
    twitter = Twython(settings.TWITTER_CONSUMER_KEY,
                      settings.TWITTER_CONSUMER_SECRET,
                      request.session['twitter_%s_oauth_token' % path],
                      request.session['twitter_%s_oauth_token_secret' % path])
    final_step = twitter.get_authorized_tokens(oauth_verifier)

    del request.session['twitter_%s_oauth_token' % path]
    del request.session['twitter_%s_oauth_token_secret' % path]

    show = Show.objects.get(slug=slug)
    if not request.user.has_perm('radioportal.change_show', show):
        return HttpResponse('Unauthorized', status=401)

    twitter = Twython(settings.TWITTER_CONSUMER_KEY,
                      settings.TWITTER_CONSUMER_SECRET,
                      final_step['oauth_token'],
                      final_step['oauth_token_secret'])

    try:
        res = twitter.verify_credentials(skip_status=True)
    except TwythonError as e:
        return HttpResponse("Error occured during Twitter authorization: %s" %
                            e.msg)

    account = TwitterAccount(
        oauth_token=final_step['oauth_token'],
        oauth_secret=final_step['oauth_token_secret'],
        screen_name=res["screen_name"])
    account.save()

    if path == "primary":
        start = NotificationTemplate()
        start.save()
        stop = NotificationTemplate()
        stop.save()
        rollover = NotificationTemplate()
        rollover.save()
        noti = PrimaryNotification(path=account, start=start, stop=stop,
                                   rollover=rollover, show=show)
        noti.save()

        kwargs = {'slug': show.slug, "path": "twitter", "nslug": noti.id}
        url = reverse('admin-show-notification-edit',
                           kwargs=kwargs, host='dashboard')
    elif path == "secondary":
        noti = SecondaryNotification(show=show, path=account)
        noti.save()

        kwargs = {'slug': show.slug, "path": "twitter", "nslug": noti.id}
        url = reverse('admin-show-secondary-notification-edit',
                           kwargs=kwargs, host='dashboard')
    return redirect(url)