Example #1
0
def random_zone_ad(context, ad_category, ad_zone):
    """
    Returns a random advert from the database.

    In order for the impression to be saved add the following
    to the TEMPLATE_CONTEXT_PROCESSORS:

    'adzone.context_processors.get_source_ip'

    Tag usage:
    {% load adzone_tags %}
    {% random_zone_ad 'my_category_slug' 'zone_slug' %}

    """
    to_return = {}

    # Retrieve a random ad for the category and zone
    ad = AdBase.objects.get_random_ad(ad_category, ad_zone)
    to_return['ad'] = ad
    
    # Record a impression for the ad
    if context.has_key('from_ip') and ad:
        from_ip = context.get('from_ip')
        try:
            impression = AdImpression(
                    ad=ad,
                    impression_date=datetime.now(),
                    source_ip=from_ip
            )
            impression.save()
        except:
            pass
    return to_return
Example #2
0
def ad_by_title(context, ad_title, ad_zone):
    """
    Returns an advert for ``ad_zone``.
    The advert returned is independent of the category

    In order for the impression to be saved add the following
    to the TEMPLATE_CONTEXT_PROCESSORS:

    'adzone.context_processors.get_source_ip'

    Tag usage:
    {% load adzone_tags %}
    {% ad_by_name 'ad_name' 'zone_slug' %}

    """
    to_return = {}

    # Retrieve an ad for the zone
    ad = AdBase.objects.get_ad_by_title(ad_title, ad_zone)
    to_return['ad'] = ad

    # Record a impression for the ad
    if context.has_key('from_ip') and ad:
        from_ip = context.get('from_ip')
        try:
            impression = AdImpression(
                ad=ad, impression_date=datetime.now(), source_ip=from_ip)
            impression.save()
        except:
            pass
    return to_return
Example #3
0
def random_category_ad(context, ad_zone, ad_category):
    """
    Returns a random advert from the specified category.

    Usage:
    {% load adzone_tags %}
    {% random_category_ad 'zone_slug' 'my_category_slug' %}

    """
    to_return = {}

    # Retrieve a random ad for the category and zone
    ad = AdBase.objects.get_random_ad(ad_zone, ad_category)
    to_return['ad'] = ad

    # Record a impression for the ad
    if context.has_key('from_ip') and ad:
        from_ip = context.get('from_ip')
        try:
            impression = AdImpression(
                ad=ad, impression_date=datetime.now(), source_ip=from_ip)
            impression.save()
        except:
            pass
    return to_return
Example #4
0
def random_category_ad(context, ad_zone, ad_category):
    """
    Returns a random advert from the specified category.

    Usage:
    {% load adzone_tags %}
    {% random_category_ad 'zone_slug' 'my_category_slug' %}

    """
    to_return = {}

    # Retrieve a random ad for the category and zone
    # ~ ads = AdBase.objects.get_random_ad(ad_category, ad_zone)
    # ~ ad = None

    # ~ for a in ads:
    # ~ ic = len(AdImpression.objects.filter(ad=a))
    # ~ if ic < a.impression_limit:
    # ~ ad = a
    # ~ break

    ad = AdBase.objects.get_random_ad(ad_zone, ad_category)

    to_return["ad"] = ad
    to_return["request"] = context["request"]

    # Record a impression for the ad
    if context.has_key("from_ip") and ad:
        from_ip = context.get("from_ip")
        try:
            impression = AdImpression(ad=ad, impression_date=timezone.now(), source_ip=from_ip)
            impression.save()
        except:
            pass
    return to_return
Example #5
0
def random_zone_ad(context, ad_zone):
    """
    Returns a random advert for ``ad_zone``.
    The advert returned is independent of the category

    In order for the impression to be saved add the following
    to the TEMPLATE_CONTEXT_PROCESSORS:

    'adzone.context_processors.get_source_ip'

    Tag usage:
    {% load adzone_tags %}
    {% random_zone_ad 'zone_slug' %}

    """
    to_return = {}

    # Retrieve a random ad for the zone
    ad = AdBase.objects.get_random_ad(ad_zone)
    to_return["ad"] = ad

    # Record a impression for the ad
    if context.has_key("from_ip") and ad:
        from_ip = context.get("from_ip")
        try:
            impression = AdImpression(ad=ad, impression_date=timezone.now(), source_ip=from_ip)
            impression.save()
        except:
            pass
    to_return["request"] = context["request"]
    return to_return
Example #6
0
def random_category_ad(context, ad_zone, ad_category):
    """
    Returns a random advert from the specified category.

    Usage:
    {% load adzone_tags %}
    {% random_category_ad 'zone_slug' 'my_category_slug' %}

    """
    to_return = {}

    # Retrieve a random ad for the category and zone
    ad = AdBase.objects.get_random_ad(ad_zone, ad_category)
    to_return['ad'] = ad

    # Record a impression for the ad
    if 'from_ip' in context and ad:
        from_ip = context.get('from_ip')
        try:
            impression = AdImpression(ad=ad,
                                      impression_date=datetime.now(),
                                      source_ip=from_ip)
            impression.save()
        except:
            pass
    return to_return
