cubicweb/web/box.py
changeset 12501 4548234cccf9
parent 11767 432f87a63057
child 12502 e651d5f24cb5
equal deleted inserted replaced
12500:ef2168f389f6 12501:4548234cccf9
    27 
    27 
    28 from cubicweb import Unauthorized, role as get_role
    28 from cubicweb import Unauthorized, role as get_role
    29 from cubicweb.schema import display_name
    29 from cubicweb.schema import display_name
    30 from cubicweb.predicates import no_cnx, one_line_rset
    30 from cubicweb.predicates import no_cnx, one_line_rset
    31 from cubicweb.view import View
    31 from cubicweb.view import View
    32 from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs
       
    33 from cubicweb.web.htmlwidgets import (BoxLink, BoxWidget, SideBoxWidget,
    32 from cubicweb.web.htmlwidgets import (BoxLink, BoxWidget, SideBoxWidget,
    34                                       RawBoxItem, BoxSeparator)
    33                                       RawBoxItem, BoxSeparator)
    35 from cubicweb.web.action import UnregisteredAction
    34 from cubicweb.web.action import UnregisteredAction
       
    35 from cubicweb.web.component import AjaxEditRelationCtxComponent, EditRelationMixIn
    36 
    36 
    37 
    37 
    38 def sort_by_category(actions, categories_in_order=None):
    38 def sort_by_category(actions, categories_in_order=None):
    39     """return a list of (category, actions_sorted_by_title)"""
    39     """return a list of (category, actions_sorted_by_title)"""
    40     result = []
    40     result = []
    41     actions_by_cat = {}
    41     actions_by_cat = {}
    42     for action in actions:
    42     for action in actions:
    43         actions_by_cat.setdefault(action.category, []).append(
    43         actions_by_cat.setdefault(action.category, []).append(
    44             (action.title, action) )
    44             (action.title, action))
    45     for key, values in actions_by_cat.items():
    45     for key, values in actions_by_cat.items():
    46         actions_by_cat[key] = [act for title, act in sorted(values, key=lambda x: x[0])]
    46         actions_by_cat[key] = [act for title, act in sorted(values, key=lambda x: x[0])]
    47     if categories_in_order:
    47     if categories_in_order:
    48         for cat in categories_in_order:
    48         for cat in categories_in_order:
    49             if cat in actions_by_cat:
    49             if cat in actions_by_cat:
    50                 result.append( (cat, actions_by_cat[cat]) )
    50                 result.append((cat, actions_by_cat[cat]))
    51     for item in sorted(actions_by_cat.items()):
    51     for item in sorted(actions_by_cat.items()):
    52         result.append(item)
    52         result.append(item)
    53     return result
    53     return result
    54 
    54 
    55 
    55 
    67     Classes inheriting from this class usually only have to override call
    67     Classes inheriting from this class usually only have to override call
    68     to fetch desired actions, and then to do something like  ::
    68     to fetch desired actions, and then to do something like  ::
    69 
    69 
    70         box.render(self.w)
    70         box.render(self.w)
    71     """
    71     """
    72     __deprecation_warning__ = '[3.10] *BoxTemplate classes are deprecated, use *CtxComponent instead (%(cls)s)'
    72     __deprecation_warning__ = (
       
    73         '[3.10] *BoxTemplate classes are deprecated, use *CtxComponent instead (%(cls)s)'
       
    74     )
    73 
    75 
    74     __registry__ = 'ctxcomponents'
    76     __registry__ = 'ctxcomponents'
    75     __select__ = ~no_cnx()
    77     __select__ = ~no_cnx()
    76 
    78 
    77     categories_in_order = ()
    79     categories_in_order = ()
    78     cw_property_defs = {
    80     cw_property_defs = {
    79         _('visible'): dict(type='Boolean', default=True,
    81         _('visible'): dict(type='Boolean', default=True,
    80                            help=_('display the box or not')),
    82                            help=_('display the box or not')),
    81         _('order'):   dict(type='Int', default=99,
    83         _('order'): dict(type='Int', default=99,
    82                            help=_('display order of the box')),
    84                          help=_('display order of the box')),
    83         # XXX 'incontext' boxes are handled by the default primary view
    85         # XXX 'incontext' boxes are handled by the default primary view
    84         _('context'): dict(type='String', default='left',
    86         _('context'): dict(type='String', default='left',
    85                            vocabulary=(_('left'), _('incontext'), _('right')),
    87                            vocabulary=(_('left'), _('incontext'), _('right')),
    86                            help=_('context where this box should be displayed')),
    88                            help=_('context where this box should be displayed')),
    87         }
    89     }
    88     context = 'left'
    90     context = 'left'
    89 
    91 
    90     def sort_actions(self, actions):
    92     def sort_actions(self, actions):
    91         """return a list of (category, actions_sorted_by_title)"""
    93         """return a list of (category, actions_sorted_by_title)"""
    92         return sort_by_category(actions, self.categories_in_order)
    94         return sort_by_category(actions, self.categories_in_order)
   159 
   161 
   160     def call(self, row=0, col=0, **kwargs):
   162     def call(self, row=0, col=0, **kwargs):
   161         """classes inheriting from EntityBoxTemplate should define cell_call"""
   163         """classes inheriting from EntityBoxTemplate should define cell_call"""
   162         self.cell_call(row, col, **kwargs)
   164         self.cell_call(row, col, **kwargs)
   163 
   165 
   164 from cubicweb.web.component import AjaxEditRelationCtxComponent, EditRelationMixIn
       
   165 
       
   166 
   166 
   167 class EditRelationBoxTemplate(EditRelationMixIn, EntityBoxTemplate):
   167 class EditRelationBoxTemplate(EditRelationMixIn, EntityBoxTemplate):
   168     """base class for boxes which let add or remove entities linked
   168     """base class for boxes which let add or remove entities linked
   169     by a given relation
   169     by a given relation
   170 
   170 
   171     subclasses should define at least id, rtype and target
   171     subclasses should define at least id, rtype and target
   172     class attributes.
   172     class attributes.
   173     """
   173     """
   174     rtype = None
   174     rtype = None
       
   175 
   175     def cell_call(self, row, col, view=None, **kwargs):
   176     def cell_call(self, row, col, view=None, **kwargs):
   176         self._cw.add_js('cubicweb.ajax.js')
   177         self._cw.add_js('cubicweb.ajax.js')
   177         entity = self.cw_rset.get_entity(row, col)
   178         entity = self.cw_rset.get_entity(row, col)
   178         title = display_name(self._cw, self.rtype, get_role(self),
   179         title = display_name(self._cw, self.rtype, get_role(self),
   179                              context=entity.cw_etype)
   180                              context=entity.cw_etype)