Exemplo n.º 1
0
def main(request, appid, signed):
    fb_app = FBApp.objects.get(appid=appid)

    try:
        signature = oauth.handle_incoming(request, fb_app)
    except oauth.AccessDenied:
        signature = oauth.AccessSignature()

    if not signature.is_valid():
        # OAuth circumvented or denied
        # Record auth fail and redirect to error URL:
        # TODO: event
        namespace = request.resolver_match.namespace or 'gimmick'
        path = reverse(namespace + ':engage-intro') + "#denied"
        if oauth.EMBEDDED_NS_PATTERN.search(namespace):
            url = oauth.get_embedded_path(fb_app, path)
            return render(request, 'chapo/redirect.html', {'url': url})
        else:
            return redirect(path)

    session_key = 'engage_task_{}'.format(signature.code)
    try:
        task_id = request.session[session_key]
    except KeyError:
        oauth_token_job = store_oauth_token.s(
            signature.code,
            signature.redirect_uri,
            api=API_VERSION,
            app_id=appid,
        )
        scoring_job = celery.group(tasks.score_posts.s(),
                                   tasks.score_likes.s(),
                                   tasks.read_network.s())

        if signed and 'oauth_token' in signed and 'user_id' in signed:
            oauth_token_job.apply_async()
            token = ShortToken(
                fbid=int(signed['user_id']),
                appid=appid,
                token=signed['oauth_token'],
                api=API_VERSION,
            )
            chain_start = scoring_job.clone((token,))
        else:
            chain_start = oauth_token_job | scoring_job

        # celery won't give us a collected group ID, so post-process in a chord callback
        chain = chain_start | tasks.compute_rankings.s()
        task = chain.apply_async()
        task_id = request.session[session_key] = task.id

    return render(request, 'gimmick/engage.html', {
        'fb_app': fb_app,
        'task_id': task_id,
    })
Exemplo n.º 2
0
def incoming(request, api, campaign_id, content_id):
    campaign = get_object_or_404(models.Campaign, pk=campaign_id)
    properties = campaign.campaignproperties.get()
    fb_app = campaign.client.fb_app

    try:
        signature = oauth.handle_incoming(request, fb_app)
    except oauth.AccessDenied:
        # OAuth denial
        # Record auth fail and redirect to error URL:
        url = "{}?{}".format(
            reverse('targetshare:outgoing', args=[
                fb_app.appid,
                properties.client_error_url
            ]),
            urllib.urlencode({'campaignid': campaign_id}),
        )
        db.bulk_create.delay([
            models.relational.Event(
                visit_id=request.visit.visit_id,
                content=url[:1028],
                event_type='incoming_redirect',
                campaign_id=campaign_id,
                client_content_id=content_id,
            ),
            models.relational.Event(
                visit_id=request.visit.visit_id,
                event_type='auth_fail',
                content='oauth',
                campaign_id=campaign_id,
                client_content_id=content_id,
            ),
        ])
        return redirect(url)

    if signature.is_valid():
        token_task = store_oauth_token.delay(
            signature.code,
            signature.redirect_uri,
            api,
            client_id=campaign.client_id,
            visit_id=request.visit.visit_id,
            campaign_id=campaign_id,
            content_id=content_id,
        )
        request.session[OAUTH_TASK_KEY] = token_task.id

    # Record event and redirect to the campaign faces URL
    # (with inheritance of incoming query string)
    url = utils.faces_url(properties.client_faces_url,
                          campaign,
                          content_id,
                          signature.redirect_query)
    db.delayed_save.delay(
        models.relational.Event(
            visit_id=request.visit.visit_id,
            content=url[:1028],
            event_type='incoming_redirect',
            campaign_id=campaign_id,
            client_content_id=content_id,
        )
    )
    return http.HttpResponseRedirect(url)