web/views/xmlrss.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 26 Aug 2009 10:18:07 +0200
branchstable
changeset 3009 3deb0fa95761
parent 2650 18aec79ec3a3
child 2789 39712da6f397
child 3165 0a50ebe53db7
permissions -rw-r--r--
no comment
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
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1882
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
824
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
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1882
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
"""
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
__docformat__ = "restructuredtext en"
1882
ce662160bb46 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1733
diff changeset
     9
_ = unicode
824
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 time import timezone
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    13
from logilab.mtconverter import xml_escape
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
    15
from cubicweb.selectors import non_final_entity, one_line_rset, appobject_selectable
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    16
from cubicweb.view import EntityView, AnyRsetView, Component
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
from cubicweb.common.uilib import simple_sgml_tag
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    18
from cubicweb.web import httpcache, box
824
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
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
    21
# base xml views ##############################################################
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
1652
ba0a6b4d04f7 more intuitive case
sylvain.thenault@logilab.fr
parents: 1550
diff changeset
    23
class XMLView(EntityView):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    24
    """xml view for entities"""
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    25
    id = 'xml'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    26
    title = _('xml')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
    templatable = False
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    28
    content_type = 'text/xml'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    29
    xml_root = 'rset'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    30
    item_vid = 'xmlitem'
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
    31
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
    def cell_call(self, row, col):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    33
        self.wview(self.item_vid, self.rset, row=row, col=col)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
    34
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
    def call(self):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    36
        """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
    37
        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
    38
        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
    39
        for i in xrange(self.rset.rowcount):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    40
            self.cell_call(i, 0)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    41
        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
    42
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    43
1652
ba0a6b4d04f7 more intuitive case
sylvain.thenault@logilab.fr
parents: 1550
diff changeset
    44
class XMLItemView(EntityView):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
    id = 'xmlitem'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    47
    def cell_call(self, row, col):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    48
        """ 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
    49
        entity = self.complete_entity(row, col)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
        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
    51
        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
    52
            attr = rschema.type
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    53
            try:
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    54
                value = entity[attr]
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    55
            except KeyError:
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    56
                # Bytes
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    57
                continue
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    58
            if value is not None:
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    59
                if attrschema == 'Bytes':
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    60
                    from base64 import b64encode
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    61
                    value = '<![CDATA[%s]]>' % b64encode(value.getvalue())
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    62
                elif isinstance(value, basestring):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    63
                    value = xml_escape(value)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    64
                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
    65
        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
    66
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
    67
1652
ba0a6b4d04f7 more intuitive case
sylvain.thenault@logilab.fr
parents: 1550
diff changeset
    68
class XMLRsetView(AnyRsetView):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    69
    """dumps raw rset as xml"""
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    70
    id = 'rsetxml'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    71
    title = _('xml export')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    72
    templatable = False
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    73
    content_type = 'text/xml'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    74
    xml_root = 'rset'
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
    75
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    76
    def call(self):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    77
        w = self.w
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    78
        rset, descr = self.rset, self.rset.description
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
        eschema = self.schema.eschema
2205
fc1447fe94df tr is now the second argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    80
        labels = self.columns_labels(tr=False)
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    81
        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
    82
        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
    83
        for rowindex, row in enumerate(self.rset):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    84
            w(u' <row>\n')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    85
            for colindex, val in enumerate(row):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    86
                etype = descr[rowindex][colindex]
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
                tag = labels[colindex]
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    88
                attrs = {}
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
                if '(' in tag:
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    90
                    attrs['expr'] = tag
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
                    tag = 'funccall'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    92
                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
    93
                    attrs['eid'] = val
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    94
                    # 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
    95
                    val = self.view('textincontext', rset,
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
                                    row=rowindex, col=colindex)
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    97
                else:
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    98
                    val = self.view('final', rset, displaytime=True,
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
    99
                                    row=rowindex, col=colindex, format='text/plain')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   100
                w(simple_sgml_tag(tag, val, **attrs))
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   101
            w(u' </row>\n')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   102
        w(u'</%s>\n' % self.xml_root)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   103
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   104
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   105
# RSS stuff ###################################################################
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   106
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   107
class RSSFeedURL(Component):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   108
    id = 'rss_feed_url'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   109
    __select__ = non_final_entity()
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   110
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   111
    def feed_url(self):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   112
        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
   113
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   114
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   115
class RSSEntityFeedURL(Component):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   116
    id = 'rss_feed_url'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   117
    __select__ = non_final_entity() & one_line_rset()
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   118
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   119
    def feed_url(self):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   120
        return self.entity(0, 0).rss_feed_url()
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   121
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   122
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   123
class RSSIconBox(box.BoxTemplate):
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   124
    """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
   125
    id = 'rss'
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   126
    __select__ = (box.BoxTemplate.__select__
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   127
                  & appobject_selectable('components', 'rss_feed_url'))
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   128
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   129
    visible = False
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   130
    order = 999
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   131
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   132
    def call(self, **kwargs):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   133
        try:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   134
            rss = self.req.external_resource('RSS_LOGO')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   135
        except KeyError:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   136
            self.error('missing RSS_LOGO external resource')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   137
            return
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
   138
        urlgetter = self.vreg['components'].select('rss_feed_url', self.req,
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
   139
                                                   rset=self.rset)
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 824
diff changeset
   140
        url = urlgetter.feed_url()
