def network_advertiser_tracking_edit(request, advertiser_id, action_id):
    ''' View to allow a Network Admin to edit the tracking actions of an Advertiser '''

    advertiser = get_object_or_404(request.user.get_profile().admin_assigned_advertisers(), id=advertiser_id)
    action = get_object_or_404(advertiser.action_set, id=action_id)
    apeRedirect = 0
    securePixel = ""
    nonSecurePixel = ""
    if request.method == 'POST':
        form = NetworkActionForm(request.POST)

        if form.is_valid():
            
            if action.ape_redirect_id == None or action.ape_redirect_id == 0:
                apeClient = Ape()
                success, createPixel = apeClient.execute_redirect_create()
                if success:
                    action.ape_redirect_id = createPixel['redirect_id']
            
            action.status = form.cleaned_data['status']
            action.name = form.cleaned_data['name']
            if form.cleaned_data['invite_id']:
                action.invite_id = form.cleaned_data['invite_id']
                
            action.network_fee = 0
            action.advertiser_payout_type=str(form.cleaned_data['advertiser_payout_type'])
            action.advertiser_payout_amount=str(form.cleaned_data['advertiser_payout_amount'])
            action.save()            
            return HttpResponseRedirect('/network/advertiser/tracking/%d/' % advertiser.id)
    else:
        if action.ape_redirect_id > 0:
            apeRedirect = base36_encode(action.ape_redirect_id)            
            securePixel = settings.APE_SECURE_PIXEL_URL + str(apeRedirect)
            nonSecurePixel = settings.APE_PIXEL_URL + str(apeRedirect)
            if action.ape_action_id != None:
                apeAction = base36_encode(action.ape_action_id)      
                securePixel = '%s/%s/' % (securePixel, apeAction)
                nonSecurePixel = '%s/%s/' % (nonSecurePixel, apeAction)
                
        else:
            apeRedirect = 0
        form = NetworkActionForm(initial={ 'name' : action.name, 'status' : action.status,
                                           'advertiser_payout_type' : action.advertiser_payout_type,
                                           'advertiser_payout_amount' : action.advertiser_payout_amount,
                                           'invite_id' : action.invite_id })

    
    return AQ_render_to_response(request, 'network/tracking-edit.html', {
            'advertiser' : advertiser,
            'action' : action,
            'form' : form,
            'apeRedirect' : apeRedirect,
            'securePixel' : securePixel,
            'nonSecurePixel' : nonSecurePixel,
        }, context_instance=RequestContext(request))
def network_advertiser_tracking_add(request, id):
    ''' View to allow a Network Admin to create new Tracking Actions for an Advertiser '''
    from atrinsic.base.models import Action
    from forms import NetworkActionForm
    advertiser = get_object_or_404(
        request.user.get_profile().admin_assigned_advertisers(), id=id)
    if request.method == 'POST':
        form = NetworkActionForm(request.POST)
        if form.is_valid():
            apeClient = Ape()
            ape_redirect_id = None
            ape_action_id = None

            getActions = Action.objects.filter(
                advertiser=advertiser).order_by('id')
            if getActions.count() == 0:
                #Call APE to create new REDIRECT
                success, createPixel = apeClient.execute_redirect_create()
                if success:
                    ape_redirect_id = createPixel['redirect_id']

            else:
                #Use RedirectID from Oldest Action(order_by('id')) and create actions under that RedirectID
                ape_redirect_id = getActions[0].ape_redirect_id

            #Call APE and create Action with redirect determined above.
            success, createAction = apeClient.execute_action_create(
                ape_redirect_id, form.cleaned_data['name'])
            if success:
                print "createAction - %s" % createAction
                ape_action_id = createAction['action_id']

            a = Action.objects.create(
                advertiser=advertiser,
                name=form.cleaned_data['name'],
                status=form.cleaned_data['status'],
                network_fee=0,
                ape_redirect_id=ape_redirect_id,
                ape_action_id=ape_action_id,
                advertiser_payout_type=str(
                    form.cleaned_data['advertiser_payout_type']),
                advertiser_payout_amount=str(
                    form.cleaned_data['advertiser_payout_amount']))

            return HttpResponseRedirect('/network/advertiser/tracking/%d/' %
                                        advertiser.id)
    else:
        form = NetworkActionForm()

    return AQ_render_to_response(request,
                                 'network/tracking-add.html', {
                                     'advertiser': advertiser,
                                     'form': form,
                                 },
                                 context_instance=RequestContext(request))
