From 4203aa409201961fb31bfa117f01ee4a5b75f951 Mon Sep 17 00:00:00 2001 From: AbawChen Date: Mon, 13 May 2019 10:42:16 +0800 Subject: [PATCH 1/3] refactor: Reduce some codes --- graphene_mongo/advanced_types.py | 45 ++++++++++++++------------------ 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/graphene_mongo/advanced_types.py b/graphene_mongo/advanced_types.py index d9c90efd..b5bd8921 100644 --- a/graphene_mongo/advanced_types.py +++ b/graphene_mongo/advanced_types.py @@ -11,37 +11,32 @@ def _resolve_fs_field(field, name, default_value=None): return getattr(v, name, default_value) -def _resolve_content_type(self, info): - return _resolve_fs_field(self, 'content_type') - - -def _resolve_md5(self, info): - return _resolve_fs_field(self, 'md5') - - -def _resolve_chunk_size(self, info): - return _resolve_fs_field(self, 'chunk_size', 0) - +class FileFieldType(graphene.ObjectType): -def _resolve_length(self, info): - return _resolve_fs_field(self, 'length', 0) + content_type = graphene.String() + md5 = graphene.String() + chunk_size = graphene.Int() + length = graphene.Int() + data = graphene.String() + def resolve_content_type(self, info): + return _resolve_fs_field(self, 'content_type') -def _resolve_data(self, info): - v = getattr(self.instance, self.key) - data = v.read() - if data is not None: - return base64.b64encode(data) - return None + def resolve_md5(self, info): + return _resolve_fs_field(self, 'md5') + def resolve_chunk_size(self, info): + return _resolve_fs_field(self, 'chunk_size', 0) -class FileFieldType(graphene.ObjectType): + def resolve_length(self, info): + return _resolve_fs_field(self, 'length', 0) - content_type = graphene.String(resolver=_resolve_content_type) - md5 = graphene.String(resolver=_resolve_md5) - chunk_size = graphene.Int(resolver=_resolve_chunk_size) - length = graphene.Int(resolver=_resolve_length) - data = graphene.String(resolver=_resolve_data) + def resolve_data(self, info): + v = getattr(self.instance, self.key) + data = v.read() + if data is not None: + return base64.b64encode(data) + return None class _TypeField(graphene.ObjectType): From 64cca49d72e089e89d5f3509905cc99a1263d2bf Mon Sep 17 00:00:00 2001 From: AbawChen Date: Mon, 13 May 2019 10:49:42 +0800 Subject: [PATCH 2/3] refactor: Move resolve_coordinates into _CoordinatesTypeField --- graphene_mongo/advanced_types.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/graphene_mongo/advanced_types.py b/graphene_mongo/advanced_types.py index b5bd8921..216d33f5 100644 --- a/graphene_mongo/advanced_types.py +++ b/graphene_mongo/advanced_types.py @@ -2,10 +2,6 @@ import graphene -def _resolve_type_coordinates(self, info): - return self['coordinates'] - - def _resolve_fs_field(field, name, default_value=None): v = getattr(field.instance, field.key) return getattr(v, name, default_value) @@ -39,33 +35,34 @@ def resolve_data(self, info): return None -class _TypeField(graphene.ObjectType): +class _CoordinatesTypeField(graphene.ObjectType): type = graphene.String() def resolve_type(self, info): return self['type'] + def resolve_coordinates(self, info): + return self['coordinates'] -class PointFieldType(_TypeField): - coordinates = graphene.List( - graphene.Float, resolver=_resolve_type_coordinates) +class PointFieldType(_CoordinatesTypeField): + coordinates = graphene.List(graphene.Float) -class PolygonFieldType(_TypeField): + +class PolygonFieldType(_CoordinatesTypeField): coordinates = graphene.List( graphene.List( - graphene.List(graphene.Float)), - resolver=_resolve_type_coordinates + graphene.List(graphene.Float)) ) -class MultiPolygonFieldType(_TypeField): +class MultiPolygonFieldType(_CoordinatesTypeField): coordinates = graphene.List( graphene.List( graphene.List( - graphene.List(graphene.Float))), - resolver=_resolve_type_coordinates) + graphene.List(graphene.Float))) + ) From 6ba2db3c61de69ff6c65d36660797bf7b668ab06 Mon Sep 17 00:00:00 2001 From: AbawChen Date: Mon, 13 May 2019 10:59:15 +0800 Subject: [PATCH 3/3] refactor: Move _resolve_fs_field into FileFieldType as a classmethod --- graphene_mongo/advanced_types.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/graphene_mongo/advanced_types.py b/graphene_mongo/advanced_types.py index 216d33f5..e7769507 100644 --- a/graphene_mongo/advanced_types.py +++ b/graphene_mongo/advanced_types.py @@ -2,11 +2,6 @@ import graphene -def _resolve_fs_field(field, name, default_value=None): - v = getattr(field.instance, field.key) - return getattr(v, name, default_value) - - class FileFieldType(graphene.ObjectType): content_type = graphene.String() @@ -15,17 +10,22 @@ class FileFieldType(graphene.ObjectType): length = graphene.Int() data = graphene.String() + @classmethod + def _resolve_fs_field(cls, field, name, default_value=None): + v = getattr(field.instance, field.key) + return getattr(v, name, default_value) + def resolve_content_type(self, info): - return _resolve_fs_field(self, 'content_type') + return FileFieldType._resolve_fs_field(self, 'content_type') def resolve_md5(self, info): - return _resolve_fs_field(self, 'md5') + return FileFieldType._resolve_fs_field(self, 'md5') def resolve_chunk_size(self, info): - return _resolve_fs_field(self, 'chunk_size', 0) + return FileFieldType._resolve_fs_field(self, 'chunk_size', 0) def resolve_length(self, info): - return _resolve_fs_field(self, 'length', 0) + return FileFieldType._resolve_fs_field(self, 'length', 0) def resolve_data(self, info): v = getattr(self.instance, self.key)