Пример #1
0
def button(request, api, campaign_id, content_id):
    campaign = get_object_or_404(models.relational.Campaign, campaign_id=campaign_id)
    client = campaign.client
    content = get_object_or_404(client.clientcontent, content_id=content_id)

    goto = sharingurls.incoming_redirect(request.is_secure(), request.get_host(), campaign, content)

    page_styles = utils.assign_page_styles(
        request.visit,
        models.relational.Page.BUTTON,
        campaign,
        content,
    )

    return render(request, 'targetshare/button.html', {
        'goto': goto,
        'campaign': campaign,
        'content': content,
        'campaign_css': page_styles,
        'fb_params': {
            'fb_app_name': client.fb_app.name,
            'fb_app_id': client.fb_app_id
        },
    })
Пример #2
0
def frame_faces(request, api, campaign_id, content_id):
    campaign = get_object_or_404(relational.Campaign, campaign_id=campaign_id)
    campaign_properties = campaign.campaignproperties.get()

    if campaign_properties.root_campaign_id != campaign_properties.campaign_id:
        LOG.warning("Received request for non-root campaign", extra={'request': request})
        raise http.Http404

    try:
        campaign_status = campaignstatus.handle_request(request, campaign, campaign_properties)
    except campaignstatus.DisallowedError as exc:
        return exc.make_error_response(embedded=True)

    client = campaign.client
    content = get_object_or_404(client.clientcontent, content_id=content_id)
    canvas = bool(re.search(r'\bcanvas\b', request.resolver_match.namespace))

    db.bulk_create.delay([
        relational.Event(
            visit_id=request.visit.pk,
            event_type='faces_page_load',
            campaign_id=campaign.pk,
            client_content_id=content.pk,
            content=api,
        ),
        relational.Event(
            visit_id=request.visit.pk,
            event_type=('faces_canvas_load' if canvas else 'faces_iframe_load'),
            campaign_id=campaign.pk,
            client_content_id=content.pk,
            content=api,
        )
    ])

    # If the visitor passed through FB OAuth and our incoming redirector, we
    # may be able to retrieve the result of their store_oauth_token job,
    # determine their FBID & token, and eagerly start their targeting jobs.
    task_id_oauth = request.session.get(OAUTH_TASK_KEY)
    if task_id_oauth:
        task_oauth = celery.current_app.AsyncResult(task_id_oauth)
        if task_oauth.ready():
            del request.session[OAUTH_TASK_KEY] # no need to do this again
            token = task_oauth.result if task_oauth.successful() else None
            if token:
                faces_tasks_key = FACES_TASKS_KEY.format(api=api,
                                                         campaign_id=campaign_id,
                                                         content_id=content_id,
                                                         fbid=token.fbid)
                if faces_tasks_key not in request.session:
                    # Initiate targeting tasks:
                    targeting_tasks = request_targeting(
                        visit=request.visit,
                        token=token,
                        api=api,
                        campaign=campaign,
                        client_content=content,
                        num_faces=campaign_properties.num_faces,
                    )
                    request.session[faces_tasks_key] = [
                        (rank, task.id) for (rank, task) in targeting_tasks
                    ]

    page_styles = utils.assign_page_styles(
        request.visit,
        relational.Page.FRAME_FACES,
        campaign,
        content,
    )

    (serialized_properties,) = serialize('python', (campaign_properties,))
    properties = serialized_properties['fields']
    for override_key, field in [
        ('efsuccessurl', 'client_thanks_url'),
        ('eferrorurl', 'client_error_url'),
    ]:
        value = request.REQUEST.get(override_key) or properties[field]
        properties[field] = "{}?{}".format(
            reverse('targetshare:outgoing', args=[client.fb_app_id, value]),
            urllib.urlencode({'campaignid': campaign_id}),
        )

    default_permissions = client.fb_app.permissions.values_list('code', flat=True)

    return render(request, 'targetshare/frame_faces.html', {
        'fb_params': {
            'fb_app_name': client.fb_app.name,
            'fb_app_id': client.fb_app_id,
        },
        'api': api,
        'default_scope': ','.join(default_permissions.iterator()),
        'campaign': campaign,
        'content': content,
        'properties': properties,
        'campaign_css': page_styles,
        'canvas': canvas,
        'draft_preview': campaign_status.isdraft,
        # Debug mode currently on for all methods of targeted sharing
        # However will likely just reflect the canvas var in the future
        'debug_mode': True,
    })
