web/views/xmlrss.py
author sylvain.thenault@logilab.fr
Wed, 13 May 2009 17:00:08 +0200
branchtls-sprint
changeset 1788 d6e6ad70e50a
parent 1733 94c36d679454
child 1882 ce662160bb46
permissions -rw-r--r--
check field isn't already instantiated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
"""base xml and rss views
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
:organization: Logilab
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
"""
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
__docformat__ = "restructuredtext en"
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
from time import timezone
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
from logilab.mtconverter import xml_escape
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
    13
from cubicweb.selectors import non_final_entity, one_line_rset, appobject_selectable
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
from cubicweb.view import EntityView, AnyRsetView
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
from cubicweb.web.httpcache import MaxAgeHTTPCacheManager
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
    16
from cubicweb.web.component import Component
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
    17
from cubicweb.web.box import BoxTemplate
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
from cubicweb.common.uilib import simple_sgml_tag
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
    20
_ = unicode
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
    21
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
    22
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
    23
# base xml views ##############################################################
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    24
1652
ba0a6b4d04f7 more intuitive case
sylvain.thenault@logilab.fr
parents: 1550
diff changeset
    25
class XMLView(EntityView):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    26
    """xml view for entities"""
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
    id = 'xml'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    28
    title = _('xml')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    29
    templatable = False
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    30
    content_type = 'text/xml'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    31
    xml_root = 'rset'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
    item_vid = 'xmlitem'
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
    33
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    34
    def cell_call(self, row, col):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
        self.wview(self.item_vid, self.rset, row=row, col=col)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
    36
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
    def call(self):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    38
        """display a list of entities by calling their <item_vid> view"""
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    39
        self.w(u'<?xml version="1.0" encoding="%s"?>\n' % self.req.encoding)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    40
        self.w(u'<%s size="%s">\n' % (self.xml_root, len(self.rset)))
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    41
        for i in xrange(self.rset.rowcount):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    42
            self.cell_call(i, 0)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    43
        self.w(u'</%s>\n' % self.xml_root)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    44
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
1652
ba0a6b4d04f7 more intuitive case
sylvain.thenault@logilab.fr
parents: 1550
diff changeset
    46
class XMLItemView(EntityView):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    47
    id = 'xmlitem'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    48
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
    def cell_call(self, row, col):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
        """ element as an item for an xml feed """
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    51
        entity = self.complete_entity(row, col)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    52
        self.w(u'<%s>\n' % (entity.e_schema))
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    53
        for rschema, attrschema in entity.e_schema.attribute_definitions():
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    54
            attr = rschema.type
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    55
            try:
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    56
                value = entity[attr]
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    57
            except KeyError:
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    58
                # Bytes
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    59
                continue
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    60
            if value is not None:
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    61
                if attrschema == 'Bytes':
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    62
                    from base64 import b64encode
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    63
                    value = '<![CDATA[%s]]>' % b64encode(value.getvalue())
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    64
                elif isinstance(value, basestring):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    65
                    value = xml_escape(value)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    66
                self.w(u'  <%s>%s</%s>\n' % (attr, value, attr))
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    67
        self.w(u'</%s>\n' % (entity.e_schema))
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    68
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
    69
1652
ba0a6b4d04f7 more intuitive case
sylvain.thenault@logilab.fr
parents: 1550
diff changeset
    70
class XMLRsetView(AnyRsetView):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    71
    """dumps raw rset as xml"""
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    72
    id = 'rsetxml'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    73
    title = _('xml export')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    74
    templatable = False
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
    content_type = 'text/xml'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    76
    xml_root = 'rset'
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
    77
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    78
    def call(self):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
        w = self.w
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
        rset, descr = self.rset, self.rset.description
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    81
        eschema = self.schema.eschema
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    82
        labels = self.columns_labels(False)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    83
        w(u'<?xml version="1.0" encoding="%s"?>\n' % self.req.encoding)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    84
        w(u'<%s query="%s">\n' % (self.xml_root, xml_escape(rset.printable_rql())))
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    85
        for rowindex, row in enumerate(self.rset):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    86
            w(u' <row>\n')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
            for colindex, val in enumerate(row):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    88
                etype = descr[rowindex][colindex]
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
                tag = labels[colindex]
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    90
                attrs = {}
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
                if '(' in tag:
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    92
                    attrs['expr'] = tag
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    93
                    tag = 'funccall'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    94
                if val is not None and not eschema(etype).is_final():
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    95
                    attrs['eid'] = val
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
                    # csvrow.append(val) # val is eid in that case
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    97
                    val = self.view('textincontext', rset,
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    98
                                    row=rowindex, col=colindex)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    99
                else:
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   100
                    val = self.view('final', rset, displaytime=True,
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   101
                                    row=rowindex, col=colindex, format='text/plain')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   102
                w(simple_sgml_tag(tag, val, **attrs))
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   103
            w(u' </row>\n')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
        w(u'</%s>\n' % self.xml_root)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   105
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   106
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   107
# RSS stuff ###################################################################
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   108
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   109
class RSSFeedURL(Component):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   110
    id = 'rss_feed_url'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   111
    __select__ = non_final_entity()
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   112
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   113
    def feed_url(self):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   114
        return self.build_url(rql=self.limited_rql(), vid='rss')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   115
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   116
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   117
class RSSEntityFeedURL(Component):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   118
    id = 'rss_feed_url'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   119
    __select__ = non_final_entity() & one_line_rset()
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   120
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   121
    def feed_url(self):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   122
        return self.entity(0, 0).rss_feed_url()
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   123
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   124
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   125
class RSSIconBox(BoxTemplate):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   126
    """just display the RSS icon on uniform result set"""
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   127
    id = 'rss'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   128
    __select__ = (BoxTemplate.__select__
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   129
                  & appobject_selectable('components', 'rss_feed_url'))
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   130
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   131
    visible = False
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   132
    order = 999
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   133
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   134
    def call(self, **kwargs):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   135
        try:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   136
            rss = self.req.external_resource('RSS_LOGO')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   137
        except KeyError:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   138
            self.error('missing RSS_LOGO external resource')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   139
            return
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   140
        urlgetter = self.vreg.select_component('rss_feed_url', self.req, self.rset)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   141
        url = urlgetter.feed_url()
