X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/72849b1c135de8c0ad14d2ca881a98fdea279859..3c26965045e8881acd4d5de8c195e4aa61b95ed1:/templatetags/embed.py diff --git a/templatetags/embed.py b/templatetags/embed.py index 901e163..eb4cd68 100644 --- a/templatetags/embed.py +++ b/templatetags/embed.py @@ -65,6 +65,9 @@ def get_embed_dict(embed_list, context): embeds = {} for e in embed_list: ct = e.get_content_type(context) + if ct is None: + # Then the embed doesn't exist for this context. + continue if ct not in embeds: embeds[ct] = [e] else: @@ -143,7 +146,6 @@ class ConstantEmbedNode(template.Node): self.template = None def compile_instance(self, object_pk): - self.object_pk = object_pk model = self.content_type.model_class() try: return model.objects.get(pk=object_pk) @@ -207,17 +209,13 @@ class ConstantEmbedNode(template.Node): try: t = context.render_context[EMBED_CONTEXT_KEY].get_embed_template(self, context) except (KeyError, IndexError): - if settings.TEMPLATE_DEBUG: - raise self.mark_rendered_for(context) return settings.TEMPLATE_STRING_IF_INVALID context.push() context['embedded'] = instance - kwargs = {} for k, v in self.kwargs.items(): - kwargs[k] = v.resolve(context) - context.update(kwargs) + context[k] = v.resolve(context) t_rendered = t.render(context) context.pop() self.mark_rendered_for(context) @@ -263,7 +261,10 @@ class InstanceEmbedNode(EmbedNode): return self.instance.resolve(context) def get_content_type(self, context): - return ContentType.objects.get_for_model(self.get_instance(context)) + instance = self.get_instance(context) + if not instance: + return None + return ContentType.objects.get_for_model(instance) def get_embedded(self): @@ -273,15 +274,15 @@ def get_embedded(self): setattr(ConstantEmbedNode, LOADED_TEMPLATE_ATTR, property(get_embedded)) -def get_content_type(bit): +def parse_content_type(bit, tagname): try: app_label, model = bit.split('.') except ValueError: - raise template.TemplateSyntaxError('"%s" template tag expects the first argument to be of the form app_label.model' % tag) + raise template.TemplateSyntaxError('"%s" template tag expects the first argument to be of the form app_label.model' % tagname) try: ct = ContentType.objects.get(app_label=app_label, model=model) except ContentType.DoesNotExist: - raise template.TemplateSyntaxError('"%s" template tag requires an argument of the form app_label.model which refers to an installed content type (see django.contrib.contenttypes)' % tag) + raise template.TemplateSyntaxError('"%s" template tag requires an argument of the form app_label.model which refers to an installed content type (see django.contrib.contenttypes)' % tagname) return ct @@ -298,7 +299,7 @@ def do_embed(parser, token): raise template.TemplateSyntaxError('"%s" template tag must have at least two arguments.' % tag) if len(bits) == 3 and bits[-2] == 'with': - ct = get_content_type(bits[0]) + ct = parse_content_type(bits[0], tag) if bits[2][0] in ['"', "'"] and bits[2][0] == bits[2][-1]: return ConstantEmbedNode(ct, template_name=bits[2]) @@ -321,7 +322,7 @@ def do_embed(parser, token): return InstanceEmbedNode(instance, kwargs) elif len(bits) > 2: raise template.TemplateSyntaxError('"%s" template tag expects at most 2 non-keyword arguments when embedding instances.') - ct = get_content_type(bits[0]) + ct = parse_content_type(bits[0], tag) pk = bits[1] try: