collective / collective.exportimport

Export and import content and other data from and to Plone
GNU General Public License v2.0
15 stars 17 forks source link

Parent containers always created as Folder rather than the related @type as defined in parent #139

Open zopyx opened 2 years ago

zopyx commented 2 years ago

Situation:

https://github.com/collective/collective.exportimport/blob/main/src/collective/exportimport/import_content.py#L831

zopyx commented 2 years ago

The basic assumption that every parent is a Folder of this code

https://github.com/collective/collective.exportimport/blob/main/src/collective/exportimport/import_content.py#L816

is wrong. Parent containers can be arbitrary containerish types. This must be taken into account.

It also does not help (as a workaround) to import some container type before the other container type because there parents might be mixed in any arbitrary combination.

zopyx commented 2 years ago

There is perhaps another flaw in the parent handling. Our File.json contains an item http://nohost/eteaching/praxis/digital-learning-map-2020/180202_CfP_pdf.pdf with this parent:

 'parent': {'@id': 'http://nohost/eteaching/praxis/digital-learning-map-2020',
            '@type': 'eteaching.policy.projectsite',
            'description': 'Was macht Lernen mit digitalen Medien an '
                           'Hochschulen erfolgreich? Das untersuchte das vom '
                           'BMBF geförderte Projekt Digital Learning Map 2020 '
                           '(LearnMap). Das dreijährige Projekt lief von 2017 '
                           'bis Anfang 2020 am Leibniz-Institut für '
                           'Wissensmedien (IWM) in Tübingen und betrachtete '
                           'das Thema Digitalisierung von Hochschulen aus '
                           'unterschiedlichen Perspektiven.',
            'review_state': 'published',
            'title': 'Digital Learning Map 2020'},

With the current implementation, both parent folders praxis/digital-learning-map-2020 would be created as Folder which is wrong as discussed above. The container type for digital-learning-map-2020 could be created correctly with the information given from parent. However, there is no information about praxis which could a non-Folder too.

I think, we need

zopyx commented 2 years ago

I implemented the @parents export through a customer export hook like

        def global_dict_hook(self, item, obj):
            """Use this to modify or skip the serialized data by type.
            Return the modified dict (item) or None if you want to skip this particular object.
            """

            def getAcquisitionChain(object):
                inner = object.aq_inner
                iter = inner
                while iter is not None:
                    yield iter
                    if ISiteRoot.providedBy(iter):
                       break
                    if not hasattr(iter, "aq_parent"):
                        raise RuntimeError("Parent traversing interrupted by object: " + str(parent))
                    iter = iter.aq_parent

            parents = list()

            p_chain = reversed(list(getAcquisitionChain(obj)))
            for p in p_chain:

                try:
                    state = get_state(p)
                except:
                    state = ""

                if p.portal_type != 'Plone Site' and p != obj:
                    parents.append({
                        "@id": p.absolute_url(),
                        "@type": p.portal_type,
                        "description": p.Description(),
                        "title": p.Title(),
                        "review_state": state
                        })

            item["parents"] = parents
zopyx commented 2 years ago

The following (ugly) code recreates the folderish objects with the correct portal_type and workflow state:

https://github.com/collective/collective.exportimport/blob/eteaching-fixes/src/collective/exportimport/import_content.py#L833-L862