Re: Weird problem with rendering a Variable in a custom tag

2011-05-23 Thread bruno desthuilliers
On May 23, 5:37 am, stevedegrace  wrote:
> I developed a custom tag to look up the country of a certain IP
> address using an IP to country database. It's sort of rough and ready,
> but it should work. The idea is that you read the comment.ip_address,
> feed it to the {% country %} tag, which then spits out the two letter
> country code. The interesting thing is that the tag works once and
> only once per page load if it has to render a variable. All subsequent
> calls raise template.VariableDoesNotExist in the code below.
>
> The interesting thing is that if you strip out the ability to
> recognize an IP as a string and make it just render variables, and you
> bind self.ip as a Variable object in the __init__ method and then try
> to call its render method in the CountryNode's render method, it
> actually raises AttributeError and claims that Variable object has no
> attribute render. Weird! FYI, I'm using Django 1.2. Anyone have any
> thoughts about what the heck is going on?

Avoid rebinding self.ip in the render method and it should work just
fine.

> Here's the Python code:
(snip imports)
> class CountryNode(template.Node):
>     def __init__(self, ip):
>         self.ip = ip
>
>     def render(self, context):
>         if self.ip[0] in '\'"':
>             self.ip = self.ip.strip('\'"')

don't rebind self.ip here.

>         else:
>             try:
>                 self.ip = Variable(self.ip).resolve(context)

don't rebind it here neither

You want to work on local variable, definitly.

HTH

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Weird problem with rendering a Variable in a custom tag

2011-05-23 Thread Brice Leroy
Be careful will computing location on rendering. IPs will change in time. The 
pattern is not clear but expect the information to expire.

As you only extract the country, it shouldn't be an issue, but if you drill 
down to Zip code I would commend you to store it with the comment's author 
(less expensive) or the comment.

Brice Leroy 


On May 23, 2011, at 1:34 AM, bruno desthuilliers 
 wrote:

> On May 23, 5:37 am, stevedegrace  wrote:
>> I developed a custom tag to look up the country of a certain IP
>> address using an IP to country database. It's sort of rough and ready,
>> but it should work. The idea is that you read the comment.ip_address,
>> feed it to the {% country %} tag, which then spits out the two letter
>> country code. The interesting thing is that the tag works once and
>> only once per page load if it has to render a variable. All subsequent
>> calls raise template.VariableDoesNotExist in the code below.
>> 
>> The interesting thing is that if you strip out the ability to
>> recognize an IP as a string and make it just render variables, and you
>> bind self.ip as a Variable object in the __init__ method and then try
>> to call its render method in the CountryNode's render method, it
>> actually raises AttributeError and claims that Variable object has no
>> attribute render. Weird! FYI, I'm using Django 1.2. Anyone have any
>> thoughts about what the heck is going on?
> 
> Avoid rebinding self.ip in the render method and it should work just
> fine.
> 
>> Here's the Python code:
> (snip imports)
>> class CountryNode(template.Node):
>> def __init__(self, ip):
>> self.ip = ip
>> 
>> def render(self, context):
>> if self.ip[0] in '\'"':
>> self.ip = self.ip.strip('\'"')
> 
> don't rebind self.ip here.
> 
>> else:
>> try:
>> self.ip = Variable(self.ip).resolve(context)
> 
> don't rebind it here neither
> 
> You want to work on local variable, definitly.
> 
> HTH
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Weird problem with rendering a Variable in a custom tag

2011-05-24 Thread stevedegrace
Thanks for the advice. I'm actually using this tag on a private
moderation page to quickly satisfy my curiosity about which countries
generate the most spam in my comments (FYI: Russia is winning hands
down so far).

