rendered paste bodyfrom django import template
from django.template import loader
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.contenttypes.models import ContentType
register = template.Library()
def show_rating(app, obj_id):
app = token.contents.split('.')
if app.length != 2:
raise template.TemplateSyntaxError, "rating tag has invalid content-type '%s'" % app
content_type = ContentType.objects.get(app_label__exact=app[0], model__exact=app[1])
except ContentType.DoesNotExist:
raise template.TemplateSyntaxError, "rating tag has invalid content-type '%s'" % app
try: # ensure the object ID is valid
content_type.get_object_for_this_type(pk=obj_id)
except ObjectDoesNotExist:
raise template.TemplateSyntaxError, "rating tag refers to %s object with ID %s, which doesn't exist" % (content_type.name, obj_id)
context.push()
context['target'] = '%s:%s' % (self.content_type.id, self.obj_id)
options = []
for var, abbr in (('rating_choices', RATING_CHOICES),):
context[var] = getattr(self, var)
if getattr(self, var):
options.append(abbr)
context['options'] = ','.join(options)
return context
register.inclusion_tag('ratings/display.html')(show_rating)