web/views/embedding.py
changeset 3451 6b46d73823f5
parent 3377 dd9d292b6a6d
child 3460 e4843535db25
equal deleted inserted replaced
3448:495862266785 3451:6b46d73823f5
    32     __regid__ = 'external'
    32     __regid__ = 'external'
    33 
    33 
    34     def call(self, body):
    34     def call(self, body):
    35         # XXX fallback to HTML 4 mode when embeding ?
    35         # XXX fallback to HTML 4 mode when embeding ?
    36         self.set_request_content_type()
    36         self.set_request_content_type()
    37         self.req.search_state = ('normal',)
    37         self._cw.search_state = ('normal',)
    38         self.template_header(self.content_type, None, self.req._('external page'),
    38         self.template_header(self.content_type, None, self._cw._('external page'),
    39                              [NOINDEX, NOFOLLOW])
    39                              [NOINDEX, NOFOLLOW])
    40         self.content_header()
    40         self.content_header()
    41         self.w(body)
    41         self.w(body)
    42         self.content_footer()
    42         self.content_footer()
    43         self.template_footer()
    43         self.template_footer()
    46 class EmbedController(Controller):
    46 class EmbedController(Controller):
    47     __regid__ = 'embed'
    47     __regid__ = 'embed'
    48     template = 'external'
    48     template = 'external'
    49 
    49 
    50     def publish(self, rset=None):
    50     def publish(self, rset=None):
    51         req = self.req
    51         req = self._cw
    52         if 'custom_css' in req.form:
    52         if 'custom_css' in req.form:
    53             req.add_css(req.form['custom_css'])
    53             req.add_css(req.form['custom_css'])
    54         embedded_url = req.form['url']
    54         embedded_url = req.form['url']
    55         allowed = self.config['embed-allowed']
    55         allowed = self._cw.config['embed-allowed']
    56         _ = req._
    56         _ = req._
    57         if allowed is None or not allowed.match(embedded_url):
    57         if allowed is None or not allowed.match(embedded_url):
    58             body = '<h2>%s</h2><h3>%s</h3>' % (
    58             body = '<h2>%s</h2><h3>%s</h3>' % (
    59                 _('error while embedding page'),
    59                 _('error while embedding page'),
    60                 _('embedding this url is forbidden'))
    60                 _('embedding this url is forbidden'))
    61         else:
    61         else:
    62             prefix = req.build_url(self.id, url='')
    62             prefix = req.build_url(self.__regid__, url='')
    63             authorization = req.get_header('Authorization')
    63             authorization = req.get_header('Authorization')
    64             if authorization:
    64             if authorization:
    65                 headers = {'Authorization' : authorization}
    65                 headers = {'Authorization' : authorization}
    66             else:
    66             else:
    67                 headers = {}
    67                 headers = {}
    68             try:
    68             try:
    69                 body = embed_external_page(embedded_url, prefix,
    69                 body = embed_external_page(embedded_url, prefix,
    70                                            headers, req.form.get('custom_css'))
    70                                            headers, req.form.get('custom_css'))
    71                 body = soup2xhtml(body, self.req.encoding)
    71                 body = soup2xhtml(body, self._cw.encoding)
    72             except HTTPError, err:
    72             except HTTPError, err:
    73                 body = '<h2>%s</h2><h3>%s</h3>' % (
    73                 body = '<h2>%s</h2><h3>%s</h3>' % (
    74                     _('error while embedding page'), err)
    74                     _('error while embedding page'), err)
    75         self.process_rql(req.form.get('rql'))
    75         self.process_rql(req.form.get('rql'))
    76         return self.vreg['views'].main_template(req, self.template,
    76         return self._cw.vreg['views'].main_template(req, self.template,
    77                                                 rset=self.rset, body=body)
    77                                                 rset=self.cw_rset, body=body)
    78 
    78 
    79 
    79 
    80 def entity_has_embedable_url(entity):
    80 def entity_has_embedable_url(entity):
    81     """return 1 if the entity provides an allowed embedable url"""
    81     """return 1 if the entity provides an allowed embedable url"""
    82     url = entity.embeded_url()
    82     url = entity.embeded_url()
    98                   & score_entity(entity_has_embedable_url))
    98                   & score_entity(entity_has_embedable_url))
    99 
    99 
   100     title = _('embed')
   100     title = _('embed')
   101 
   101 
   102     def url(self, row=0):
   102     def url(self, row=0):
   103         entity = self.rset.get_entity(row, 0)
   103         entity = self.cw_rset.get_entity(row, 0)
   104         url = urljoin(self.req.base_url(), entity.embeded_url())
   104         url = urljoin(self._cw.base_url(), entity.embeded_url())
   105         if self.req.form.has_key('rql'):
   105         if self._cw.form.has_key('rql'):
   106             return self.build_url('embed', url=url, rql=self.req.form['rql'])
   106             return self.build_url('embed', url=url, rql=self._cw.form['rql'])
   107         return self.build_url('embed', url=url)
   107         return self.build_url('embed', url=url)
   108 
   108 
   109 
   109 
   110 
   110 
   111 # functions doing necessary substitutions to embed an external html page ######
   111 # functions doing necessary substitutions to embed an external html page ######