On May 23, 10:24 am, Brice Leroy  wrote:
> Be careful will computing location on rendering. IPs will change in time. The 
> pattern is not clear but expect the information to expire.
>
> As you only extract the country, it shouldn't be an issue, but if you drill 
> down to Zip code I would commend you to store it with the comment's author 
> (less expensive) or the comment.
>
> Brice Leroy
>
> On May 23, 2011, at 1:34 AM, bruno desthuilliers 
>  wrote:
>
> > On May 23, 5:37 am, stevedegrace  wrote:
> >> I developed a custom tag to look up the country of a certain IP
> >> address using an IP to country database. It's sort of rough and ready,
> >> but it should work. The idea is that you read the comment.ip_address,
> >> feed it to the {% country %} tag, which then spits out the two letter
> >> country code. The interesting thing is that the tag works once and
> >> only once per page load if it has to render a variable. All subsequent
> >> calls raise template.VariableDoesNotExist in the code below.
>
> >> The interesting thing is that if you strip out the ability to
> >> recognize an IP as a string and make it just render variables, and you
> >> bind self.ip as a Variable object in the __init__ method and then try
> >> to call its render method in the CountryNode's render method, it
> >> actually raises AttributeError and claims that Variable object has no
> >> attribute render. Weird! FYI, I'm using Django 1.2. Anyone have any
> >> thoughts about what the heck is going on?
>
> > Avoid rebinding self.ip in the render method and it should work just
> > fine.
>
> >> Here's the Python code:
> > (snip imports)
> >> class CountryNode(template.Node):
> >>     def __init__(self, ip):
> >>         self.ip = ip
>
> >>     def render(self, context):
> >>         if self.ip[0] in '\'"':
> >>             self.ip = self.ip.strip('\'"')
>
> > don't rebind self.ip here.
>
> >>         else:
> >>             try:
> >>                 self.ip = Variable(self.ip).resolve(context)
>
> > don't rebind it here neither
>
> > You want to work on local variable, definitly.
>
> > HTH
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Django users" group.
> > To post to this group, send email to django-users@googlegroups.com.
> > To unsubscribe from this group, send email to 
> > django-users+unsubscr...@googlegroups.com.
> > For more options, visit this group 
> > athttp://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Weird problem with rendering a Variable in a custom tag

2011-05-24 Thread stevedegrace
And you are absolutely correct, using a local variable in the render
method and not rebinding self,ip resolves the problem. Thank you very
much. Can you explain why this is?

On May 23, 5:34 am, bruno desthuilliers
 wrote:
> On May 23, 5:37 am, stevedegrace  wrote:
>
> > I developed a custom tag to look up the country of a certain IP
> > address using an IP to country database. It's sort of rough and ready,
> > but it should work. The idea is that you read the comment.ip_address,
> > feed it to the {% country %} tag, which then spits out the two letter
> > country code. The interesting thing is that the tag works once and
> > only once per page load if it has to render a variable. All subsequent
> > calls raise template.VariableDoesNotExist in the code below.
>
> > The interesting thing is that if you strip out the ability to
> > recognize an IP as a string and make it just render variables, and you
> > bind self.ip as a Variable object in the __init__ method and then try
> > to call its render method in the CountryNode's render method, it
> > actually raises AttributeError and claims that Variable object has no
> > attribute render. Weird! FYI, I'm using Django 1.2. Anyone have any
> > thoughts about what the heck is going on?
>
> Avoid rebinding self.ip in the render method and it should work just
> fine.
>
>
>
> > Here's the Python code:
> (snip imports)
> > class CountryNode(template.Node):
> >     def __init__(self, ip):
> >         self.ip = ip
>
> >     def render(self, context):
> >         if self.ip[0] in '\'"':
> >             self.ip = self.ip.strip('\'"')
>
> don't rebind self.ip here.
>
> >         else:
> >             try:
> >                 self.ip = Variable(self.ip).resolve(context)
>
> don't rebind it here neither
>
> You want to work on local variable, definitly.
>
> HTH

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Weird problem with rendering a Variable in a custom tag

2011-05-25 Thread bruno desthuilliers
On May 25, 4:43 am, stevedegrace  wrote:
> And you are absolutely correct, using a local variable in the render
> method and not rebinding self,ip resolves the problem. Thank you very
> much. Can you explain why this is?

"Once a node is parsed, its render method may be called any number of
times"
(http://docs.djangoproject.com/en/1.3/howto/custom-template-tags/
#thread-safety-considerations)

If you wan to make sure, just add a couple print statements (or better
use the logger) in the __init__ and render methods.

HTH

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.