Skip to content

Commit

Permalink
Updated the acquire_target to be more lenient about list-like objects…
Browse files Browse the repository at this point in the history
… that it will parse while searching targets.
  • Loading branch information
lethain committed Aug 29, 2008
1 parent 6c20473 commit 59e61f6
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions django_monetize/templatetags/monetize.py
Expand Up @@ -42,15 +42,15 @@
"""

from django import template
from django.con import settings
from django.conf 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:])
return MonetizeSlotNode(*lst[1:])


class MonetizeSlotNode(template.Node):
Expand All @@ -65,6 +65,7 @@ def __init__(self, *vals):
def render(self,context):
'Apply targeting and render monetization option for value/slot combo.'
target = self.acquire_target(self.params,context)
print "(%s, %s) --> %s" % (self.slot,self.params,target)
return self.target(target,self.slot,context)

def acquire_target(self,params,context):
Expand All @@ -73,19 +74,19 @@ def acquire_target(self,params,context):

for param in params:
try:
print "trying to resolve param: %s" % param
param = template.resolve_variable(param,context)
except template.VariableDoesNotExist:
print "failed to resolve :("
pass
if type(param) == list or type(param) == tuple):
if type(param) == dict:
param = dict.iteritems()

if hasattr(param,'__iter__'):
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):
Expand All @@ -107,7 +108,9 @@ def target(self,value,slot,context):
if logic_dict.has_key(value):
logic = logic_dict[value]
else:
logic = getattr(setting,"MONETIZE_DEFAULT",False)
logic = getattr(settings,"MONETIZE_DEFAULT",False)

#print "logic(%s) for slot(%s),value(%s)" % (logic,slot,value)

# Deconstruct slot specific logic from dict.
if type(logic) == dict:
Expand All @@ -121,25 +124,30 @@ def target(self,value,slot,context):
# Otherwise display nothing.
logic = False

#print "(after dict) logic(%s) for slot(%s),value(%s)" % (logic,slot,value)

if type(logic) == tuple or type(logic) == list:
context_dict = getattr(settings.'MONETIZE_CONTEXT',{}).copy()
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',{})
context_dict = getattr(settings,'MONETIZE_CONTEXT',{})

#print "(after list) logic(%s) for slot(%s),value(%s)" % (logic,slot,value)

# At this point ``logic`` should be a string for a template, or False

#print "(pre render) logic(%s) for slot(%s),value(%s)" % (logic,slot,value)

if logic == False:
# False means no monetization option, so return empty string.
rendered = u""
else:
new_context = Context(context_dict,context.autoescape)
else:
new_context = template.Context(context_dict,context.autoescape)
t = template.loader.get_template(logic)
rendered = t.render(new_context)

Expand Down

0 comments on commit 59e61f6

Please sign in to comment.