Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created custom django_cms serializer to be able to handle django cms … #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Changelog
=========

1.0.1 (2022-07-09)
==================

* Added support for Django 4.0
* Added support for custom serializer
* Added serializer `django_cms` to be able to serialize `filer.Image`

1.0.0 (2020-09-02)
==================
Expand Down
18 changes: 13 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ django CMS Transfer
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 <https://github.com/django-cms/djangocms-transfer/issues/18>`_.

.. note::
.. note::

This project is endorsed by the `django CMS Association <https://www.django-cms.org/en/about-us/>`_.
That means that it is officially accepted by the dCA as being in line with our roadmap vision and development/plugin policy.
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 <https://www.django-cms.org/slack/>`_.

.. image:: preview.gif
Expand All @@ -23,8 +23,8 @@ Contribute to this project and win rewards

Because this is a an open-source project, we welcome everyone to
`get involved in the project <https://www.django-cms.org/en/contribute/>`_ and
`receive a reward <https://www.django-cms.org/en/bounty-program/>`_ for their contribution.
Become part of a fantastic community and help us make django CMS the best CMS in the world.
`receive a reward <https://www.django-cms.org/en/bounty-program/>`_ 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
Expand All @@ -44,6 +44,14 @@ file for additional dependencies:

|python| |django| |djangocms|

Custom serializer
------------
To be able to define custom behavior for various plugins, you can define a custom serializer as following::

SERIALIZATION_MODULES = {
"django_cms": "djangocms_transfer.serializers.django_cms",
}
DJANGO_CMS_TRANSFER_SERIALIZER = "django_cms"

Installation
------------
Expand Down
9 changes: 4 additions & 5 deletions djangocms_transfer/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

from django.core.serializers import deserialize
from django.db import transaction
from django.utils.encoding import force_text
from django.utils.encoding import force_str
from django.utils.functional import cached_property

from cms.models import CMSPlugin

from .utils import get_plugin_model

from .utils import get_plugin_model, get_serializer_name