Example #7
0
def random_zone_ad(context, ad_zone):
    """
    Returns a random advert for ``ad_zone``.
    The advert returned is independent of the category

    In order for the impression to be saved add the following
    to the TEMPLATE_CONTEXT_PROCESSORS:

    'adzone.context_processors.get_source_ip'

    Tag usage:
    {% load adzone_tags %}
    {% random_zone_ad 'zone_slug' %}

    """
    to_return = {}

    # Retrieve a random ad for the zone
    ad = AdBase.objects.get_random_ad(ad_zone)
    to_return['ad'] = ad

    # Record a impression for the ad
    if 'from_ip' in context and ad:
        from_ip = context.get('from_ip')
        try:
            impression = AdImpression(ad=ad,
                                      impression_date=datetime.now(),
                                      source_ip=from_ip)
            impression.save()
        except:
            pass
    return to_return
Example #8
0
def random_many_ad(context, ad_zone, ad_category=None, cnt=1):
    """
    Returns a queryset with random adverts for specified zone and
    from the specified category if passed.
    Write AdImpression for all returned adverts

    Usage:
    {% load adzone_tags %}
    {% random_many_ad ad_zone='zone_slug' ad_category='my_category_slug' cnt=5 as ad_qs %}

    """

    ad_qs = AdBase.objects.get_random_ad(ad_zone, ad_category, cnt)

    from_ip = context.get('from_ip')
    if from_ip:
        for ad in ad_qs:
            try:
                impression = AdImpression(
                    ad=ad, impression_date=datetime.now(), source_ip=from_ip)
                impression.save()
            except:
                pass

    return ad_qs
Example #9
0
def random_zone_ad(context, ad_zone, cnt=1):
    """
    Returns a random advert for ``ad_zone``.
    The advert returned is independent of the category

    In order for the impression to be saved add the following
    to the TEMPLATE_CONTEXT_PROCESSORS:

    'adzone.context_processors.get_source_ip'

    Tag usage:
    {% load adzone_tags %}
    {% random_zone_ad 'zone_slug' %}

    """
    to_return = {'ad_zone': ad_zone, 'ad_category': None}

    # Retrieve a random ad for the zone
    try:
        ad = AdBase.objects.get_random_ad(ad_zone)[0]
    except IndexError:
        ad = None
    to_return['ad'] = ad

    # Record a impression for the ad
    if 'from_ip' in context and ad:
        from_ip = context.get('from_ip')
        try:
            impression = AdImpression(
                ad=ad, impression_date=datetime.now(), source_ip=from_ip)
            impression.save()
        except:
            pass
    return to_return
Example #10
0
def random_zone_ad_block(context, ad_category, ad_zone, number):
    """
    Returns a block of random adverts from the database.

    In order for the impression to be saved add the following
    to the TEMPLATE_CONTEXT_PROCESSORS:

    'adzone.context_processors.get_source_ip'

    Tag usage:
    {% load adzone_tags %}
    {% random_zone_ad_block 'my_category_slug' 'zone_slug' 4 %}

    """
    to_return = {}

    # Retrieve a random ad for the category and zone
    ads=[]

    # Check whether category has been specified
    if ad_category:
        lookup_ads=AdBase.objects.filter(enabled=True, category__slug=ad_category, zone__slug=ad_zone)
    else:
        lookup_ads=AdBase.objects.filter(enabled=True, zone__slug=ad_zone)

    # If we have fewer ads in system, adjust our block
    if len(lookup_ads) < number:
        number = len(lookup_ads)

    while(number > 0):
        new_ad= AdBase.objects.get_random_ad(ad_category, ad_zone)
        if new_ad in ads:
            pass
        else:
            ads.append(new_ad)
            number=number-1
        
            # Record a impression for the ad
            if new_ad:
                excluded_ip = context.get('excluded_ip')
                if not excluded_ip:
                    try:
                        impression = AdImpression(
                                ad=new_ad,
                                impression_date=datetime.now(),
                                source_ip=context.get('from_ip')
                                )
                        impression.save()
                    except:
                        pass
    to_return['ads'] = ads
    return to_return
Example #11
0
def ad_display(request):
    """
    Simply pull an ad from the db and display it as a raw img.
    This was added to interface with legacy apps in iframes.
    Currently it's hardwired to calendar zone, but could be made scriptabled via GET params.
    """
    context = RequestContext(request)
    ad = AdBase.objects.get_random_ad('', 'calendar')
    from_ip = request.META['REMOTE_ADDR']
    if not request.excluded_ip:
        impression = AdImpression(ad=ad, impression_date=datetime.now(), source_ip=from_ip)
        impression.save()
    return render_to_response('adzone/ad_display.html', locals(), context_instance=RequestContext(request))
Example #12
0
 def test_model(self):
     advert = create_advert()
     AdImpression(
         impression_date=datenow(),
         source_ip='127.0.0.1',
         ad=advert,
     )