Django DRY 源

发布于 2024-09-01 08:08:30 字数 705 浏览 9 评论 0 原文

我正在使用 Django Feeds Framework,它非常好、非常直观且易于使用。但是,我认为在 HTML 中创建提要链接时存在问题。

例如:

<link rel="alternate" type="application/rss+xml" title="{{ feed_title }}" href="{{ url_of_feed }}" />

链接的 HREF 属性可以很容易地找到,只需使用 reverse()

但是,TITLE 属性呢?模板引擎应该在哪里寻找这个?更重要的是,如果 feed 是动态构建的并且标题取决于参数(例如 这个)?

我无法想出一个对我来说“似乎”干燥的解决方案...我能想到的就是使用上下文处理器或模板标签,但是当上下文处理器/模板标签必须找到参数时,它会变得混乱构造 Feed 类,并在编写此内容时我意识到我什至不知道如何在视图中自己创建 Feed 实例。

如果我把所有这些逻辑都放在视图中,它就不仅仅是一个视图。此外,TITLE 的值将位于视图和提要中。

I'm using the Django Feeds Framework and it's really nice, very intuitive and easy to use. But, I think there is a problem when creating links to feeds in HTML.

For example:

<link rel="alternate" type="application/rss+xml" title="{{ feed_title }}" href="{{ url_of_feed }}" />

Link's HREF attribute can be easily found out, just use reverse()

But, what about the TITLE attribute? Where the template engine should look for this? Even more, what if the feed is build up dinamically and the title depends on parameters (like this)?

I can't come up with a solution that "seems" DRY to me... All that I can come up with is using context processors o template tags, but it gets messy when the context procesor/template tag has to find parameters to construct the Feed class, and writing this I realize I don't even know how to create a Feed instance myself within the view.

If I put all this logic in the view, it would not be just one view. Also, the value for TITLE would be in the view AND in the feed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七婞 2024-09-08 08:08:30

只是一个猜测(因为我还没有在我的 django 应用程序中使用 feeds),但是您可以使用 feed 对象为 feed 添加一个特殊的 template_context ,并在您的 base.html 中使用它。

Just a guess (as I have not used feeds yet in my django app), but you could add a special template_context for your feed with your feed object and use it in your base.html.

浴红衣 2024-09-08 08:08:30

我对这个解决方案并不完全满意,它可能会破坏使用 Request 的提要,并且依赖于一种神奇的方法。事情是这样的:

#coding:utf-8
# Author: Armando Pérez Marqués <[email protected]>
# Purpose: Django TemplateTag to output feed links in templates in a DRY way
# Created: 05/07/2010

import re

from django import template
from django.conf import settings
from django.contrib.syndication.views import Feed
from django.core.urlresolvers import reverse, resolve, NoReverseMatch
from django.template import Node
from django.template import TemplateSyntaxError
from django.utils.encoding import smart_str
from django.utils.html import escape as html_escape
from django.utils.safestring import mark_safe

register = template.Library()

kwarg_re = re.compile(r"(?:(\w+)=)?(.+)")

class FeedInfoNode(Node):
    def __init__(self, view_name, args, kwargs, asvar):
        self.view_name = view_name
        self.args = args
        self.kwargs = kwargs
        self.asvar = asvar

    def render(self, context):
        args = [arg.resolve(context) for arg in self.args]
        kwargs = dict([(smart_str(k,'ascii'), v.resolve(context))
                       for k, v in self.kwargs.items()])

        # Try to look up the URL twice: once given the view name, and again
        # relative to what we guess is the "main" app. If they both fail,
        # re-raise the NoReverseMatch unless we're using the
        # {% feed_info ... as var %} construct in which cause return nothing.
        url = ''
        try:
            url = reverse(self.view_name, args=args, kwargs=kwargs, current_app=context.current_app)
        except NoReverseMatch, e:
            if settings.SETTINGS_MODULE:
                project_name = settings.SETTINGS_MODULE.split('.')[0]
                try:
                    url = reverse(project_name + '.' + self.view_name,
                              args=args, kwargs=kwargs, current_app=context.current_app)
                except NoReverseMatch:
                    if self.asvar is None:
                        # Re-raise the original exception, not the one with
                        # the path relative to the project. This makes a
                        # better error message.
                        raise e
            else:
                if self.asvar is None:
                    raise e

        if 'request' in context:
            request = context['request']
        else:
            request = None

        feed_instance, feed_args, feed_kwargs = resolve(url)
        if not isinstance(feed_instance, Feed):
            raise NoReverseMatch, \
                  'feed_info can only reverse class-based feeds'

        feed_obj = feed_instance.get_object(request, *feed_args, **feed_kwargs)

        feed_data = {
            'url': url,
            'obj': feed_instance,
            'args': feed_args,
            'kwargs': feed_kwargs,
            #'title': html_escape(feed_instance.__get_dynamic_attr('title', obj)),
            'title': html_escape(
                feed_instance._Feed__get_dynamic_attr('title', feed_obj)
                ),
            'type': feed_instance.feed_type.mime_type,
        }

        if self.asvar:
            context[self.asvar] = feed_data
            return ''
        else:
            return mark_safe(
                '<link rel="alternate" type="%(type)s" title="%(title)s" href="%(url)s" />' \
                % feed_data
            )

