Code Monkey home page Code Monkey logo

djangocms-transfer's Introduction

django CMS Transfer

pypi build coverage

django CMS Transfer is an experimental package that allows you to export and import plugin data from a page or a placeholder. It does not support foreign key relations and won't import/export related data, such as media.

Note

This project is endorsed by the django CMS Association. That means that it is officially accepted by the dCA as being in line with our roadmap vision and development/plugin policy. Join us on Slack.

image

Contribute to this project and win rewards

Because this is a an open-source project, we welcome everyone to get involved in the project and receive a reward for their contribution. Become part of a fantastic community and help us make django CMS the best CMS in the world.

We'll be delighted to receive your feedback in the form of issues and pull requests. Before submitting your pull request, please review our contribution guidelines.

We're grateful to all contributors who have helped create and maintain this package. Contributors are listed at the contributors section.

Documentation

The setting DJANGO_CMS_TRANSFER_SERIALIZER allows to register a custom json serializer. An example for an use case could be to subclass Django's build-in python serializer and let it base64-encode inline image data base64.

See REQUIREMENTS in the setup.py file for additional dependencies:

python django djangocms

Installation

For a manual install:

  • run pip install djangocms-transfer
  • add djangocms_transfer to your INSTALLED_APPS
  • run python manage.py migrate djangocms_transfer

Customization

Following settings are available:

  • DJANGOCMS_TRANSFER_PROCESS_EXPORT_PLUGIN_DATA:

    Enables processing of plugin instances prior to serialization, e.g. myapp.module.function.

  • DJANGOCMS_TRANSFER_PROCESS_IMPORT_PLUGIN_DATA:

    Enables processing of plugin instances prior to saving, e.g. myapp.module.function. For example: set default-values for ForeignKeys (images for django_filer, ..)

As an example the combination of _PROCESS_EXPORT_PLUGIN_DATA and _PROCESS_IMPORT_PLUGIN_DATA lets you export and import the data between different systems while setting the contents as you need it:

# settings.py
.._PROCESS_EXPORT_PLUGIN_DATA = "myapp.some.module.export_function"
.._PROCESS_IMPORT_PLUGIN_DATA = "myapp.some.module.import_function"

# custom functions
def export_function(plugin, plugin_data):
    # remove child-plugins which can't be handled
    if plugin.parent_id and plugin.parent.plugin_type == "SomeParentPlugin":
        return None
    # change data
    if plugin.plugin_type == "SomePlugin":
        plugin_data["data"].update({
            "some_field": "TODO: change me",
        })
    return plugin_data

def import_function(deserialized_object):
    some_related_object = MyModel.objects.first()
    for field in deserialized_object.object._meta.fields:
        # example of setting a default value for a related field
        if isinstance(field, ForeignKey):
            value = getattr(deserialized_object.object, field.attname)
            if field.related_model == MyModel and value is not None:
                setattr(deserialized_object.object, field.name, some_related_object)

Running Tests

You can run tests by executing:

virtualenv env
source env/bin/activate
pip install -r tests/requirements/base.txt
python setup.py test

For code formatting, black is used. To automatically fix errors reported from black, you can install it via virtualenv and pip install -r tests/requirements/base.txt. After this you just need to run tools/black.

djangocms-transfer's People

Contributors

arneb avatar czpython avatar filwaitman avatar finalangel avatar fsbraun avatar kaushal-dhungel avatar lgtm-com[bot] avatar marksweb avatar mbi avatar nicolairidani avatar vthaian avatar vxsx avatar wesleyboar avatar wfehr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

djangocms-transfer's Issues

After installing, any command ran from manage.py brings up an error

After installing djangocms-transfer i tried to run python manage.py migrate djangocms_transfer as stated in the readme. This brought up the following error ImportError: cannot import name 'reorder_plugins' from 'cms.utils.plugins' (.../venv/lib/python3.12/site-packages/cms/utils/plugins.py)
This error also occurs whenever I try to run other commands from manage.py e.g: python manage.py runserver, or python manage.py version
Details:
Python version 3.12
Django version 5.0.1
Django cms version 4.1.0

Importing content with media fails to due database constraint violations.

Error Logs