Пример #3
0
def faces_email_friends(request, notification_uuid):
    ''' A view that's fairly similar to our Faces/Frame Faces views, except
    that this will not perform any crawls. We've already done the crawling
    in the background, so we can skip that here, and instead leverage the
    friends passed in via GET params.
    '''
    # Campaign setup
    notification_user = get_object_or_404(
        relational.NotificationUser,
        uuid=notification_uuid
    )
    campaign = notification_user.notification.campaign
    content = notification_user.notification.client_content
    client = campaign.client
    utils.set_visit(request, client.fb_app_id, notification_user.fbid, {
        'campaign': campaign,
        'client_content': content,
    })

    # Gather friend data
    user = dynamo.User.items.get_item(fbid=notification_user.fbid)
    friend_fbids = notification_user.events.filter(
        event_type__in=('generated', 'shown')
    ).values_list('friend_fbid', flat=True).distinct()
    all_friends = dynamo.User.items.batch_get(
        keys=LazyList({'fbid': fbid} for fbid in friend_fbids.iterator())
    )
    shown_fbids = set(
        notification_user.events.filter(
            event_type='shown',
        ).values_list('friend_fbid', flat=True).iterator()
    )
    num_face = len(shown_fbids)

    (face_friends, show_faces) = ([], [])
    for friend in all_friends:
        if friend.fbid in shown_fbids:
            show_faces.append(friend)
        else:
            face_friends.append(friend)

    db.delayed_save.delay(
        relational.Event(
            visit_id=request.visit.pk,
            campaign_id=campaign.pk,
            client_content_id=content.pk,
            event_type='faces_email_page_load',
        )
    )

    # FBObj setup
    fb_object = campaign.campaignfbobjects.for_datetime().random_assign()
    db.delayed_save.delay(
        relational.Assignment.make_managed(
            visit_id=request.visit.pk,
            campaign_id=campaign.pk,
            content_id=content.pk,
            feature_row=fb_object,
            chosen_from_rows=campaign.campaignfbobjects.for_datetime(),
        )
    )
    fb_attrs = fb_object.fbobjectattribute_set.get()
    fb_object_url = 'https://%s%s?%s' % (
        request.get_host(),
        reverse('targetshare:objects', kwargs={
            'fb_object_id': fb_object.pk,
            'content_id': content.pk,
        }),
        urllib.urlencode({
            'campaign_id': campaign.pk,
        }),
    )

    fb_params = {
        'fb_action_type': fb_attrs.og_action,
        'fb_object_type': fb_attrs.og_type,
        'fb_object_url': fb_object_url,
        'fb_app_name': client.fb_app.name,
        'fb_app_id': client.fb_app_id,
        'fb_object_title': fb_attrs.og_title,
        'fb_object_image': fb_attrs.og_image,
        'fb_object_description': fb_attrs.og_description
    }
    msg_params = {
        'sharing_prompt': fb_attrs.sharing_prompt,
        'msg1_pre': fb_attrs.msg1_pre,
        'msg1_post': fb_attrs.msg1_post,
        'msg2_pre': fb_attrs.msg2_pre,
        'msg2_post': fb_attrs.msg2_post,
    }
    content_str = '%s:%s %s' % (
        fb_params['fb_app_name'],
        fb_params['fb_object_type'],
        fb_params['fb_object_url']
    )
    db.bulk_create.delay([
        relational.Event(
            visit_id=request.visit.pk,
            campaign_id=event.campaign.pk,
            client_content_id=event.client_content.pk,
            friend_fbid=event.friend_fbid,
            content=content_str,
            event_type=event.event_type,
        )
        for event in notification_user.events.iterator()
    ])
    db.bulk_create.delay([
        relational.Assignment(
            visit_id=request.visit.pk,
            campaign_id=assignment.campaign.pk,
            content_id=assignment.content.pk,
            feature_type=assignment.feature_type,
            feature_row=assignment.feature_row,
            random_assign=assignment.random_assign,
            chosen_from_table=assignment.chosen_from_table,
            chosen_from_rows=assignment.chosen_from_rows,
        )
        for assignment in notification_user.assignments.iterator()
    ])

    page_styles = utils.assign_page_styles(
        request.visit,
        relational.Page.FRAME_FACES,
        campaign,
        content,
    )

    return render(request, 'targetshare/faces_email_friends.html', {
        'fb_params': fb_params,
        'msg_params': msg_params,
        'campaign': campaign,
        'content': content,
        'properties': campaign.campaignproperties.get(),
        'campaign_css': page_styles,
        'all_friends': all_friends,
        'face_friends': face_friends,
        'show_faces': show_faces,
        'user': user,
        'num_face': num_face,
    })