def _parse_img(bits, legacy=True): if len(bits) < 2 or bits[-2] != 'as': raise template.TemplateSyntaxError, "{% img FORMAT for VAR as VAR_NAME %} or {% img FORMAT with FIELD VALUE as VAR_NAME %}" try: format = Format.objects.get_for_name(bits[1]) except Format.DoesNotExist: logmsg = "Format with name %r does not exist (for site id %d)" % (bits[1], settings.SITE_ID) log.error(logmsg) if not settings.TEMPLATE_DEBUG: return template.Node() raise template.TemplateSyntaxError(logmsg) if len(bits) == 6: # img FORMAT for VAR_NAME if bits[2] != 'for': raise template.TemplateSyntaxError, "{% img FORMAT for VAR as VAR_NAME %}" formated_photo = bits[3] elif len(bits) == 7: # img FORMAT with FIELD VALUE if bits[2] != 'with': raise template.TemplateSyntaxError, "{% img FORMAT with FIELD VALUE as VAR_NAME %}" try: photo = get_cached_object(Photo, **{str(bits[3]) : bits[4]}) except photo.DoesNotExist: raise template.TemplateSyntaxError, "Photo with %r of %r does not exist" % (bits[3], bits[4]) formated_photo = FormatedPhoto.objects.get_photo_in_format(photo, format) else: raise template.TemplateSyntaxError, "{% img FORMAT for VAR as VAR_NAME %} or {% img FORMAT with FIELD VALUE as VAR_NAME %}" return ImgTag(formated_photo, format, bits[-1])
def static_map_profile(parser, tokens): """ Displays a static map for a CC3Profile object: {% static_map_profile cc3_profile %} """ def render(ctx): args = tokens.contents.split() if len(args) < 2: return "" cc3_profile = var(args[1], ctx) if cc3_profile: if cc3_profile.latitude and cc3_profile.longitude: loc = "{0},{1}".format(cc3_profile.latitude, cc3_profile.longitude) return render_static_map(loc, "color:red|" + loc) else: return static_map_fn(ctx, cc3_profile.street, cc3_profile.city, cc3_profile.num_street, cc3_profile.country.name) res = template.Node() setattr(res, 'render', render) return res
def static_map(parser, tokens): def render(ctx): (street, city, number, country) = full_address_fn(ctx, *(tokens.contents.split()[1:])) return static_map_fn(ctx, street, city, number, country) res = template.Node() setattr(res, 'render', render) return res
def load_i18n_tag(parser, token): if token.contents.split()[1] == 'i18n': original_library = template.import_library('django.templatetags.i18n') library = template.Library() library.tags['trans'] = lineno_tag(original_library.tags['trans']) library.tags['blocktrans'] = lineno_tag( original_library.tags['blocktrans']) parser.add_library(library) return template.Node()
def img(parser, token): """ Examples: {% img FORMAT for VAR as VAR_NAME %} {% img FORMAT with FIELD VALUE as VAR_NAME %} """ bits = token.split_contents() if len(bits) < 2 or bits[-2] != 'as': raise template.TemplateSyntaxError, "{% img FORMAT for VAR as VAR_NAME %} or {% img FORMAT with FIELD VALUE as VAR_NAME %}" try: format = get_cached_object(Format, name=bits[1], sites__id=settings.SITE_ID) except Format.DoesNotExist: logmsg = "Format with name %r does not exist (for site id %d)" % ( bits[1], settings.SITE_ID) log.error(logmsg) if not settings.TEMPLATE_DEBUG: return template.Node() raise template.TemplateSyntaxError(logmsg) if len(bits) == 6: # img FORMAT for VAR_NAME if bits[2] != 'for': raise template.TemplateSyntaxError, "{% img FORMAT for VAR as VAR_NAME %}" formated_photo = bits[3] elif len(bits) == 7: # img FORMAT with FIELD VALUE if bits[2] != 'with': raise template.TemplateSyntaxError, "{% img FORMAT with FIELD VALUE as VAR_NAME %}" try: photo = get_cached_object(Photo, **{str(bits[3]): bits[4]}) except photo.DoesNotExist: raise template.TemplateSyntaxError, "Photo with %r of %r does not exist" % ( bits[3], bits[4]) try: formated_photo = get_cached_object(FormatedPhoto, photo=photo, format=format) except FormatedPhoto.DoesNotExist: formated_photo = FormatedPhoto.objects.create(photo=photo, format=format) else: raise template.TemplateSyntaxError, "{% img FORMAT for VAR as VAR_NAME %} or {% img FORMAT with FIELD VALUE as VAR_NAME %}" return ImgTag(formated_photo, format, bits[-1])
def full_address(parser, tokens): """ Returns the address of the user in a form suitable for Google Maps: {% full_address street city num_street country %} """ def render(ctx): (street, city, number, country) = full_address_fn(ctx, *(tokens.contents.split()[1:])) return "{0} {1}, {2}, {3}".format(number, street, city, country) res = template.Node() setattr(res, 'render', render) return res
def _parse_image(bits): if len(bits) != 6 or bits[2] != 'in' or bits[4] != 'as': raise template.TemplateSyntaxError('{% image <photo_variable> in "format" as foobar %}') format = template.Variable(bits[3]) if format.literal is not None: try: format = Format.objects.get_for_name(format.literal) except Format.DoesNotExist: logmsg = "Format with name %r does not exist (for site id %d)" % (format.literal, settings.SITE_ID) log.error(logmsg) if not settings.TEMPLATE_DEBUG: return template.Node() raise template.TemplateSyntaxError(logmsg) return ImageTag(format, bits[1], bits[5])
def do_include_ifapp(parser, token): """ Loads a template and renders it with the current context if the specified application is in settings.INSTALLED_APPS. Example:: {% includeifapp app_label "foo/some_include" %} """ bits = token.split_contents() if len(bits) != 3: raise TemplateSyntaxError, "%r tag takes two argument: the application label and the name of the template to be included" % bits[0] app_name, path = bits[1:] app_name = app_name.strip('"\'') try: models = get_app(app_name) except ImproperlyConfigured: return template.Node() if path[0] in ('"', "'") and path[-1] == path[0]: return ConstantIncludeNode(path[1:-1]) return IncludeNode(path)
def site_title(parser, token): """Register a template tag called site_title which returns SITE_TITLE""" node = template.Node() node.render = lambda context: settings.SITE_TITLE return node
def sentry_public_dsn(parser, token): return template.Node()
def dummy_tag(parser, token): return template.Node()
def csrf_token(parser, token): return template.Node()