Hi Malcolm,
Thanks for your patient guidance. I took your advice on a better name
for the variable. Now it should return true if the image_is_vertical.
I am getting a lot closer, but I think I am losing the photo's ID
when I get to the templatetag in photos.py. Here's what debug tells
me:
ProgrammingError at /homepage/dmtest/
ERROR: invalid input syntax for integer:
"homepage.get_piece_dict.lead_story.get_lead_photo" SELECT
"photos"."id","photos"."creation_date","photos"."photographer_id","photos"."one_off_photographer","photos"."credit","photos"."caption","photos"."photo","photos"."width","photos"."height"
FROM "photos" WHERE "photos"."id" =
'homepage.get_piece_dict.lead_story.get_lead_photo'
here's the new code:
class WideOrDeepNode(CachedNode):
def __init__(self, photoobj, varname):
self.photoobj = photoobj
self.varname = varname
photos.get_object(pk=photoobj)
self.cache_timeout = 180
def get_cache_key(self, context):
return "ellington.media.templatetags.photos.do_get_wideordeep:
%s" % (SITE_ID)
def get_content(self,context):
context[self.varname] = self.photoobj.width <=
self.photoobj.height
return ""
def do_get_wideordeep(parser, token):
"""
Checks to see if the photo is horizontal (or square) or vertical.
Will return '1' if it is vertical. Otherwise returns '0'.
Syntax::
{% get_wideordeep object.id [as varname] %}
"""
bits = token.contents.split()
if len(bits) == 2:
photoobj = bits[1]
varname = 'image_is_vertical'
elif len(bits) == 4:
photoobj = bits[1]
varname = bits[3]
else:
raise template.TemplateSyntaxError, "'%s' tag takes either one
or two arguments" % bits[0]
return WideOrDeepNode(photoobj,varname)
Thanks again for your help.
Take care,
David
On Feb 20, 12:18 am, Malcolm Tredinnick <[email protected]>
wrote:
> On Thu, 2009-02-19 at 21:09 -0800, [email protected] wrote:
>
> > I am trying to set up a templatetag that will give me a variable to
> > use
> > in templates so we can modify our presentation of stories depending
> > upon
> > whether the accompanying photo is horizontal or vertical. I am using
> > 0.91 EllingtonCMS.
>
> > I've made a templatetag in /ellington/media/photos.py that looks like
> > this:
>
> This is pretty close to correct. A couple of notes inline below...
>
>
>
>
>
> > class WideOrDeepNode(CachedNode):
> > def __init__(self, photoobj, varname):
> > self.photoobj = photoobj
> > self.varname = varname
> > self.cache_timeout = 180
>
> > def get_cache_key(self, context):
> > return
> > "ellington.media.templatetags.photos.do_get_wideordeep:%s" % (SITE_ID)
>
> > def get_content(self, context):
> > if self.photoobj.width <= self.photoobj.height:
> > context[self.varname] = '0'
> > else:
> > context[self.varname] = '1'
> > return ""
>
> This feels clunkier than it could be. You'd get the same result by
> writing:
>
> def get_content(self, context):
> context[self.varname] = self.photoobj.width > self.photoobj.height
> return ""
>
> Since True == 1 and False == 0 in Python 2.x, you can see it's the same
> result. Writing it as a boolean check looks a bit more natural to me,
> however.
>
> Still, that's a minor point.
>
>
>
>
>
> > def do_get_wideordeep(parser, token):
> > """
> > Checks to see if the photo object's width is greater than its
> > length
> > and will return '0' if it is. Otherwise returns '1'.
>
> > Syntax::
> > {% get_wideordeep object.id [as varname] %}
> > """
> > bits = token.contents.split()
> > if len(bits) == 2:
> > photoobj = bits[1]
> > varname = 'imageflag'
> > elif len(bits) == 4:
> > photoobj = bits[1]
> > varname = bits[3]
>
> When you're setting up photoobj here, it will be string, since it's one
> of the words passed in from the template tag. You need to convert that
> string (the object id) into an object. I would probably do that in the
> __init__ method of the Node class, but wherever you do it is up to you.
> Something like
>
> Photo.objects.get(pk=photoobj)
>
> and be prepared to handle to DoesNotExist exception.
>
> > else:
> > raise template.TemplateSyntaxError, "'%s' tag takes either one
> > or two arguments" % bits[0]
> > return WideOrDeepNode(photoobj,varname)
>
> > on the template, here is how I am asking for it:
>
> > {% get_wideordeep homepage.get_piece_dict.lead_story.get_lead_photo
> > as
> > imageflag %}
> > {% ifequal imageflag 0 %}
>
> Since imageflag is True or False (or 1 or 0), {% if imageflag %} can be
> used here (although you'll need to reverse the order of your blocks,
> since the True case will now be first.
>
> That also raises one final little concern I would have with this: the
> name. The template tag doesn't return "wide" or "deep". It returns a
> boolean. So in 6 months, are you (or the next guy) going to be able to
> remember what True means here?
>
> If you called it "image_is_wider_than_deep" then it's clear what True
> should mean here. There are no doubt other similarly self-descriptive
> names like that you could choose. My point is, pick a name that helps
> describe the results. Be nice to yourself and your template authors.
>
> Regards,
> Malcolm
> =
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---