Source

django-lastfm / templatetags / lastfm_widget.py

Full commit
# encoding: utf-8

"""
Displays your recently listend tracks from last.fm.

    {% load lastfm_widget %}
    
    {% get_lastfm_widget as lastfm_widget %}
"""


from django import template
from django.conf import settings


register = template.Library()


class LastfmWidgetNode(template.Node):
    """Renders the weblog’s sidebar widgets."""
    def __init__(self, var_name):
        self.var_name = var_name
        # self.chart_types = {
        #     'recent_tracks': self._get_recent_tracks,
        #     'weekly_top_artists': self._get_weekly_top_artists,
        #     'top_artists': self._get_top_artists,
        # }
        # self.url = 'http://ws.audioscrobbler.com/2.0/?'
        
    def render(self, context):
        # try:
            # data = self.chart_types[settings.LASTFM_CHART_TYPE]()
        # except:
        #     data = []
        
        t = template.loader.get_template('lastfm_widget/_widget.html')
        lastfm_widget = {
            'title': settings.LASTFM_WIDGET_TITLE,
            'content': t.render(template.Context({},
                    autoescape=context.autoescape)),
        }
        
        context[self.var_name] = lastfm_widget
        return ''


@register.tag
def get_lastfm_widget(parser, token):
    """Get the last.fm widget and store it in a template variable."""
    try:
        tagname, _as, var_name = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError('%r tag requires two arguments.' % 
                token.contents.split()[0])
    return LastfmWidgetNode(var_name)