1135
00eb43688a57 pylint detected errors
sylvain.thenault@logilab.fr
parents: 1091
diff changeset
   141
        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
   142
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   143
1652
ba0a6b4d04f7 more intuitive case
sylvain.thenault@logilab.fr
parents: 1550
diff changeset
   144
class RSSView(XMLView):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   145
    id = 'rss'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   146
    title = _('rss')
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   147
    templatable = False
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   148
    content_type = 'text/xml'
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   149
    http_cache_manager = httpcache.MaxAgeHTTPCacheManager
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   150
    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
   151
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   152
    def _open(self):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   153
        req = self.req
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   154
        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
   155
        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
   156
        self.w(u'  <channel>\n')
1733
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   157
        self.w(u'    <title>%s RSS Feed</title>\n'
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   158
               % xml_escape(self.page_title()))
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   159
        self.w(u'    <description>%s</description>\n'
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   160
               % xml_escape(req.form.get('vtitle', '')))
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   161
        params = req.form.copy()
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   162
        params.pop('vid', None)
1550
8260e857822b use xml_escape
sylvain.thenault@logilab.fr
parents: 1477
diff changeset
   163
        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
   164
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   165
    def _close(self):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   166
        self.w(u'  </channel>\n')
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   167
        self.w(u'</rss>')
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   168
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   169
    def call(self):
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   170
        """display a list of entities by calling their <item_vid> view"""
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   171
        self._open()
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   172
        for i in xrange(self.rset.rowcount):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   173
            self.cell_call(i, 0)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   174
        self._close()
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   175
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   176
    def cell_call(self, row, col):
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   177
        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
   178
1550
8260e857822b use xml_escape
sylvain.thenault@logilab.fr
parents: 1477
diff changeset
   179
1652
ba0a6b4d04f7 more intuitive case
sylvain.thenault@logilab.fr
parents: 1550
diff changeset
   180
class RSSItemView(EntityView):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   181
    id = 'rssitem'
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   182
    date_format = '%%Y-%%m-%%dT%%H:%%M%+03i:00' % (timezone / 3600)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   183
    add_div_section = False
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   184
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   185
    def cell_call(self, row, col):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   186
        entity = self.complete_entity(row, col)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   187
        self.w(u'<item>\n')
1733
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   188
        self.w(u'<guid isPermaLink="true">%s</guid>\n'
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   189
               % xml_escape(entity.absolute_url()))
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   190
        self.render_title_link(entity)
1733
94c36d679454 use html for rss description
sylvain.thenault@logilab.fr
parents: 1652
diff changeset
   191
        self._marker('description', entity.dc_description(format='text/html'))
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   192
        self._marker('dc:date', entity.dc_date(self.date_format))
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   193
        self.render_entity_creator(entity)
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   194
        self.w(u'</item>\n')
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   195
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   196
    def render_title_link(self, entity):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   197
        self._marker('title', entity.dc_long_title())
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   198
        self._marker('link', entity.absolute_url())
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   199
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   200
    def render_entity_creator(self, entity):
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   201
        if entity.creator:
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   202
            self._marker('dc:creator', entity.dc_creator())
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   203
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1135
diff changeset
   204
824
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   205
    def _marker(self, marker, value):
a5e6acffde30 merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
diff changeset
   206
        if value:
1550
8260e857822b use xml_escape
sylvain.thenault@logilab.fr
parents: 1477
diff changeset
   207
            self.w(u'  <%s>%s</%s>\n' % (marker, xml_escape(value), marker))