Permalink
Browse files

Initial implementation of monetize.py, the templatetag library that s…

…upports django_monetize, and also updated README to reflect minor change in usage.
  • Loading branch information...
1 parent 6004d5f commit 7592cafc7385b3f3f9c7565374fab1a0b9c3e7c8 @lethain committed Aug 29, 2008
Showing with 114 additions and 6 deletions.
  1. +8 −3 README
  2. +106 −3 django_monetize/templatetags/monetize.py
View
11 README
@@ -50,20 +50,25 @@ For example, consider the simplest situation where you wanted to display AdSense
Now lets say you want to display a link to an Amazon Affiliate's search results page when targetted on a list containing ``django`` (and otherwise default back to an Adsense ad unit):
MONETIZE_TARGET = {
- 'django':['django_monetize/amazon_search.html','Django books','Buy some Django books today!'],
+ 'django':['django_monetize/amazon_search.html',('amazon_search_terms','Django books'),('amazon_search_title','Buy some Django books today!')],
}
MONETIZE_DEFAULT = 'django_monetize/adsense_ad_unit.html'
+If you specify a monetization method using a list/tuple instead of a string, then the zeroth value is the template's string, and the remaining values are 2-tuples (or 2-lists) containing a key and a value, which are used to overwrite values in ``MONETIZE_CONTEXT`` for this specfic monetization.
+
Now lets say you have three ad slots on your page: 'header','footer', and 'side'. You can customize the contents of each slot for each term. For this example, lets not show any ads by default.
MONETIZE_TARGET = {
'django':{
'header':'django_monetize/adsense_ad_unit.html',
'footer':'django_monetize/slicehost_referral.html',
- # if you don't specify an ad for a slot, it uses the default ad instead
+ None:'django_monetize/dreamhost_referral.html',
+ # Value for None specifies value for non-listed slots.
+ # if you don't specify an ad for a slot, and don't specify
+ # a value default, then it won't display an ad.
},
}
- MONETIZE_DEFAULT = None
+ MONETIZE_DEFAULT = False
Specifying an ad you either pass a string with the ads template, or you pass a list (or tuple) where the zeroth object is the template's string and the other objects are parameters for the rendering function. Whether or not a specific monetization option takes parameters varies, so you'll have to consult the documentation (or the source, if documentation is lacking).
View
109 django_monetize/templatetags/monetize.py
@@ -5,13 +5,13 @@
{% load monetize %}
- {% monetize-slot "top" object.tags %}
+ {% monetize_slot "top" object.tags %}
<p> Some content. </p>
- {% monetize-slot "bottom" request.META.HTTP_USER_AGENT %}
+ {% monetize_slot "bottom" request.META.HTTP_USER_AGENT %}
<div class="sidebar">
- {% monetize-slot "side bar" request.META object.tags "django" %}
+ {% monetize_slot "side bar" request.META object.tags "django" %}
</div>
@@ -41,3 +41,106 @@
Don't be fooled by the above example: ``django_monetize`` doesn't help you inject ``request`` into your templates' context; you'll have to handle that yourself.
"""
+from django import template
+from django.con import settings
+
+register = template.Library()
+
+@register.tag(name="monetize_slot")
+def monetize_slot(parser, token):
+ 'Template tag for displaying a monetization option in a slot.'
+ lst = token.split_contents()
+ return MonetizeSlotNode(lst[1:])
+
+
+class MonetizeSlotNode(template.Node):
+ def __init__(self, *vals):
+ if len(vals) > 0:
+ self.slot = vals[0]
+ self.params = vals[1:]
+ else:
+ self.slot = None
+ self.params = ()
+
+ def render(self,context):
+ 'Apply targeting and render monetization option for value/slot combo.'
+ target = self.acquire_target(self.params,context)
+ return self.target(target,self.slot,context)
+
+ def acquire_target(self,params,context):
+ 'Go through parameters and try to find a valid targeting parameter.'
+ logic_dict = getattr(settings,'MONETIZE_TARGET',{})
+
+ for param in params:
+ try:
+ param = template.resolve_variable(param,context)
+ except template.VariableDoesNotExist:
+ pass
+ if type(param) == list or type(param) == tuple):
+ for x in param:
+ x = unicode(x)
+ if logic_dict.has_key(x):
+ return x
+ elif type(param) == dict:
+ for x in dict.iteritems():
+ x = unicode(x)
+ if logic_dict.has_key(x):
+ return x
+ else:
+ param = unicode(param)
+ if logic_dict.has_key(param):
+ return param
+
+ return None
+
+ def target(self,value,slot,context):
+ '''
+ Returns the rendered text for 'value'. 'value' should be
+ the output of the 'choose_target' method.
+
+ Also be aware the distinction being made between
+ False and None. None refers to the concept of using
+ the default monetization option, while False refers
+ to not using a monetization option.
+ '''
+ logic_dict = getattr(settings,'MONETIZE_TARGET',{})
+ if logic_dict.has_key(value):
+ logic = logic_dict[value]
+ else:
+ logic = getattr(setting,"MONETIZE_DEFAULT",False)
+
+ # Deconstruct slot specific logic from dict.
+ if type(logic) == dict:
+ if logic.has_key(slot):
+ # Check for slot specific logic.
+ logic = logic[slot]
+ elif logic.has_key(None):
+ # Check for value specific default logic.
+ logic = logic[None]
+ else:
+ # Otherwise display nothing.
+ logic = False
+
+ if type(logic) == tuple or type(logic) == list:
+ context_dict = getattr(settings.'MONETIZE_CONTEXT',{}).copy()
+ if len(logic) == 0:
+ logic = False
+ else:
+ # load extra context from list
+ for key,val in logic[1:]:
+ context_dict[key] = val
+ else:
+ context_dict = getattr(settings.'MONETIZE_CONTEXT',{})
+
+ # At this point ``logic`` should be a string for a template, or False
+
+
+ if logic == False:
+ # False means no monetization option, so return empty string.
+ rendered = u""
+ else:
+ new_context = Context(context_dict,context.autoescape)
+ t = template.loader.get_template(logic)
+ rendered = t.render(new_context)
+
+ return rendered

0 comments on commit 7592caf

Please sign in to comment.