Commit f6c46821 authored by Samuel GAIST's avatar Samuel GAIST

[utils][all] Pre-commit cleanup

parent e11107fb
Pipeline #42704 passed with stage
in 15 minutes and 11 seconds
...@@ -25,22 +25,22 @@ ...@@ -25,22 +25,22 @@
# # # #
############################################################################### ###############################################################################
import json
import sys
from actstream.models import Action
from django.contrib.contenttypes.models import ContentType
from django.core.serializers.base import DeserializationError from django.core.serializers.base import DeserializationError
from django.core.serializers.python import Deserializer as PythonDeserializer, _get_model
from django.core.serializers.json import Serializer as JSONSerializer from django.core.serializers.json import Serializer as JSONSerializer
from django.core.serializers.python import Deserializer as PythonDeserializer
from django.core.serializers.python import _get_model
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.contrib.contenttypes.models import ContentType
elements_to_convert = [
from actstream.models import Action ("actor_object_id", "actor_content_type"),
("action_object_object_id", "action_object_content_type"),
import sys ("target_object_id", "target_content_type"),
import json ]
elements_to_convert = [('actor_object_id', 'actor_content_type'),
('action_object_object_id', 'action_object_content_type'),
('target_object_id', 'target_content_type')]
class Serializer(JSONSerializer): class Serializer(JSONSerializer):
...@@ -48,13 +48,14 @@ class Serializer(JSONSerializer): ...@@ -48,13 +48,14 @@ class Serializer(JSONSerializer):
Custom JSON serializer that replaces the actstream Action model ids by Custom JSON serializer that replaces the actstream Action model ids by
their natural key counterpart. their natural key counterpart.
""" """
def get_dump_object(self, obj): def get_dump_object(self, obj):
data = { data = {
"model": force_text(obj._meta), "model": force_text(obj._meta),
"fields": self._current, "fields": self._current,
} }
if not self.use_natural_primary_keys or not hasattr(obj, 'natural_key'): if not self.use_natural_primary_keys or not hasattr(obj, "natural_key"):
data["pk"] = force_text(obj._get_pk_val(), strings_only=True) data["pk"] = force_text(obj._get_pk_val(), strings_only=True)
if self.use_natural_primary_keys and isinstance(obj, Action): if self.use_natural_primary_keys and isinstance(obj, Action):
...@@ -62,15 +63,19 @@ class Serializer(JSONSerializer): ...@@ -62,15 +63,19 @@ class Serializer(JSONSerializer):
element_object_id = element[0] element_object_id = element[0]
element_content_type = element[1] element_content_type = element[1]
element_info = data['fields'][element_content_type] element_info = data["fields"][element_content_type]
if element_info: if element_info:
element_app = element_info[0] element_app = element_info[0]
element_model = element_info[1] element_model = element_info[1]
element_type = ContentType.objects.get(app_label=element_app, model=element_model) element_type = ContentType.objects.get(
element_id = data['fields'][element_object_id] app_label=element_app, model=element_model
element_object = element_type.get_object_for_this_type(id=int(element_id)) )
data['fields'][element_object_id] = element_object.natural_key() element_id = data["fields"][element_object_id]
element_object = element_type.get_object_for_this_type(
id=int(element_id)
)
data["fields"][element_object_id] = element_object.natural_key()
return data return data
...@@ -82,7 +87,7 @@ def Deserializer(stream_or_string, **options): ...@@ -82,7 +87,7 @@ def Deserializer(stream_or_string, **options):
if not isinstance(stream_or_string, (bytes, str)): if not isinstance(stream_or_string, (bytes, str)):
stream_or_string = stream_or_string.read() stream_or_string = stream_or_string.read()
if isinstance(stream_or_string, bytes): if isinstance(stream_or_string, bytes):
stream_or_string = stream_or_string.decode('utf-8') stream_or_string = stream_or_string.decode("utf-8")
try: try:
objects = json.loads(stream_or_string) objects = json.loads(stream_or_string)
...@@ -94,16 +99,20 @@ def Deserializer(stream_or_string, **options): ...@@ -94,16 +99,20 @@ def Deserializer(stream_or_string, **options):
for element in elements_to_convert: for element in elements_to_convert:
element_object_id = element[0] element_object_id = element[0]
element_content_type = element[1] element_content_type = element[1]
element_info = d['fields'][element_content_type] element_info = d["fields"][element_content_type]
if element_info: if element_info:
element_app = element_info[0] element_app = element_info[0]
element_model = element_info[1] element_model = element_info[1]
element_type = ContentType.objects.get(app_label=element_app, model=element_model) element_type = ContentType.objects.get(
element_natural_key = d['fields'][element_object_id] app_label=element_app, model=element_model
element_object = element_type.model_class().objects.get_by_natural_key(*element_natural_key) )
element_natural_key = d["fields"][element_object_id]
element_object = element_type.model_class().objects.get_by_natural_key(
*element_natural_key
)
d['fields'][element_object_id] = str(element_object.id) d["fields"][element_object_id] = str(element_object.id)
for obj in PythonDeserializer(objects, **options): for obj in PythonDeserializer(objects, **options):
yield obj yield obj
...@@ -111,4 +120,4 @@ def Deserializer(stream_or_string, **options): ...@@ -111,4 +120,4 @@ def Deserializer(stream_or_string, **options):
raise raise
except Exception as e: except Exception as e:
# Map to deserializer error # Map to deserializer error
raise(DeserializationError, DeserializationError(e), sys.exc_info()[2]) raise (DeserializationError, DeserializationError(e), sys.exc_info()[2])
...@@ -27,12 +27,11 @@ ...@@ -27,12 +27,11 @@
""" """
Django REST framework helpers Django REST framework helpers
""" """
import logging import logging
from rest_framework.views import exception_handler
from rest_framework.exceptions import APIException from rest_framework.exceptions import APIException
from rest_framework.status import is_client_error from rest_framework.status import is_client_error
from rest_framework.views import exception_handler
logger = logging.getLogger("beat.drf_exceptions") logger = logging.getLogger("beat.drf_exceptions")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment