author Sylvain Thénault <>
Mon, 11 Dec 2017 16:10:27 +0100
changeset 2906 69ce3f55cd88
parent 2905 b6b20ce5dead
child 2910 8a08e1df2e59
permissions -rw-r--r--
[seda tree] Extract reparenting into a generic function and move it to the itree module since this is entity logic, not view.

# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact --
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <>.
"""cubicweb-seda views for ArchiveUnit"""

from cubicweb import _
from cubicweb.predicates import adaptable, is_instance
from cubicweb.web import component

from ..entities import simplified_profile, component_unit
from ..entities.itree import reparent
from . import jqtree

class ProfileTreeComponent(component.EntityCtxComponent):
    """Display the whole profile tree."""
    __regid__ = 'seda.tree'
    __select__ = (component.EntityCtxComponent.__select__
                  & adaptable('ITreeBase'))
    context = 'left'
    order = -1
    title = _('SEDA profile tree')

    def render_body(self, w):
        self.entity.view('jqtree.treeview', w=w)

class UnitTreeComponent(ProfileTreeComponent):
    __select__ = ProfileTreeComponent.__select__ & component_unit()
    title = _('Archive unit component tree')

class ArchiveTransferIJQTreeAdapter(jqtree.IJQTreeAdapter):
    __select__ = (jqtree.IJQTreeAdapter.__select__
                  & is_instance('SEDAArchiveTransfer'))

    def maybe_parent_of(self):
        return ['SEDAArchiveUnit',
                'SEDAPhysicalDataObject', 'SEDABinaryDataObject']

class SimplifiedArchiveTransferIJQTreeAdapter(ArchiveTransferIJQTreeAdapter):
    __select__ = ArchiveTransferIJQTreeAdapter.__select__ & simplified_profile()

    def maybe_parent_of(self):
        return ['SEDAArchiveUnit']

class ArchiveUnitIJQTreeAdapter(jqtree.IJQTreeAdapter):
    __select__ = (jqtree.IJQTreeAdapter.__select__
                  & is_instance('SEDAArchiveUnit'))

    def maybe_parent_of(self):
        return [] if self.entity.is_archive_unit_ref else ['SEDAArchiveUnit']

    def maybe_moved(self):
        return True

    def reparent(self, peid):
        parent = self._cw.entity_from_eid(peid)
        if parent.cw_etype == 'SEDAArchiveUnit':
            parent = parent.first_level_choice.content_sequence
            assert parent.cw_etype == 'SEDAArchiveTransfer', (
                'cannot re-parent to entity type {0}'.format(parent.cw_etype))
        reparent(self.entity, parent.eid)

class SimplifiedArchiveUnitIJQTreeAdapter(ArchiveUnitIJQTreeAdapter):
    __select__ = ArchiveUnitIJQTreeAdapter.__select__ & simplified_profile()

    def maybe_parent_of(self):
        return [] if self.entity.is_archive_unit_ref else ['SEDAArchiveUnit',

class DataObjectIJQTreeAdapter(jqtree.IJQTreeAdapter):
    __select__ = (jqtree.IJQTreeAdapter.__select__
                  & is_instance('SEDABinaryDataObject', 'SEDAPhysicalDataObject'))

    def maybe_moved(self):
        return False

class SimplifiedBinaryDataObjectIJQTreeAdapter(jqtree.IJQTreeAdapter):
    __select__ = (jqtree.IJQTreeAdapter.__select__
                  & is_instance('SEDABinaryDataObject') & simplified_profile())
    rtype_to_archivetransfer = 'seda_binary_data_object'

    def maybe_moved(self):
        return True

    def reparent(self, peid):
        archunit = self._cw.entity_from_eid(peid)
        parent = archunit.first_level_choice.content_sequence
        child = self.entity.reverse_seda_data_object_reference_id[0]
        reparent(child, parent.eid)