Skip to content

Commit 2601c0b

Browse files
committed
feat: created custom django_cms serializer to be able to handle django cms filer images
1 parent 703aaa0 commit 2601c0b

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changelog
33
=========
44

5+
1.0.1 (2022-07-09)
6+
==================
7+
8+
* Added support for custom serializer
9+
* Added serializer `django_cms` to be able to serializer `filer.Image`
510

611
1.0.0 (2020-09-02)
712
==================

README.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ django CMS Transfer
88
and import plugin data from a page or a placeholder. It does not support foreign
99
key relations and won't import/export related data, such as `media <https://github.com/django-cms/djangocms-transfer/issues/18>`_.
1010

11-
.. note::
12-
11+
.. note::
12+
1313
This project is endorsed by the `django CMS Association <https://www.django-cms.org/en/about-us/>`_.
14-
That means that it is officially accepted by the dCA as being in line with our roadmap vision and development/plugin policy.
14+
That means that it is officially accepted by the dCA as being in line with our roadmap vision and development/plugin policy.
1515
Join us on `Slack <https://www.django-cms.org/slack/>`_.
1616

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

2424
Because this is a an open-source project, we welcome everyone to
2525
`get involved in the project <https://www.django-cms.org/en/contribute/>`_ and
26-
`receive a reward <https://www.django-cms.org/en/bounty-program/>`_ for their contribution.
27-
Become part of a fantastic community and help us make django CMS the best CMS in the world.
26+
`receive a reward <https://www.django-cms.org/en/bounty-program/>`_ for their contribution.
27+
Become part of a fantastic community and help us make django CMS the best CMS in the world.
2828

2929
We'll be delighted to receive your
3030
feedback in the form of issues and pull requests. Before submitting your
@@ -44,6 +44,14 @@ file for additional dependencies:
4444

4545
|python| |django| |djangocms|
4646

47+
Custom serializer
48+
------------
49+
To be able to define custom behavior for various plugins, you can define a custom serializer as following::
50+
51+
SERIALIZATION_MODULES = {
52+
"django_cms": "djangocms_transfer.serializers.django_cms",
53+
}
54+
DJANGO_CMS_TRANSFER_SERIALIZER = "django_cms"
4755

4856
Installation
4957
------------

djangocms_transfer/serializers/__init__.py

Whitespace-only changes.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import base64
2+
import io
3+
4+
from django.core.files import File as DjangoFile
5+
from django.core.serializers import base
6+
from django.core.serializers.python import (
7+
Serializer as PythonSerializer,
8+
Deserializer as PythonDeserializer,
9+
_get_model,
10+
)
11+
12+
try:
13+
from filer.fields.image import FilerImageField
14+
from filer.models import Image
15+
16+
has_filer = True
17+
except ImportError:
18+
has_filer = False
19+
20+
21+
class FilerImageFieldSerializer:
22+
@classmethod
23+
def serialize(cls, field_instance):
24+
serializer = Serializer()
25+
_image_plugin_data = serializer.serialize((field_instance,))[0]
26+
_file_plugin_data = serializer.serialize(
27+
(field_instance.file_ptr,), fields=["original_filename", "mime_type"]
28+
)[0]
29+
base64_image = base64.b64encode(field_instance.file.read())
30+
31+
_plugin_data = _image_plugin_data["fields"]
32+
_plugin_data.update(_file_plugin_data["fields"])
33+
_plugin_data["file_content"] = base64_image.decode()
34+
return _plugin_data
35+
36+
@classmethod
37+
def deserialize(cls, serialized_data):
38+
base64_image = base64.b64decode(serialized_data.pop("file_content"))
39+
40+
filename = serialized_data["original_filename"]
41+
file_obj = DjangoFile(io.BytesIO(base64_image), name=filename)
42+
image = Image.objects.create(
43+
**serialized_data,
44+
file=file_obj,
45+
)
46+
47+
return image.pk
48+
49+
50+
class Serializer(PythonSerializer):
51+
def handle_fk_field(self, obj, field):
52+
if has_filer and isinstance(field, FilerImageField):
53+
field_instance = getattr(obj, field.name)
54+
self._current[field.name] = FilerImageFieldSerializer.serialize(
55+
field_instance
56+
)
57+
else:
58+
super(Serializer, self).handle_fk_field(obj, field)
59+
60+
61+
def Deserializer(object_list, **options):
62+
for d in object_list:
63+
# Look up the model and starting build a dict of data for it.
64+
try:
65+
Model = _get_model(d["model"])
66+
except base.DeserializationError:
67+
if options["ignorenonexistent"]:
68+
continue
69+
else:
70+
raise
71+
for (field_name, field_value) in d["fields"].items():
72+
field = Model._meta.get_field(field_name)
73+
if has_filer and isinstance(field, FilerImageField):
74+
value = FilerImageFieldSerializer.deserialize(field_value)
75+
d["fields"][field_name] = value
76+
77+
yield from PythonDeserializer(object_list, **options)

0 commit comments

Comments
 (0)