goa/appobjects/components.py
branchstable
changeset 6340 470d8e828fda
parent 6339 bdc3dc94d744
child 6341 ad5e08981153
equal deleted inserted replaced
6339:bdc3dc94d744 6340:470d8e828fda
     1 # copyright 2003-2010 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 """overrides some base views for cubicweb on google appengine
       
    19 
       
    20 """
       
    21 __docformat__ = "restructuredtext en"
       
    22 
       
    23 from logilab.mtconverter import xml_escape
       
    24 
       
    25 from cubicweb import typed_eid
       
    26 from cubicweb.selectors import one_line_rset, match_search_state, accept
       
    27 from cubicweb.schema import display_name
       
    28 from cubicweb.view import StartupView, EntityView
       
    29 from cubicweb.web import Redirect
       
    30 from cubicweb.web.views import vid_from_rset
       
    31 
       
    32 from google.appengine.api import mail
       
    33 
       
    34 
       
    35 class SearchForAssociationView(EntityView):
       
    36     """view called by the edition view when the user asks
       
    37     to search for something to link to the edited eid
       
    38     """
       
    39     id = 'search-associate'
       
    40 
       
    41     __select__ = one_line_rset() & match_search_state('linksearch') & accept
       
    42 
       
    43     def cell_call(self, row, col):
       
    44         entity = self.rset.get_entity(0, 0)
       
    45         role, eid, rtype, etype = self.req.search_state[1]
       
    46         assert entity.eid == typed_eid(eid)
       
    47         rset = entity.unrelated(rtype, etype, role, ordermethod='fetch_order')
       
    48         vid = vid_from_rset(self.req, rset, self.schema)
       
    49         self.w(u'<div id="search-associate-content">')
       
    50         self.pagination(self.req, rset, w=self.w)
       
    51         self.wview(vid, rset)
       
    52         self.w(u'</div>')
       
    53 
       
    54 
       
    55 class SchemaImageView(StartupView):
       
    56     id = 'schemagraph'
       
    57     binary = True
       
    58     content_type = 'image/png'
       
    59     def call(self):
       
    60         """display global schema information"""
       
    61         skipmeta = int(self.req.form.get('skipmeta', 1))
       
    62         if skipmeta:
       
    63             url = self.build_url('data/schema.png')
       
    64         else:
       
    65             url = self.build_url('data/metaschema.png')
       
    66         raise Redirect(url)
       
    67 
       
    68 
       
    69 from cubicweb.web.views.baseviews import MetaDataView
       
    70 
       
    71 class GAEMetaDataView(MetaDataView):
       
    72     show_eid = False
       
    73 
       
    74 
       
    75 from cubicweb.web.views.startup import ManageView
       
    76 
       
    77 def entity_types_no_count(self, eschemas):
       
    78     """return a list of formatted links to get a list of entities of
       
    79     a each entity's types
       
    80     """
       
    81     req = self.req
       
    82     for eschema in eschemas:
       
    83         if eschema.final or not (eschema.has_perm(req, 'read') or
       
    84                                       eschema.has_local_role('read')):
       
    85             continue
       
    86         etype = eschema.type
       
    87         label = display_name(req, etype, 'plural')
       
    88         view = self.vreg.select('views', 'list', req, req.etype_rset(etype))
       
    89         url = view.url()
       
    90         etypelink = u'&#160;<a href="%s">%s</a>' % (xml_escape(url), label)
       
    91         yield (label, etypelink, self.add_entity_link(eschema, req))
       
    92 
       
    93 ManageView.entity_types = entity_types_no_count
       
    94 
       
    95 
       
    96 from cubicweb.web.views.basecontrollers import SendMailController
       
    97 
       
    98 def sendmail(self, recipient, subject, body):
       
    99     sender = '%s <%s>' % (
       
   100         self.req.user.dc_title() or self.config['sender-name'],
       
   101         self.req.user.cw_adapt_to('IEmailable').get_email() or self.config['sender-addr'])
       
   102     mail.send_mail(sender=sender, to=recipient,
       
   103                    subject=subject, body=body)
       
   104 
       
   105 SendMailController.sendmail = sendmail