Skip to content

Commit

Permalink
fix get_attribute on related field issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Mng-dev-ai committed Nov 21, 2021
1 parent e1ed2b1 commit eec8e34
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 23 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion drf_turbo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

__author__ = """Michael Gendy"""
__email__ = 'mngback@gmail.com'
__version__ = '0.1.3'
__version__ = '0.1.5'

__all__ = [
'BaseSerializer',
Expand Down
2 changes: 1 addition & 1 deletion drf_turbo/fields.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions drf_turbo/fields.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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.',

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
return getattr(root, self.method_name)
2 changes: 1 addition & 1 deletion drf_turbo/serializer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
11 changes: 1 addition & 10 deletions drf_turbo/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ pyyaml
uritemplate
cython
djangorestframework-simplejwt
psycopg2-binary
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),

Expand Down

0 comments on commit eec8e34

Please sign in to comment.