BaseArchivedPlugin = namedtuple(
'ArchivedPlugin',
Expand All @@ -30,12 +29,12 @@
@cached_property
def deserialized_instance(self):
data = {
'model': force_text(self.model._meta),
'model': force_str(self.model._meta),
'fields': self.data,
}

# TODO: Handle deserialization error
return list(deserialize('python', [data]))[0]
return list(deserialize(get_serializer_name(), [data]))[0]

Check warning on line 37 in djangocms_transfer/datastructures.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/datastructures.py#L37

Added line #L37 was not covered by tests

@transaction.atomic
def restore(self, placeholder, language, parent=None, with_data=True):
Expand Down
4 changes: 2 additions & 2 deletions djangocms_transfer/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.core import serializers

from .utils import get_plugin_fields, get_plugin_model
from .utils import get_plugin_fields, get_plugin_model, get_serializer_name


def get_bound_plugins(plugins):
Expand Down Expand Up @@ -41,7 +41,7 @@
custom_data = None
else:
plugin_fields = get_plugin_fields(plugin.plugin_type)
_plugin_data = serializers.serialize('python', (plugin,), fields=plugin_fields)[0]
_plugin_data = serializers.serialize(get_serializer_name(), (plugin,), fields=plugin_fields)[0]

Check warning on line 44 in djangocms_transfer/helpers.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/helpers.py#L44

Added line #L44 was not covered by tests
custom_data = _plugin_data['fields']

plugin_data = {
Expand Down
Empty file.
77 changes: 77 additions & 0 deletions djangocms_transfer/serializers/django_cms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import base64
import io

Check warning on line 2 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L1-L2

Added lines #L1 - L2 were not covered by tests

from django.core.files import File as DjangoFile
from django.core.serializers import base
from django.core.serializers.python import (

Check warning on line 6 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L4-L6

Added lines #L4 - L6 were not covered by tests
Serializer as PythonSerializer,
Deserializer as PythonDeserializer,
_get_model,
)

try:
from filer.fields.image import FilerImageField
from filer.models import Image

Check warning on line 14 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L12-L14

Added lines #L12 - L14 were not covered by tests

has_filer = True
except ImportError:
has_filer = False

Check warning on line 18 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L16-L18

Added lines #L16 - L18 were not covered by tests


class FilerImageFieldSerializer:
@classmethod
def serialize(cls, field_instance):
serializer = Serializer()
_image_plugin_data = serializer.serialize((field_instance,))[0]
_file_plugin_data = serializer.serialize(

Check warning on line 26 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L22-L26

Added lines #L22 - L26 were not covered by tests
(field_instance.file_ptr,), fields=["original_filename", "mime_type"]
)[0]
base64_image = base64.b64encode(field_instance.file.read())

Check warning on line 29 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L29

Added line #L29 was not covered by tests

_plugin_data = _image_plugin_data["fields"]
_plugin_data.update(_file_plugin_data["fields"])
_plugin_data["file_content"] = base64_image.decode()
return _plugin_data

Check warning on line 34 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L31-L34

Added lines #L31 - L34 were not covered by tests

@classmethod
def deserialize(cls, serialized_data):
base64_image = base64.b64decode(serialized_data.pop("file_content"))

Check warning on line 38 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L36-L38

Added lines #L36 - L38 were not covered by tests

filename = serialized_data["original_filename"]
file_obj = DjangoFile(io.BytesIO(base64_image), name=filename)
image = Image.objects.create(

Check warning on line 42 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L40-L42

Added lines #L40 - L42 were not covered by tests
**serialized_data,
file=file_obj,
)

return image.pk

Check warning on line 47 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L47

Added line #L47 was not covered by tests


class Serializer(PythonSerializer):
def handle_fk_field(self, obj, field):

Check warning on line 51 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L51

Added line #L51 was not covered by tests
if has_filer and isinstance(field, FilerImageField):
field_instance = getattr(obj, field.name)
self._current[field.name] = FilerImageFieldSerializer.serialize(

Check warning on line 54 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L53-L54

Added lines #L53 - L54 were not covered by tests
field_instance
)
else:
super(Serializer, self).handle_fk_field(obj, field)

Check warning on line 58 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L58

Added line #L58 was not covered by tests


def Deserializer(object_list, **options):

Check warning on line 61 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L61

Added line #L61 was not covered by tests
Copy link

@robert-foreflight robert-foreflight Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pep8: this should be lowercase if it's a function, however, it appears that you might have meant this to be a class with a deserialize method??? It seems by the name that is should be a class with maybe a static method. Anyway, just a nitpick

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I can change it to lowercase. It was inspired by Django python serializer itself https://github.com/django/django/blob/main/django/core/serializers/python.py#L88

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robert-foreflight what do you think about that ?

for d in object_list:
# Look up the model and starting build a dict of data for it.
try:
Model = _get_model(d["model"])
except base.DeserializationError:

Check warning on line 66 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L64-L66

Added lines #L64 - L66 were not covered by tests
if options["ignorenonexistent"]:
continue

Check warning on line 68 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L68

Added line #L68 was not covered by tests
else:
raise

Check warning on line 70 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L70

Added line #L70 was not covered by tests
for (field_name, field_value) in d["fields"].items():
field = Model._meta.get_field(field_name)

Check warning on line 72 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L72

Added line #L72 was not covered by tests
if has_filer and isinstance(field, FilerImageField):
value = FilerImageFieldSerializer.deserialize(field_value)
d["fields"][field_name] = value

Check warning on line 75 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L74-L75

Added lines #L74 - L75 were not covered by tests

yield from PythonDeserializer(object_list, **options)

Check warning on line 77 in djangocms_transfer/serializers/django_cms.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/serializers/django_cms.py#L77

Added line #L77 was not covered by tests
5 changes: 5 additions & 0 deletions djangocms_transfer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@
@lru_cache()
def get_plugin_model(plugin_type):
return get_plugin_class(plugin_type).model


def get_serializer_name(default='python'):
from django.conf import settings
return getattr(settings, 'DJANGO_CMS_TRANSFER_SERIALIZER', default)

Check warning on line 41 in djangocms_transfer/utils.py

View check run for this annotation

Codecov / codecov/patch

djangocms_transfer/utils.py#L40-L41

Added lines #L40 - L41 were not covered by tests