django.db.utils.IntegrityError: insert or update on table "bootstrap4_picture_bootstrap4picture" violates foreign key constraint "bootstrap4_picture_b_picture_id_f26d968d_fk_filer_ima"
web_1               | DETAIL:  Key (picture_id)=(6) is not present in table "filer_image".
New
12:56
Internal Server Error: /admin/cms/page/plugin/plugin_importer/import-plugins/
web_1               | Traceback (most recent call last):
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 242, in _commit
web_1               |     return self.connection.commit()
web_1               | psycopg2.errors.ForeignKeyViolation: insert or update on table "bootstrap4_picture_bootstrap4picture" violates foreign key constraint "bootstrap4_picture_b_picture_id_f26d968d_fk_filer_ima"
web_1               | DETAIL:  Key (picture_id)=(6) is not present in table "filer_image".
web_1               |
web_1               |
web_1               | The above exception was the direct cause of the following exception:
web_1               |
web_1               | Traceback (most recent call last):
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
web_1               |     response = get_response(request)
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
web_1               |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1               |   File "/usr/local/lib/python3.8/site-packages/djangocms_transfer/cms_plugins.py", line 147, in import_plugins_view
web_1               |     import_form.run_import()
web_1               |   File "/usr/local/lib/python3.8/site-packages/djangocms_transfer/forms.py", line 159, in run_import
web_1               |     import_plugins(
web_1               |   File "/usr/local/lib/python3.8/contextlib.py", line 75, in inner
web_1               |     return func(*args, **kwds)
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/transaction.py", line 232, in __exit__
web_1               |     connection.commit()
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
web_1               |     return func(*args, **kwargs)
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 266, in commit
web_1               |     self._commit()
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 242, in _commit
web_1               |     return self.connection.commit()
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
web_1               |     raise dj_exc_value.with_traceback(traceback) from exc_value
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 242, in _commit
web_1               |     return self.connection.commit()
web_1               | django.db.utils.IntegrityError: insert or update on table "bootstrap4_picture_bootstrap4picture" violates foreign key constraint "bootstrap4_picture_b_picture_id_f26d968d_fk_filer_ima"
web_1               | DETAIL:  Key (picture_id)=(6) is not present in table "filer_image".
web_1               |
web_1               | [2021-03-18 03:13:08,290] {log.py:224} ERROR - Internal Server Error: /admin/cms/page/plugin/plugin_importer/import-plugins/
web_1               | Traceback (most recent call last):
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 242, in _commit
web_1               |     return self.connection.commit()
web_1               | psycopg2.errors.ForeignKeyViolation: insert or update on table "bootstrap4_picture_bootstrap4picture" violates foreign key constraint "bootstrap4_picture_b_picture_id_f26d968d_fk_filer_ima"
web_1               | DETAIL:  Key (picture_id)=(6) is not present in table "filer_image".
web_1               |
web_1               |
web_1               | The above exception was the direct cause of the following exception:
web_1               |
web_1               | Traceback (most recent call last):
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
web_1               |     response = get_response(request)
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
web_1               |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1               |   File "/usr/local/lib/python3.8/site-packages/djangocms_transfer/cms_plugins.py", line 147, in import_plugins_view
web_1               |     import_form.run_import()
web_1               |   File "/usr/local/lib/python3.8/site-packages/djangocms_transfer/forms.py", line 159, in run_import
web_1               |     import_plugins(
web_1               |   File "/usr/local/lib/python3.8/contextlib.py", line 75, in inner
web_1               |     return func(*args, **kwds)
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/transaction.py", line 232, in __exit__
web_1               |     connection.commit()
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
web_1               |     return func(*args, **kwargs)
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 266, in commit
web_1               |     self._commit()
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 242, in _commit
web_1               |     return self.connection.commit()
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
web_1               |     raise dj_exc_value.with_traceback(traceback) from exc_value
web_1               |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 242, in _commit
web_1               |     return self.connection.commit()
web_1               | django.db.utils.IntegrityError: insert or update on table "bootstrap4_picture_bootstrap4picture" violates foreign key constraint "bootstrap4_picture_b_picture_id_f26d968d_fk_filer_ima"
web_1               | DETAIL:  Key (picture_id)=(6) is not present in table "filer_image".

Pypi Release

Hi, this app has seen a lot of fixes but no pypi release in 3 years. Can you create a v1.1?

Thank you!

add tests for the project

Follow-up on #33.
Main goal is to implement (at least basic) tests for the functionality of this project, i.e. export and re-import plugins.

Import Error: File is not valid

Hello,

I am getting a File is not valid error when I try to import some plugins.

By inspecting with a debugger, I have pinned down the issue to the presence of the plugin_type field within the data dictionary of a plugin. To be more clear, here is a json example of a problematic plugin:

[{
    "pk": 155,
    "creation_date": "2020-11-03T17:29:48.460Z",
    "position": 0,
    "plugin_type": "BannerTopPlugin",
    "parent_id": null,
    "data": {
        "path": "002J",
        "depth": 1,
        "numchild": 0,
        "placeholder": 13,
        "parent": null,
        "position": 0,
        "language": "it",
        "plugin_type": "BannerTopPlugin",
        "creation_date": "2020-11-03T17:29:48.460Z",
        "changed_date": "2020-11-03T17:29:48.463Z"
}}]

The existence of the plugin_type field within the data dictionary confuses the _object_version_data_hook function, which tries to instantiate an ArchivedPlugin, but fails because of the existence of unknown parameters such as path. Indeed, I could implement a simple fix by patching the _object_version_data_hook to return right away the data dictionary untouched if it doesn't contain the pk key.

Obviously this fix is not sustainable, and it would be better to understand why the plugin_type key is duplicated in the first place. An important hint that I discovered is that this problem seems to only happen with plugins that don't have an associated model in the database.

I am using Django 2.2, django-cms 3.7.4 and djangocms-transfer 1.0.0

Thank you!

Import from Python

A function which needs the json contents and a page as parameters to import it to a page.
This is usefull for seting up standard pages on your site from a custom setup command (e.g. Contact page)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.