def network_advertiser_tracking_add(request, id):
    ''' View to allow a Network Admin to create new Tracking Actions for an Advertiser '''
    from atrinsic.base.models import Action
    from forms import NetworkActionForm
    advertiser = get_object_or_404(request.user.get_profile().admin_assigned_advertisers(), id=id)
    if request.method == 'POST':
        form = NetworkActionForm(request.POST)
        if form.is_valid():                
            apeClient = Ape()
            ape_redirect_id = None   
            ape_action_id = None 

            getActions = Action.objects.filter(advertiser=advertiser).order_by('id')
            if getActions.count() == 0:
                #Call APE to create new REDIRECT
                success, createPixel = apeClient.execute_redirect_create()            
                if success:
                    ape_redirect_id = createPixel['redirect_id']

            else:
                #Use RedirectID from Oldest Action(order_by('id')) and create actions under that RedirectID
                ape_redirect_id = getActions[0].ape_redirect_id
            
                
            #Call APE and create Action with redirect determined above.
            success, createAction = apeClient.execute_action_create(ape_redirect_id,form.cleaned_data['name'])       
            if success:
                print "createAction - %s" % createAction
                ape_action_id = createAction['action_id']      
            
            a = Action.objects.create(advertiser=advertiser, name=form.cleaned_data['name'],
                                      status=form.cleaned_data['status'], network_fee=0,
                                      ape_redirect_id=ape_redirect_id,
                                      ape_action_id=ape_action_id,
                                      advertiser_payout_type=str(form.cleaned_data['advertiser_payout_type']),
                                      advertiser_payout_amount=str(form.cleaned_data['advertiser_payout_amount']))
            
            return HttpResponseRedirect('/network/advertiser/tracking/%d/' % advertiser.id)
    else:
        form = NetworkActionForm()

    return AQ_render_to_response(request, 'network/tracking-add.html', {
            'advertiser' : advertiser,
            'form' : form,
        }, context_instance=RequestContext(request))
def network_advertiser_tracking_edit(request, advertiser_id, action_id):
    ''' View to allow a Network Admin to edit the tracking actions of an Advertiser '''

    advertiser = get_object_or_404(
        request.user.get_profile().admin_assigned_advertisers(),
        id=advertiser_id)
    action = get_object_or_404(advertiser.action_set, id=action_id)
    apeRedirect = 0
    securePixel = ""
    nonSecurePixel = ""
    if request.method == 'POST':
        form = NetworkActionForm(request.POST)

        if form.is_valid():

            if action.ape_redirect_id == None or action.ape_redirect_id == 0:
                apeClient = Ape()
                success, createPixel = apeClient.execute_redirect_create()
                if success:
                    action.ape_redirect_id = createPixel['redirect_id']

            action.status = form.cleaned_data['status']
            action.name = form.cleaned_data['name']
            if form.cleaned_data['invite_id']:
                action.invite_id = form.cleaned_data['invite_id']

            action.network_fee = 0
            action.advertiser_payout_type = str(
                form.cleaned_data['advertiser_payout_type'])
            action.advertiser_payout_amount = str(
                form.cleaned_data['advertiser_payout_amount'])
            action.save()
            return HttpResponseRedirect('/network/advertiser/tracking/%d/' %
                                        advertiser.id)
    else:
        if action.ape_redirect_id > 0:
            apeRedirect = base36_encode(action.ape_redirect_id)
            securePixel = settings.APE_SECURE_PIXEL_URL + str(apeRedirect)
            nonSecurePixel = settings.APE_PIXEL_URL + str(apeRedirect)
            if action.ape_action_id != None:
                apeAction = base36_encode(action.ape_action_id)
                securePixel = '%s/%s/' % (securePixel, apeAction)
                nonSecurePixel = '%s/%s/' % (nonSecurePixel, apeAction)

        else:
            apeRedirect = 0
        form = NetworkActionForm(
            initial={
                'name': action.name,
                'status': action.status,
                'advertiser_payout_type': action.advertiser_payout_type,
                'advertiser_payout_amount': action.advertiser_payout_amount,
                'invite_id': action.invite_id
            })

    return AQ_render_to_response(request,
                                 'network/tracking-edit.html', {
                                     'advertiser': advertiser,
                                     'action': action,
                                     'form': form,
                                     'apeRedirect': apeRedirect,
                                     'securePixel': securePixel,
                                     'nonSecurePixel': nonSecurePixel,
                                 },
                                 context_instance=RequestContext(request))