def feed_info(parser, token):
    """
    Returns an mapping containing populated info about the reversed feed
    Works exactly as the url tag, but the mapping is not returned, instead
    a variable is always set  in the context.

    This is a way to define links that aren't tied to a particular URL
    configuration::

        {% feed_info path.to.some_feed_view_class arg1 arg2 as feed_info_var %}

        or

        {% feed_info path.to.some_feed_view_class name1=value1 name2=value2 as feed_info_var %}
    """

    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a feed view)" % bits[0])
    viewname = bits[1]
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    # Backwards compatibility: check for the old comma separated format
    # {% url urlname arg1,arg2 %}
    # Initial check - that the first space separated bit has a comma in it
    if bits and ',' in bits[0]:
        check_old_format = True
        # In order to *really* be old format, there must be a comma
        # in *every* space separated bit, except the last.
        for bit in bits[1:-1]:
            if ',' not in bit:
                # No comma in this bit. Either the comma we found
                # in bit 1 was a false positive (e.g., comma in a string),
                # or there is a syntax problem with missing commas
                check_old_format = False
                break
    else:
        # No comma found - must be new format.
        check_old_format = False

    if check_old_format:
        # Confirm that this is old format by trying to parse the first
        # argument. An exception will be raised if the comma is
        # unexpected (i.e. outside of a static string).
        match = kwarg_re.match(bits[0])
        if match:
            value = match.groups()[1]
            try:
                parser.compile_filter(value)
            except TemplateSyntaxError:
                bits = ''.join(bits).split(',')

    # Now all the bits are parsed into new format,
    # process them as template vars
    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return FeedInfoNode(viewname, args, kwargs, asvar)

feed_info = register.tag(feed_info)

我从 {% url %} 模板标记的代码开始,然后在获取 feed 的 URL 后,使用 resolve() 来获取Feed子类实例,然后获取需要的属性。

注意事项

  • 需要 Django 1.2 类提要,不知道如何使用旧的提要方式执行此操作。
  • 如果 feed 类使用 request 对象,则 request 上下文处理器,因为如果上下文中不存在,则传递 None
  • Feed.__get_dynamic_attr() 有一个奇怪的地方。 Feed子类实例没有这个方法;相反,它以另一个名称出现。不知道如何在运行时计算出名称......

I'm not fully satisfied with this solution, it may break feeds using Request and depends on a magic method. There it goes:

#coding:utf-8
# Author: Armando Pérez Marqués <[email protected]>
# Purpose: Django TemplateTag to output feed links in templates in a DRY way
# Created: 05/07/2010

import re

from django import template
from django.conf import settings
from django.contrib.syndication.views import Feed
from django.core.urlresolvers import reverse, resolve, NoReverseMatch
from django.template import Node
from django.template import TemplateSyntaxError
from django.utils.encoding import smart_str
from django.utils.html import escape as html_escape
from django.utils.safestring import mark_safe

register = template.Library()

kwarg_re = re.compile(r"(?:(\w+)=)?(.+)")

