-
Notifications
You must be signed in to change notification settings - Fork 19
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
viliammihalik
wants to merge
3
commits into
django-cms:master
Choose a base branch
from
viliammihalik:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
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) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it needs to be uppercase https://github.com/django/django/blob/0dd29209091280ccf34e07c9468746c396b7778e/django/core/serializers/__init__.py#L125
There was a problem hiding this comment.
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 ?