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 @@ def model(self):
@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]

@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 @@ def get_plugin_data(plugin, only_meta=False):
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]
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

from django.core.files import File as DjangoFile
from django.core.serializers import base
from django.core.serializers.python import (
Serializer as PythonSerializer,
Deserializer as PythonDeserializer,
_get_model,
)

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

has_filer = True
except ImportError:
has_filer = False


class FilerImageFieldSerializer:
@classmethod
def serialize(cls, field_instance):
serializer = Serializer()
_image_plugin_data = serializer.serialize((field_instance,))[0]
_file_plugin_data = serializer.serialize(
(field_instance.file_ptr,), fields=["original_filename", "mime_type"]
)[0]
base64_image = base64.b64encode(field_instance.file.read())

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

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

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

return image.pk


class Serializer(PythonSerializer):
def handle_fk_field(self, obj, field):
if has_filer and isinstance(field, FilerImageField):
field_instance = getattr(obj, field.name)
self._current[field.name] = FilerImageFieldSerializer.serialize(
field_instance
)
else:
super(Serializer, self).handle_fk_field(obj, field)


def Deserializer(object_list, **options):
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:
if options["ignorenonexistent"]:
continue
else:
raise
for (field_name, field_value) in d["fields"].items():
field = Model._meta.get_field(field_name)
if has_filer and isinstance(field, FilerImageField):
value = FilerImageFieldSerializer.deserialize(field_value)
d["fields"][field_name] = value

yield from PythonDeserializer(object_list, **options)
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 @@ def get_plugin_fields(plugin_type):
@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)