web/views/ajaxedit.py
changeset 1808 aa09e20dd8c0
parent 1622 1a1c494b88e2
child 1977 606923dff11b
equal deleted inserted replaced
1693:49075f57cf2c 1808:aa09e20dd8c0
     1 """Set of views allowing edition of entities/relations using ajax
     1 """Set of views allowing edition of entities/relations using ajax
     2 
     2 
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
     7 __docformat__ = "restructuredtext en"
     7 __docformat__ = "restructuredtext en"
     8 
     8 
     9 from cubicweb.common.selectors import (chainfirst, match_form_params,
     9 from cubicweb import role
    10                                     match_kwargs)
    10 from cubicweb.selectors import match_form_params, match_kwargs
    11 from cubicweb.web.box import EditRelationBoxTemplate
    11 from cubicweb.web.box import EditRelationBoxTemplate
    12 
    12 
    13 class AddRelationView(EditRelationBoxTemplate):
    13 class AddRelationView(EditRelationBoxTemplate):
    14     """base class for view which let add entities linked
    14     """base class for view which let add entities linked
    15     by a given relation
    15     by a given relation
    16 
    16 
    17     subclasses should define at least id, rtype and target
    17     subclasses should define at least id, rtype and target
    18     class attributes.
    18     class attributes.
    19     """
    19     """
    20     __registry__ = 'views'
    20     __registry__ = 'views'
    21     __selectors__ = (chainfirst(match_form_params, match_kwargs),)
    21     __select__ = (match_form_params('rtype', 'target')
       
    22                   | match_kwargs('rtype', 'target'))
    22     property_defs = {} # don't want to inherit this from Box
    23     property_defs = {} # don't want to inherit this from Box
    23     id = 'xaddrelation'
    24     id = 'xaddrelation'
    24     expected_kwargs = form_params = ('rtype', 'target')
    25     expected_kwargs = form_params = ('rtype', 'target')
    25 
    26 
    26     build_js = EditRelationBoxTemplate.build_reload_js_call
    27     build_js = EditRelationBoxTemplate.build_reload_js_call
    27     
    28 
    28     def cell_call(self, row, col, rtype=None, target=None, etype=None):
    29     def cell_call(self, row, col, rtype=None, target=None, etype=None):
    29         self.rtype = rtype or self.req.form['rtype']
    30         self.rtype = rtype or self.req.form['rtype']
    30         self.target = target or self.req.form['target']
    31         self.target = target or self.req.form['target']
    31         self.etype = etype or self.req.form.get('etype')
    32         self.etype = etype or self.req.form.get('etype')
    32         entity = self.entity(row, col)
    33         entity = self.entity(row, col)
    39             if len(etypes) == 1:
    40             if len(etypes) == 1:
    40                 self.etype = etypes[0]
    41                 self.etype = etypes[0]
    41         fakebox = []
    42         fakebox = []
    42         self.w(u'<div id="%s">' % self.id)
    43         self.w(u'<div id="%s">' % self.id)
    43         self.w(u'<h1>%s</h1>' % self.req._('relation %(relname)s of %(ent)s')
    44         self.w(u'<h1>%s</h1>' % self.req._('relation %(relname)s of %(ent)s')
    44                % {'relname': rschema.display_name(self.req, self.xtarget()[0]),
    45                % {'relname': rschema.display_name(self.req, role(self)),
    45                   'ent': entity.view('incontext')})
    46                   'ent': entity.view('incontext')})
    46         self.w(u'<ul>')
    47         self.w(u'<ul>')
    47         self.w_unrelated(fakebox, entity)
    48         self.w_unrelated(fakebox, entity)
    48         for boxitem in fakebox:
    49         for boxitem in fakebox:
    49             boxitem.render(self.w)
    50             boxitem.render(self.w)
    53         """returns the list of unrelated entities
    54         """returns the list of unrelated entities
    54 
    55 
    55         if etype is not defined on the Box's class, the default
    56         if etype is not defined on the Box's class, the default
    56         behaviour is to use the entity's appropraite vocabulary function
    57         behaviour is to use the entity's appropraite vocabulary function
    57         """
    58         """
    58         x, target = self.xtarget()
       
    59         # use entity.unrelated if we've been asked for a particular etype
    59         # use entity.unrelated if we've been asked for a particular etype
    60         if getattr(self, 'etype', None):
    60         if getattr(self, 'etype', None):
    61             rset = entity.unrelated(self.rtype, self.etype, x, ordermethod='fetch_order')
    61             rset = entity.unrelated(self.rtype, self.etype, role(self),
       
    62                                     ordermethod='fetch_order')
    62             self.pagination(self.req, rset, w=self.w)
    63             self.pagination(self.req, rset, w=self.w)
    63             return rset.entities()
    64             return rset.entities()
    64         # in other cases, use vocabulary functions
    65         # in other cases, use vocabulary functions
    65         entities = []
    66         entities = []
    66         for _, eid in entity.vocabulary(self.rtype, x):
    67         # XXX to update for 3.2
       
    68         for _, eid in entity.vocabulary(self.rtype, role(self)):
    67             if eid is not None:
    69             if eid is not None:
    68                 rset = self.req.eid_rset(eid)
    70                 rset = self.req.eid_rset(eid)
    69                 entities.append(rset.get_entity(0, 0))
    71                 entities.append(rset.get_entity(0, 0))
    70         return entities
    72         return entities