{"id":18388270,"url":"https://github.com/ccnmtl/django-pagetree_export","last_synced_at":"2025-07-16T05:04:28.534Z","repository":{"id":66197766,"uuid":"1860508","full_name":"ccnmtl/django-pagetree_export","owner":"ccnmtl","description":"XML+zipfile-based import/export for django-pagetree and pageblocks","archived":false,"fork":false,"pushed_at":"2019-01-07T14:03:38.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-12T06:44:59.091Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ccnmtl.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.txt","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-06-07T15:27:07.000Z","updated_at":"2019-01-07T13:58:28.000Z","dependencies_parsed_at":"2023-06-28T21:10:04.497Z","dependency_job_id":null,"html_url":"https://github.com/ccnmtl/django-pagetree_export","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/ccnmtl/django-pagetree_export","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ccnmtl%2Fdjango-pagetree_export","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ccnmtl%2Fdjango-pagetree_export/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ccnmtl%2Fdjango-pagetree_export/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ccnmtl%2Fdjango-pagetree_export/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ccnmtl","download_url":"https://codeload.github.com/ccnmtl/django-pagetree_export/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ccnmtl%2Fdjango-pagetree_export/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265483213,"owners_count":23774167,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T01:33:23.844Z","updated_at":"2025-07-16T05:04:28.489Z","avatar_url":"https://github.com/ccnmtl.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/ccnmtl/django-pagetree_export.svg?branch=master)](https://travis-ci.org/ccnmtl/django-pagetree_export)\n\n\nInstallation\n============\n\n    pip install django-pagetree_export\n\nUsage\n=====\n\nThis is a library, not an app; so you'll have to integrate it\ninto your Django project manually.  The interesting functions\nare:\n\n    from pagetree_export.exportimport import export_zip\n    from pagetree_export.exportimport import import_zip\n\nThey do what you'd expect: export a pagetree hierarchy to a zipfile,\nand import a zipfile to a pagetree hierarchy.  You can use them in\nviews like:\n\n    hierarchy = pagetree.helpers.get_hierarchy(name=\"main\")\n    filename = export_zip(hierarchy)\n    with open(filename) as zipfile:\n    \t resp = HttpResponse(zipfile.read())\n    resp['Content-Disposition'] = \"attachment; filename=pagetree-export.zip\"\n    os.unlink(filename)\n    return resp\n\nNote that `export_zip` returns a _filename_ which can be used to read \nand return the contents of a zipfile.  The file it creates is a\ntemporary file (i.e. it lives under /tmp) so you don't have to worry\nabout it too much, but you should remember to be polite and remove the\ntempfile from the system before returning the HTTP response.\n\nAnd here's the corresponding import:\n\n    from zipfile import ZipFile\n    file = request.FILES['file']\n    zipfile = ZipFile(file)\n    new_hierarchy = import_zip(zipfile)\n\nThe `import_zip` function takes a `zipfile.ZipFile` which is an object\nin the Python standard library.  You can get a ZipFile object from a\nDjango file upload by wrapping the object, as shown in the sample\nabove.\n\nDetails\n-------\n\nWhen importing a hierarchy from a zipfile, the system prevents\nduplicate hierarchies with identical names, and duplicate root nodes\nwithin a hierarchy, by using `get_or_create` rather than `create`.  If\na matching hierarchy (or a root node) already exists, its attributes\nwill be replaced with the attributes described in the imported\nzipfile.\n\nIf you're doing export/import on a pair of simple pagetree sites with\nonly one hierarchy (e.g. https://github.com/ccnmtl/Diabeaters) you\nprobably don't need to worry about this -- you'll want the\nimported-site to match the exported-site exactly.\n\nHowever, if you're integrating this library into a complex pagetree\nproject which manages multiple hierarchies in a single installation,\nyou probably *do* need to worry about this: you're using export/import\nas a copy function within a single site.  The way to worry about this\nis to override the import-bundle's knowledge of its own hierarchy\nname, by passing a `hierarchy_name` argument to the `import_zip`\nfunction.  Here's an example, using the hostname of the current\nrequest as the hierarchy identifier:\n   \n    file = request.FILES['file']\n    zipfile = ZipFile(file)\n \n    # If we exported the morx.com site, and we are now\n    # visiting http://fleem.com/import/, we don't want\n    # to touch the morx.com hierarchy -- instead we want\n    # to import the bundle to the fleem.com hierarchy.\n    hierarchy_name = request.get_host()\n    hierarchy = import_zip(zipfile, hierarchy_name)\n\n\nExtensions\n==========\n\nYou can plug in new exporters and importers for particular types of\npageblocks.\n\nAn export function takes a PageBlock, an open writable XML file, and\nan open writable zipfile; it should at minimum write to the XML file\nat its current position, and may also want to write extra files to the\nzipfile.\n\nAn import function takes an xml.etree node and an open readable\nzipfile; it should at minimum parse the node (and its children) to\ngenerate a new PageBlock, and may also want to read extra files from\nthe zipfile.  It should return the newly-created PageBlock.\n\nYou will need to register your exporter and importer functions like\nso:\n\n    from pagetree_export import register\n    register(MyPageBlockClass, \"mypageblockclass\", \n             my_export_fn, my_import_fn)\n\nIf you prefer, you can organize your code with classes, and register\nthe classes with a decorator.  The contract for these classes is:\n\n    from pagetree_export import register_class\n\n    @register_class\n    class MyExporter(object):\n        block_class = MyPageBlockClass\n        identifier = \"mypageblockclass\"\n\n        def exporter(self, block, xmlfile, zipfile):\n            \"\"\" write to the file, return nothing \"\"\"\n\n        def importer(self, etree_node, zipfile):\n            new_block = MyPageBlockClass(**some_attributes)\n            new_block.save()\n            return new_block\n\nThe `exporter` and `importer` methods are both optional; you can omit\none or the other of them to register an exporter with no corresponding\nimporter, or vice versa.  The `block_class` and `identifier`\nattributes are not optional.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fccnmtl%2Fdjango-pagetree_export","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fccnmtl%2Fdjango-pagetree_export","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fccnmtl%2Fdjango-pagetree_export/lists"}