src/pyams_content/component/association/zmi/__init__.py
changeset 633 7ebe8a011c16
parent 551 0856dfc5bab3
child 678 259e16f7189d
equal deleted inserted replaced
632:aa2352c824ca 633:7ebe8a011c16
    15 
    15 
    16 # import standard library
    16 # import standard library
    17 import json
    17 import json
    18 
    18 
    19 # import interfaces
    19 # import interfaces
    20 from pyams_content.component.association.interfaces import IAssociationTarget, IAssociationContainer, IAssociationInfo
    20 from pyams_content.component.association.interfaces import IAssociationContainerTarget, IAssociationContainer, \
       
    21     IAssociationInfo
    21 from pyams_content.component.association.zmi.interfaces import IAssociationsParentForm, IAssociationsView
    22 from pyams_content.component.association.zmi.interfaces import IAssociationsParentForm, IAssociationsView
    22 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
    23 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
    23 from pyams_form.interfaces.form import IInnerSubForm
    24 from pyams_form.interfaces.form import IInnerSubForm
    24 from pyams_skin.layer import IPyAMSLayer
    25 from pyams_skin.layer import IPyAMSLayer
    25 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
    26 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
    71 
    72 
    72 class AssociationItemAJAXEditForm(AJAXEditForm):
    73 class AssociationItemAJAXEditForm(AJAXEditForm):
    73     """Association item properties edit form, JSON renderer"""
    74     """Association item properties edit form, JSON renderer"""
    74 
    75 
    75     def get_associations_table(self):
    76     def get_associations_table(self):
    76         target = get_parent(self.context, IAssociationTarget)
    77         target = get_parent(self.context, IAssociationContainerTarget)
    77         return {
    78         return {
    78             'status': 'success',
    79             'status': 'success',
    79             'message': self.request.localizer.translate(self.successMessage),
    80             'message': self.request.localizer.translate(self.successMessage),
    80             'events': [
    81             'events': [
    81                 get_json_table_row_refresh_event(target, self.request, AssociationsTable, self.context)
    82                 get_json_table_row_refresh_event(target, self.request, AssociationsTable, self.context)
    85 
    86 
    86 #
    87 #
    87 # Content associations view
    88 # Content associations view
    88 #
    89 #
    89 
    90 
    90 @viewlet_config(name='associations.menu', context=IAssociationTarget, layer=IPyAMSLayer,
    91 @viewlet_config(name='associations.menu', context=IAssociationContainerTarget, layer=IPyAMSLayer,
    91                 manager=IPropertiesMenu, permission=VIEW_SYSTEM_PERMISSION, weight=60)
    92                 manager=IPropertiesMenu, permission=VIEW_SYSTEM_PERMISSION, weight=60)
    92 class AssociationsMenu(MenuItem):
    93 class AssociationsMenu(MenuItem):
    93     """Associations menu"""
    94     """Associations menu"""
    94 
    95 
    95     label = _("Associations...")
    96     label = _("Associations...")
   115         return {'table': ' '.join(classes)}
   116         return {'table': ' '.join(classes)}
   116 
   117 
   117     @property
   118     @property
   118     def data_attributes(self):
   119     def data_attributes(self):
   119         registry = get_current_registry()
   120         registry = get_current_registry()
   120         target = get_parent(self.context, IAssociationTarget)
   121         target = get_parent(self.context, IAssociationContainerTarget)
   121         container = registry.getAdapter(target, IAssociationContainer, name=self.associations_name)
   122         container = registry.getAdapter(target, IAssociationContainer, name=self.associations_name)
   122         attributes = super(AssociationsTable, self).data_attributes
   123         attributes = super(AssociationsTable, self).data_attributes
   123         attributes['table'] = {
   124         attributes['table'] = {
   124             'id': self.id,
   125             'id': self.id,
   125             'data-ams-location': absolute_url(container, self.request),
   126             'data-ams-location': absolute_url(container, self.request),
   132     @reify
   133     @reify
   133     def values(self):
   134     def values(self):
   134         return list(super(AssociationsTable, self).values)
   135         return list(super(AssociationsTable, self).values)
   135 
   136 
   136 
   137 
   137 @adapter_config(context=(IAssociationTarget, IPyAMSLayer, AssociationsTable), provides=IValues)
   138 @adapter_config(context=(IAssociationContainerTarget, IPyAMSLayer, AssociationsTable), provides=IValues)
   138 class AssociationsTableValuesAdapter(ContextRequestViewAdapter):
   139 class AssociationsTableValuesAdapter(ContextRequestViewAdapter):
   139     """Associations table values adapter"""
   140     """Associations table values adapter"""
   140 
   141 
   141     @property
   142     @property
   142     def values(self):
   143     def values(self):
   143         registry = get_current_registry()
   144         registry = get_current_registry()
   144         return registry.getAdapter(self.context, IAssociationContainer, name=self.view.associations_name).values()
   145         return registry.getAdapter(self.context, IAssociationContainer, name=self.view.associations_name).values()
   145 
   146 
   146 
   147 
   147 @adapter_config(name='sorter', context=(IAssociationTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   148 @adapter_config(name='sorter', context=(IAssociationContainerTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   148 @adapter_config(name='sorter', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   149 @adapter_config(name='sorter', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   149 class AssociationsTableSorterColumn(ProtectedFormObjectMixin, SorterColumn):
   150 class AssociationsTableSorterColumn(ProtectedFormObjectMixin, SorterColumn):
   150     """Associations table sorter column"""
   151     """Associations table sorter column"""
   151 
   152 
   152 
   153 
   157     order = list(map(str, json.loads(request.params.get('names'))))
   158     order = list(map(str, json.loads(request.params.get('names'))))
   158     request.context.updateOrder(order)
   159     request.context.updateOrder(order)
   159     return {'status': 'success'}
   160     return {'status': 'success'}
   160 
   161 
   161 
   162 
   162 @adapter_config(name='show-hide', context=(IAssociationTarget, IPyAMSLayer, AssociationsTable),
   163 @adapter_config(name='show-hide', context=(IAssociationContainerTarget, IPyAMSLayer, AssociationsTable),
   163                 provides=IColumn)
   164                 provides=IColumn)
   164 @adapter_config(name='show-hide', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable),
   165 @adapter_config(name='show-hide', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable),
   165                 provides=IColumn)
   166                 provides=IColumn)
   166 class AssociationsTableShowHideColumn(ProtectedFormObjectMixin, VisibilitySwitcherColumn):
   167 class AssociationsTableShowHideColumn(ProtectedFormObjectMixin, VisibilitySwitcherColumn):
   167     """Associations container visibility switcher column"""
   168     """Associations container visibility switcher column"""
   172 def set_association_visibility(request):
   173 def set_association_visibility(request):
   173     """Set association visibility"""
   174     """Set association visibility"""
   174     return switch_element_visibility(request, IAssociationContainer)
   175     return switch_element_visibility(request, IAssociationContainer)
   175 
   176 
   176 
   177 
   177 @adapter_config(name='pictogram', context=(IAssociationTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   178 @adapter_config(name='pictogram', context=(IAssociationContainerTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   178 @adapter_config(name='pictogram', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   179 @adapter_config(name='pictogram', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   179 class AssociationsTablePictogramColumn(ImageColumn):
   180 class AssociationsTablePictogramColumn(ImageColumn):
   180     """Associations table pictogram column"""
   181     """Associations table pictogram column"""
   181 
   182 
   182     weight = 8
   183     weight = 8
   188 
   189 
   189     def get_icon_hint(self, item):
   190     def get_icon_hint(self, item):
   190         return self.request.localizer.translate(item.icon_hint)
   191         return self.request.localizer.translate(item.icon_hint)
   191 
   192 
   192 
   193 
   193 @adapter_config(name='name', context=(IAssociationTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   194 @adapter_config(name='name', context=(IAssociationContainerTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   194 @adapter_config(name='name', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   195 @adapter_config(name='name', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   195 class AssociationsTablePublicNameColumn(NameColumn):
   196 class AssociationsTablePublicNameColumn(NameColumn):
   196     """Associations table name column"""
   197     """Associations table name column"""
   197 
   198 
   198     _header = _("Public title")
   199     _header = _("Public title")
   206         if info.user_icon:
   207         if info.user_icon:
   207             title += ' ' + info.user_icon
   208             title += ' ' + info.user_icon
   208         return title
   209         return title
   209 
   210 
   210 
   211 
   211 @adapter_config(name='inner_name', context=(IAssociationTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   212 @adapter_config(name='inner_name', context=(IAssociationContainerTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   212 @adapter_config(name='inner_name', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   213 @adapter_config(name='inner_name', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   213 class AssociationsTableInnerNameColumn(I18nColumn, GetAttrColumn):
   214 class AssociationsTableInnerNameColumn(I18nColumn, GetAttrColumn):
   214     """Associations table inner name column"""
   215     """Associations table inner name column"""
   215 
   216 
   216     _header = _("Inner title")
   217     _header = _("Inner title")
   222             return info.inner_title
   223             return info.inner_title
   223         else:
   224         else:
   224             return '--'
   225             return '--'
   225 
   226 
   226 
   227 
   227 @adapter_config(name='size', context=(IAssociationTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   228 @adapter_config(name='size', context=(IAssociationContainerTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   228 @adapter_config(name='size', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   229 @adapter_config(name='size', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   229 class AssociationsTableSizeColumn(I18nColumn, GetAttrColumn):
   230 class AssociationsTableSizeColumn(I18nColumn, GetAttrColumn):
   230     """Associations table size column"""
   231     """Associations table size column"""
   231 
   232 
   232     _header = _("Size")
   233     _header = _("Size")
   238             return info.human_size
   239             return info.human_size
   239         else:
   240         else:
   240             return '--'
   241             return '--'
   241 
   242 
   242 
   243 
   243 @adapter_config(name='trash', context=(IAssociationTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   244 @adapter_config(name='trash', context=(IAssociationContainerTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
   244 @adapter_config(name='trash', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   245 @adapter_config(name='trash', context=(IAssociationContainer, IPyAMSLayer, AssociationsTable), provides=IColumn)
   245 class AssociationsTableTrashColumn(ProtectedFormObjectMixin, TrashColumn):
   246 class AssociationsTableTrashColumn(ProtectedFormObjectMixin, TrashColumn):
   246     """Associations table trash column"""
   247     """Associations table trash column"""
   247 
   248 
   248 
   249 
   251 def delete_association(request):
   252 def delete_association(request):
   252     """Delete association"""
   253     """Delete association"""
   253     output = delete_container_element(request)
   254     output = delete_container_element(request)
   254     if output.get('status') == 'success':
   255     if output.get('status') == 'success':
   255         from pyams_content.component.paragraph.zmi import get_json_paragraph_toolbar_refresh_event
   256         from pyams_content.component.paragraph.zmi import get_json_paragraph_toolbar_refresh_event
   256         parent = get_parent(request.context, IAssociationTarget)
   257         parent = get_parent(request.context, IAssociationContainerTarget)
   257         output.update({
   258         output.update({
   258             'handle_json': True,
   259             'handle_json': True,
   259             'events': [
   260             'events': [
   260                 get_json_paragraph_toolbar_refresh_event(parent, request),
   261                 get_json_paragraph_toolbar_refresh_event(parent, request),
   261                 get_json_switched_table_refresh_event(parent, request, AssociationsTable)
   262                 get_json_switched_table_refresh_event(parent, request, AssociationsTable)
   262             ]
   263             ]
   263         })
   264         })
   264     return output
   265     return output
   265 
   266 
   266 
   267 
   267 @pagelet_config(name='associations.html', context=IAssociationTarget, layer=IPyAMSLayer,
   268 @pagelet_config(name='associations.html', context=IAssociationContainerTarget, layer=IPyAMSLayer,
   268                 permission=VIEW_SYSTEM_PERMISSION)
   269                 permission=VIEW_SYSTEM_PERMISSION)
   269 @implementer(IAssociationsView)
   270 @implementer(IAssociationsView)
   270 class AssociationsContainerView(ContainerAdminView, Pagelet):
   271 class AssociationsContainerView(ContainerAdminView, Pagelet):
   271     """Associations container view"""
   272     """Associations container view"""
   272 
   273 
   273     title = _("Associations list")
   274     title = _("Associations list")
   274     table_class = AssociationsTable
   275     table_class = AssociationsTable
   275 
   276 
   276 
   277 
   277 @pagelet_config(name='associations-dialog.html', context=IAssociationTarget, layer=IPyAMSLayer,
   278 @pagelet_config(name='associations-dialog.html', context=IAssociationContainerTarget, layer=IPyAMSLayer,
   278                 permission=VIEW_SYSTEM_PERMISSION)
   279                 permission=VIEW_SYSTEM_PERMISSION)
   279 @implementer(IAssociationsParentForm)
   280 @implementer(IAssociationsParentForm)
   280 class AssociationsContainerDialogView(AdminDialogDisplayForm):
   281 class AssociationsContainerDialogView(AdminDialogDisplayForm):
   281     """Associations dialog view"""
   282     """Associations dialog view"""
   282 
   283 
   285     fieldset_class = 'height-300'
   286     fieldset_class = 'height-300'
   286 
   287 
   287     fields = field.Fields(Interface)
   288     fields = field.Fields(Interface)
   288 
   289 
   289 
   290 
   290 @adapter_config(name='associations', context=(IAssociationTarget, IPyAMSLayer, IAssociationsParentForm),
   291 @adapter_config(name='associations', context=(IAssociationContainerTarget, IPyAMSLayer, IAssociationsParentForm),
   291                 provides=IInnerSubForm)
   292                 provides=IInnerSubForm)
   292 @implementer(IAssociationsView)
   293 @implementer(IAssociationsView)
   293 class AssociationsView(InnerTableView):
   294 class AssociationsView(InnerTableView):
   294     """Associations view"""
   295     """Associations view"""
   295 
   296