cubicweb/web/views/editviews.py
changeset 11057 0b59724cb3f2
parent 10666 7f6b5f023884
child 11767 432f87a63057
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """Some views used to help to the edition process"""
       
    19 
       
    20 __docformat__ = "restructuredtext en"
       
    21 from cubicweb import _
       
    22 
       
    23 from logilab.common.decorators import cached
       
    24 from logilab.mtconverter import xml_escape
       
    25 
       
    26 from cubicweb.view import EntityView, StartupView
       
    27 from cubicweb.predicates import (one_line_rset, non_final_entity,
       
    28                                  match_search_state)
       
    29 from cubicweb.web import httpcache
       
    30 from cubicweb.web.views import baseviews, linksearch_select_url
       
    31 
       
    32 
       
    33 class SearchForAssociationView(EntityView):
       
    34     """view called by the edition view when the user asks to search for
       
    35     something to link to the edited eid
       
    36     """
       
    37     __regid__ = 'search-associate'
       
    38     __select__ = (one_line_rset() & match_search_state('linksearch')
       
    39                   & non_final_entity())
       
    40 
       
    41     title = _('search for association')
       
    42 
       
    43     def cell_call(self, row, col):
       
    44         rset, vid, divid, paginate = self.filter_box_context_info()
       
    45         self.cw_rset = rset
       
    46         self.w(u'<div id="%s">' % divid)
       
    47         self.paginate()
       
    48         self.wview(vid, rset, 'noresult')
       
    49         self.w(u'</div>')
       
    50 
       
    51     @cached
       
    52     def filter_box_context_info(self):
       
    53         entity = self.cw_rset.get_entity(0, 0)
       
    54         role, eid, rtype, etype = self._cw.search_state[1]
       
    55         assert entity.eid == int(eid)
       
    56         # the default behaviour is to fetch all unrelated entities and display
       
    57         # them. Use fetch_order and not fetch_unrelated_order as sort method
       
    58         # since the latter is mainly there to select relevant items in the combo
       
    59         # box, it doesn't give interesting result in this context
       
    60         rql, args = entity.cw_unrelated_rql(rtype, etype, role,
       
    61                                             ordermethod='fetch_order',
       
    62                                             vocabconstraints=False)
       
    63         rset = self._cw.execute(rql, args)
       
    64         return rset, 'list', "search-associate-content", True
       
    65 
       
    66 
       
    67 class OutOfContextSearch(EntityView):
       
    68     __regid__ = 'outofcontext-search'
       
    69     def cell_call(self, row, col):
       
    70         entity = self.cw_rset.get_entity(row, col)
       
    71         erset = entity.as_rset()
       
    72         if self._cw.match_search_state(erset):
       
    73             self.w(u'<a href="%s" title="%s">%s</a>&#160;<a href="%s" title="%s">[...]</a>' % (
       
    74                 xml_escape(linksearch_select_url(self._cw, erset)),
       
    75                 self._cw._('select this entity'),
       
    76                 xml_escape(entity.view('textoutofcontext')),
       
    77                 xml_escape(entity.absolute_url(vid='primary')),
       
    78                 self._cw._('view detail for this entity')))
       
    79         else:
       
    80             entity.view('outofcontext', w=self.w)
       
    81 
       
    82 
       
    83 class ComboboxView(EntityView):
       
    84     """the view used in combobox (unrelated entities)
       
    85 
       
    86     THIS IS A TEXT VIEW. DO NOT HTML_ESCAPE
       
    87     """
       
    88     __regid__ = 'combobox'
       
    89     title = None
       
    90 
       
    91     def cell_call(self, row, col, **kwargs):
       
    92         """the combo-box view for an entity: same as text out of context view
       
    93         by default
       
    94         """
       
    95         self.wview('textoutofcontext', self.cw_rset, row=row, col=col)
       
    96 
       
    97 
       
    98 class EditableFinalView(baseviews.FinalView):
       
    99     """same as FinalView but enables inplace-edition when possible"""
       
   100     __regid__ = 'editable-final'
       
   101 
       
   102     def cell_call(self, row, col, props=None):
       
   103         entity, rtype = self.cw_rset.related_entity(row, col)
       
   104         if entity is not None:
       
   105             self.w(entity.view('reledit', rtype=rtype))
       
   106         else:
       
   107             super(EditableFinalView, self).cell_call(row, col, props)
       
   108 
       
   109 try:
       
   110     from cubicweb.web import captcha
       
   111 except ImportError:
       
   112     # PIL not installed
       
   113     pass
       
   114 else:
       
   115     class CaptchaView(StartupView):
       
   116         __regid__ = 'captcha'
       
   117 
       
   118         http_cache_manager = httpcache.NoHTTPCacheManager
       
   119         binary = True
       
   120         templatable = False
       
   121         content_type = 'image/jpg'
       
   122 
       
   123         def call(self):
       
   124             text, data = captcha.captcha(self._cw.vreg.config['captcha-font-file'],
       
   125                                          self._cw.vreg.config['captcha-font-size'])
       
   126             key = self._cw.form.get('captchakey', 'captcha')
       
   127             self._cw.session.data[key] = text
       
   128             self.w(data.read())