Login

Serializer/Deserializer with utf-8/timezone support

Author:
mucius
Posted:
November 24, 2017
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

Sorry, this snippet only tested on Django ver.2.0rc1.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding:utf-8 -*-
""" pretty serialization
    original from <http://djangosnippets.org/snippets/2397/>
"""
from io import StringIO
from datetime import datetime
import yaml

try:
    from yaml import CSafeLoader as SafeLoader
except ImportError:
    from yaml import SafeLoader
from django.utils import timezone
from django.core.serializers.base import DeserializationError
from django.core.serializers.pyyaml import (
    Serializer as YamlSerializer, DjangoSafeDumper)
from django.core.serializers.python import (
    Deserializer as PythonDeserializer,
)


# noinspection PyClassHasNoInit
class Serializer(YamlSerializer):
    """ utf8-friendly dumpdata management command """

    def end_serialization(self):
        yaml.dump(self.objects, self.stream, allow_unicode=True,
                  default_flow_style=False,
                  Dumper=DjangoSafeDumper, **self.options)


def load_yaml(stream_or_string):
    """ laod yaml data with adding timezone """
    output = yaml.load(stream_or_string, SafeLoader)
    for a_model in output:
        for key, value in a_model.items():
            if key == 'fields':
                for vkey, vvalue in value.items():
                    if isinstance(vvalue, datetime):
                        value[vkey] = vvalue.replace(tzinfo=timezone.utc)
    return output


# noinspection PyPep8Naming
def Deserializer(stream_or_string, **options):  # pylint:disable=invalid-name
    """Deserialize a stream or string of YAML data."""
    if isinstance(stream_or_string, bytes):
        stream_or_string = stream_or_string.decode()
    if isinstance(stream_or_string, str):
        stream = StringIO(stream_or_string)
    else:
        stream = stream_or_string
    try:
        yield from PythonDeserializer(load_yaml(stream), **options)
    except (GeneratorExit, DeserializationError):
        raise
    except Exception as exc:
        raise DeserializationError() from exc

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.