From eec8e34b5075607e8ba7948370b9c50c0903f67c Mon Sep 17 00:00:00 2001 From: Micheal Gendy Date: Sun, 21 Nov 2021 21:25:10 +0200 Subject: [PATCH] fix get_attribute on related field issue --- README.rst | 5 ++++- drf_turbo/__init__.py | 2 +- drf_turbo/fields.pxd | 2 +- drf_turbo/fields.pyx | 16 +++++++++------- drf_turbo/serializer.pyx | 2 +- drf_turbo/utils.pyx | 11 +---------- requirements.txt | 1 - setup.py | 2 +- 8 files changed, 18 insertions(+), 23 deletions(-) diff --git a/README.rst b/README.rst index 44e4c73..9710011 100755 --- a/README.rst +++ b/README.rst @@ -13,11 +13,14 @@ drf-turbo :target: https://drf-turbo.readthedocs.io/en/latest/?version=latest :alt: Documentation Status - .. image:: https://pyup.io/repos/github/Mng-dev-ai/drf-turbo/shield.svg :target: https://pyup.io/repos/github/Mng-dev-ai/drf-turbo/ :alt: Updates +.. image:: https://pepy.tech/badge/drf-turbo/ + :target: https://pepy.tech/project/drf-turbo/ + :alt: Downloads + An alternative serializer implementation for REST framework written in cython built for speed. diff --git a/drf_turbo/__init__.py b/drf_turbo/__init__.py index b2fea94..90398d4 100755 --- a/drf_turbo/__init__.py +++ b/drf_turbo/__init__.py @@ -9,7 +9,7 @@ __author__ = """Michael Gendy""" __email__ = 'mngback@gmail.com' -__version__ = '0.1.3' +__version__ = '0.1.5' __all__ = [ 'BaseSerializer', diff --git a/drf_turbo/fields.pxd b/drf_turbo/fields.pxd index 888df5d..98605c7 100755 --- a/drf_turbo/fields.pxd +++ b/drf_turbo/fields.pxd @@ -28,7 +28,7 @@ cdef class Field : cpdef void bind(self,basestring name, object root) cpdef run_validation(self,object data,dict context) cpdef get_initial(self) - cpdef get_attribute(self, instance,attr=*) + cpdef get_attribute(self, instance,attrs=*) cpdef get_default_value(self) cpdef tuple validate_empty_values(self, data) cpdef long validate_or_raise(self,value) except -1 diff --git a/drf_turbo/fields.pyx b/drf_turbo/fields.pyx index 0cf84ea..afd7226 100755 --- a/drf_turbo/fields.pyx +++ b/drf_turbo/fields.pyx @@ -205,14 +205,14 @@ cdef class Field : return self.initial() return self.initial - cpdef get_attribute(self, instance , attr=None): + cpdef get_attribute(self, instance , attrs=None): """ Return the value of the field from the provided instance. """ try: - if attr is None: + if attrs is None: return get_attribute(instance, self.attrs) - return get_attribute(instance, attr) + return get_attribute(instance, attrs) except (KeyError, AttributeError) as exc: if self.default_value is not NO_DEFAULT: return self.get_default_value() @@ -221,8 +221,10 @@ cdef class Field : if not self.required: raise SkipField() msg = ( - 'Got {exc_type} when attempting to get a value for field'.format( + 'Got {exc_type} when attempting to get a value for field {field} on serializer {serializer}'.format( exc_type=type(exc).__name__, + field=self.field_name, + serializer=self.root.__class__.__name__, ) ) raise type(exc)(msg) @@ -298,7 +300,7 @@ cdef class StrField(Field): default_error_messages = { 'blank': 'May not be blank.', 'invalid': 'Not a valid string.', - 'null' : 'Null characters are not allowed.', + 'null_chars' : 'Null characters are not allowed.', 'min_length' : 'Must have at least {min_length} characters.', 'max_length': 'Must have no more than {max_length} characters.', @@ -328,7 +330,7 @@ cdef class StrField(Field): if len(data) > self.max_length: raise self.raise_if_fail("max_length",max_length=self.max_length) if '\x00' in str(data): - raise self.raise_if_fail('null') + raise self.raise_if_fail('null_chars') data = str(data) return data.strip() if self.trim_whitespace else data @@ -1487,4 +1489,4 @@ cdef class MethodField(Field): cpdef inline method_getter(self,field_name, root) : if self.method_name is None: self.method_name = 'get_{0}'.format(field_name) - return getattr(root, self.method_name) \ No newline at end of file + return getattr(root, self.method_name) diff --git a/drf_turbo/serializer.pyx b/drf_turbo/serializer.pyx index d7954c2..d7e5396 100755 --- a/drf_turbo/serializer.pyx +++ b/drf_turbo/serializer.pyx @@ -329,7 +329,7 @@ cdef class Serializer(BaseSerializer): else: try: if isinstance(field,RelatedField): - result = field.get_attribute(instance,attr + '_id') + result = field.get_attribute(instance,[attr + '_id']) else: result = field.get_attribute(instance) if hasattr(result,'all'): diff --git a/drf_turbo/utils.pyx b/drf_turbo/utils.pyx index 264d581..68c6390 100755 --- a/drf_turbo/utils.pyx +++ b/drf_turbo/utils.pyx @@ -64,16 +64,7 @@ cpdef object get_attribute(object instance, list attrs): instance = getattr(instance, attr) except ObjectDoesNotExist : return None - - if callable(instance): - try: - instance = instance() - except (AttributeError, KeyError) as exc: - raise ValueError( - "Unable to resolve attribute '%s' on %s: %s" % ( - attr, instance, exc - ) - ) + return instance diff --git a/requirements.txt b/requirements.txt index 9ceef0b..a921c96 100755 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,3 @@ pyyaml uritemplate cython djangorestframework-simplejwt -psycopg2-binary diff --git a/setup.py b/setup.py index b91f959..aefabe3 100755 --- a/setup.py +++ b/setup.py @@ -41,7 +41,7 @@ packages=find_packages(include=['drf_turbo', 'drf_turbo.*']), test_suite='tests', url='https://github.com/Mng-dev-ai/drf-turbo', - version='0.1.3', + version='0.1.5', zip_safe=False, ext_modules=cythonize(["drf_turbo/*.pyx"]),