class FeedInfoNode(Node):
    def __init__(self, view_name, args, kwargs, asvar):
        self.view_name = view_name
        self.args = args
        self.kwargs = kwargs
        self.asvar = asvar

    def render(self, context):
        args = [arg.resolve(context) for arg in self.args]
        kwargs = dict([(smart_str(k,'ascii'), v.resolve(context))
                       for k, v in self.kwargs.items()])

        # Try to look up the URL twice: once given the view name, and again
        # relative to what we guess is the "main" app. If they both fail,
        # re-raise the NoReverseMatch unless we're using the
        # {% feed_info ... as var %} construct in which cause return nothing.
        url = ''
        try:
            url = reverse(self.view_name, args=args, kwargs=kwargs, current_app=context.current_app)
        except NoReverseMatch, e:
            if settings.SETTINGS_MODULE:
                project_name = settings.SETTINGS_MODULE.split('.')[0]
                try:
                    url = reverse(project_name + '.' + self.view_name,
                              args=args, kwargs=kwargs, current_app=context.current_app)
                except NoReverseMatch:
                    if self.asvar is None:
                        # Re-raise the original exception, not the one with
                        # the path relative to the project. This makes a
                        # better error message.
                        raise e
            else:
                if self.asvar is None:
                    raise e

        if 'request' in context:
            request = context['request']
        else:
            request = None

        feed_instance, feed_args, feed_kwargs = resolve(url)
        if not isinstance(feed_instance, Feed):
            raise NoReverseMatch, \
                  'feed_info can only reverse class-based feeds'

        feed_obj = feed_instance.get_object(request, *feed_args, **feed_kwargs)

        feed_data = {
            'url': url,
            'obj': feed_instance,
            'args': feed_args,
            'kwargs': feed_kwargs,
            #'title': html_escape(feed_instance.__get_dynamic_attr('title', obj)),
            'title': html_escape(
                feed_instance._Feed__get_dynamic_attr('title', feed_obj)
                ),
            'type': feed_instance.feed_type.mime_type,
        }

        if self.asvar:
            context[self.asvar] = feed_data
            return ''
        else:
            return mark_safe(
                '<link rel="alternate" type="%(type)s" title="%(title)s" href="%(url)s" />' \
                % feed_data
            )

def feed_info(parser, token):
    """
    Returns an mapping containing populated info about the reversed feed
    Works exactly as the url tag, but the mapping is not returned, instead
    a variable is always set  in the context.

    This is a way to define links that aren't tied to a particular URL
    configuration::

        {% feed_info path.to.some_feed_view_class arg1 arg2 as feed_info_var %}

        or

        {% feed_info path.to.some_feed_view_class name1=value1 name2=value2 as feed_info_var %}
    """

    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a feed view)" % bits[0])
    viewname = bits[1]
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    # Backwards compatibility: check for the old comma separated format
    # {% url urlname arg1,arg2 %}
    # Initial check - that the first space separated bit has a comma in it
    if bits and ',' in bits[0]:
        check_old_format = True
        # In order to *really* be old format, there must be a comma
        # in *every* space separated bit, except the last.
        for bit in bits[1:-1]:
            if ',' not in bit:
                # No comma in this bit. Either the comma we found
                # in bit 1 was a false positive (e.g., comma in a string),
                # or there is a syntax problem with missing commas
                check_old_format = False
                break
    else:
        # No comma found - must be new format.
        check_old_format = False

    if check_old_format:
        # Confirm that this is old format by trying to parse the first
        # argument. An exception will be raised if the comma is
        # unexpected (i.e. outside of a static string).
        match = kwarg_re.match(bits[0])
        if match:
            value = match.groups()[1]
            try:
                parser.compile_filter(value)
            except TemplateSyntaxError:
                bits = ''.join(bits).split(',')

    # Now all the bits are parsed into new format,
    # process them as template vars
    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return FeedInfoNode(viewname, args, kwargs, asvar)

feed_info = register.tag(feed_info)

I'm starting with the code of the {% url %} template tag, and then, after obtaining the feed's URL, use resolve() to get the Feed subclass instance, and then get the needed attributes.

Caveats

  • Requires Django 1.2 Class Feeds, don't know exactly how to do this with the old way of feeds.
  • If the feed class uses the request object, the request context processor must be configured, since None is passed if it isn't present in the context.
  • There's an oddity with Feed.__get_dynamic_attr(). The Feed subclass instance doesn't have this method; instead, it appears with another name. Don't know how to figure the name out at runtime...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文