1135
00eb43688a57 pylint detected errors
sylvain.thenault@logilab.fr
parents: 1091
diff changeset
   142
        self.w(u'<a href="%s"><img src="%s" alt="rss"/></a>\n' % (xml_escape(url), rss))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   143
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   144
1652
ba0a6b4d04f7 more intuitive case
sylvain.thenault@logilab.fr
parents: 1550
diff changeset
   145
class RSSView(XMLView):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   146
    id = 'rss'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   147
    title = _('rss')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   148
    templatable = False
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   149
    content_type = 'text/xml'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   150
    http_cache_manager = MaxAgeHTTPCacheManager
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   151
    cache_max_age = 60*60*2 # stay in http cache for 2 hours by default
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   152
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   153
    def _open(self):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   154
        req = self.req
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   155
        self.w(u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   156
        self.w(u'<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">\n')
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   157
        self.w(u'  <channel>\n')
1733
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   158
        self.w(u'    <title>%s RSS Feed</title>\n'
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   159
               % xml_escape(self.page_title()))
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   160
        self.w(u'    <description>%s</description>\n'
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   161
               % xml_escape(req.form.get('vtitle', '')))
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   162
        params = req.form.copy()
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   163
        params.pop('vid', None)
1550
8260e857822b use xml_escape
sylvain.thenault@logilab.fr
parents: 1477
diff changeset
   164
        self.w(u'    <link>%s</link>\n' % xml_escape(self.build_url(**params)))
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   165
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   166
    def _close(self):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   167
        self.w(u'  </channel>\n')
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   168
        self.w(u'</rss>')
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   169
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   170
    def call(self):
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   171
        """display a list of entities by calling their <item_vid> view"""
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   172
        self._open()
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   173
        for i in xrange(self.rset.rowcount):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   174
            self.cell_call(i, 0)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   175
        self._close()
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   176
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   177
    def cell_call(self, row, col):
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   178
        self.wview('rssitem', self.rset, row=row, col=col)
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   179
1550
8260e857822b use xml_escape
sylvain.thenault@logilab.fr
parents: 1477
diff changeset
   180
1652
ba0a6b4d04f7 more intuitive case
sylvain.thenault@logilab.fr
parents: 1550
diff changeset
   181
class RSSItemView(EntityView):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   182
    id = 'rssitem'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   183
    date_format = '%%Y-%%m-%%dT%%H:%%M%+03i:00' % (timezone / 3600)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   184
    add_div_section = False
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   185
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   186
    def cell_call(self, row, col):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   187
        entity = self.complete_entity(row, col)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   188
        self.w(u'<item>\n')
1733
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   189
        self.w(u'<guid isPermaLink="true">%s</guid>\n'
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   190
               % xml_escape(entity.absolute_url()))
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   191
        self.render_title_link(entity)
1733
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   192
        self._marker('description', entity.dc_description(format='text/html'))
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   193
        self._marker('dc:date', entity.dc_date(self.date_format))
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   194
        self.render_entity_creator(entity)
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   195
        self.w(u'</item>\n')
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   196
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   197
    def render_title_link(self, entity):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   198
        self._marker('title', entity.dc_long_title())
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   199
        self._marker('link', entity.absolute_url())
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   200
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   201
    def render_entity_creator(self, entity):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   202
        if entity.creator:
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   203
            self._marker('dc:creator', entity.dc_creator())
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   204
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   205
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   206
    def _marker(self, marker, value):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   207
        if value:
1550
8260e857822b use xml_escape
sylvain.thenault@logilab.fr
parents: 1477
diff changeset
   208
            self.w(u'  <%s>%s</%s>\n' % (marker, xml_escape(value), marker))