I hope I'm using the correct terminology here...

As anyone who's read my posts knows, I'm new to both Django and Python,
but I'm trying to figure stuff out. In my last post, I mentioned I
wanted a way to bring in things from Amazon Web Services, and it was
suggested I should use templatetags.

Well, it was harder than I thought it was going to be, but I've got it
working. My problem is manipulating what I return. Currently, I'm
returning EVERYTHING.  I'm using xmltramp, so I can drill down through
the xml and return what I want, but right now I'm forcing the template
to do way more of the sorting and filtering than I want.  In other
words, I want something like
Amazon.objects.all().filter(title).order_by('?')

As it is now, my template's jumping through hoops like this:
{% load amazon.amazon %}
{% get_amazon_book_list as amazon_book_list %}
<dl>
    {% for book in amazon_book_list %}
        {% ifequal book.ItemAttributes.Format HTML %}
            {% if book.ItemAttributes.Title %}
               <dt>{{ book.ItemAttributes.Title}}" /></dt>
              <dd>{{ book.EditorialReviews.EditorialReview.Content
}}</dd>
           {% endif %}
       {% endifequal %}
   {% endfor %}
</dl>

And that just seems silly to me. I KNOW there's a better way.

Here's my template tag:

from django import template
import datetime
import urllib
import xmltramp
register = template.Library()

bookfile =
urllib.urlopen("http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=17AFEX4H54748CG47E02&AssociatesId=thegretschpag-20&Operation=ItemSearch&Keywords=gretsch%20guitar&SearchIndex=Books&ResponseGroup=Medium";).read()

bookxml = xmltramp.parse(bookfile)

class AmazonBookListNode(template.Node):
    def __init__(self,varname):
        self.varname = varname
        self.xml = bookxml.Items

    def __repr__(self):
        return "<Amazon Book Node>"

    def render(self, context):
        context[self.varname] = self.xml
        return ''

class GetAmazonBookList:
    """
    {% get_amazon_book_list as amazon_book_list %}
    """
    def __init__(self, tag_name):
        self.tag_name = tag_name

    def __call__(self, parser, token):
        bits = token.contents.split()
        if len(bits) != 3:
            raise template.TemplateSyntaxError, "'%s' tag takes two
arguments" % bits[0]
        if bits[1] != "as":
            raise template.TemplateSyntaxError, "First argument to '%s'
tag must be 'as'" % bits[0]
        return AmazonBookListNode(bits[2])

register.tag('get_amazon_book_list',
GetAmazonBookList("get_amazon_book_list"))


Thanks!


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to