|
| 1 | +from collective.exportimport.import_content import ImportContent |
| 2 | +from imio.smartweb.core.behaviors.minisite import IImioSmartwebMinisite |
| 3 | +from imio.smartweb.core.behaviors.minisite import IImioSmartwebMinisiteSettings |
| 4 | +from imio.smartweb.core.contents.pages.pages import IDefaultPages |
| 5 | +from plone.dexterity.interfaces import IDexterityFTI |
| 6 | +from six.moves.urllib.parse import unquote |
| 7 | +from six.moves.urllib.parse import urlparse |
| 8 | +from zope.component import getUtility |
| 9 | +from zope.interface import alsoProvides |
| 10 | +from plone import api |
| 11 | + |
| 12 | +import logging |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class CustomImportContent(ImportContent): |
| 18 | + |
| 19 | + def create_container(self, item): |
| 20 | + """Create container for item. |
| 21 | +
|
| 22 | + See remarks in get_parent_as_container for some corner cases. |
| 23 | + """ |
| 24 | + # if ( |
| 25 | + # item["@id"] |
| 26 | + # == "http://localhost:8080/Plone/fr/je-suis/commercant/section-gallery/banner.jpg" |
| 27 | + # ): |
| 28 | + # import pdb |
| 29 | + |
| 30 | + # pdb.set_trace() |
| 31 | + folder = self.context |
| 32 | + parent_url = unquote(item["parent"]["@id"]) |
| 33 | + parent_url_parsed = urlparse(parent_url) |
| 34 | + # Get the path part, split it, remove the always empty first element. |
| 35 | + parent_path = parent_url_parsed.path.split("/")[1:] |
| 36 | + if ( |
| 37 | + len(parent_url_parsed.netloc.split(":")) > 1 |
| 38 | + or parent_url_parsed.netloc == "nohost" |
| 39 | + ): |
| 40 | + # For example localhost:8080, or nohost when running tests. |
| 41 | + # First element will then be a Plone Site id. |
| 42 | + # Get rid of it. |
| 43 | + parent_path = parent_path[1:] |
| 44 | + |
| 45 | + # Handle folderish Documents provided by plone.volto |
| 46 | + fti = getUtility(IDexterityFTI, name="Document") |
| 47 | + # BOULCH |
| 48 | + # if "fr/la-boverie/album/section-gallery/logo-laboverie.jpg" in item["@id"]: |
| 49 | + # import pdb |
| 50 | + |
| 51 | + # pdb.set_trace() |
| 52 | + parent_type = ( |
| 53 | + "Document" |
| 54 | + if fti.klass.endswith("FolderishDocument") |
| 55 | + else item["parent"]["@type"] |
| 56 | + ) |
| 57 | + # parent_type = ( |
| 58 | + # "Document" if fti.klass.endswith("FolderishDocument") else "Folder" |
| 59 | + # ) |
| 60 | + # create original structure for imported content |
| 61 | + for element in parent_path: |
| 62 | + try: |
| 63 | + if element not in folder: |
| 64 | + folder = api.content.create( |
| 65 | + container=folder, |
| 66 | + type=parent_type, |
| 67 | + id=element, |
| 68 | + title=element, |
| 69 | + ) |
| 70 | + logger.info( |
| 71 | + "Created container %s to hold %s", |
| 72 | + folder.absolute_url(), |
| 73 | + item["@id"], |
| 74 | + ) |
| 75 | + else: |
| 76 | + folder = folder[element] |
| 77 | + except: |
| 78 | + print(f"ERREUR SUR :{element} -- {folder} -- {folder.absolute_url()}") |
| 79 | + |
| 80 | + return folder |
| 81 | + |
| 82 | + def global_obj_hook(self, obj, item): |
| 83 | + """Inspect the content item before serialization data.""" |
| 84 | + |
| 85 | + if "cpskin.minisite.interfaces.IMinisiteRoot" in item.get( |
| 86 | + "_cpskin_interfaces", [] |
| 87 | + ): |
| 88 | + alsoProvides(obj, IImioSmartwebMinisite) |
| 89 | + alsoProvides(obj, IImioSmartwebMinisiteSettings) |
| 90 | + logger.info(f"Set minisite to {obj.absolute_url()} ") |
| 91 | + |
| 92 | + if "imio.smartweb.core.contents.pages.pages.IDefaultPages" in item.get( |
| 93 | + "_smartweb_interfaces", [] |
| 94 | + ): |
| 95 | + alsoProvides(obj, IDefaultPages) |
| 96 | + logger.info(f"Set default page to {obj.absolute_url()} ") |
| 97 | + layout = item.get("layout") |
| 98 | + if layout: |
| 99 | + try: |
| 100 | + obj.setLayout(layout) |
| 101 | + logger.info(f"Set layout : {layout} to {obj.absolute_url()} ") |
| 102 | + # print(f"setLayout {layout} TO {obj.absolute_url()}") |
| 103 | + except AttributeError: |
| 104 | + pass |
| 105 | + obj.reindexObject() |
| 106 | + return obj |
| 107 | + |
| 108 | + # !!! by default, this method remove item layout |
| 109 | + def global_dict_hook(self, item): |
| 110 | + return item |
0 commit comments