mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-10 16:22:20 +03:00
linting
This commit is contained in:
parent
cde0e23854
commit
5cd43c562f
|
@ -23,13 +23,13 @@ class SerializerMutationOptions(MutationOptions):
|
||||||
|
|
||||||
|
|
||||||
def fields_for_serializer(
|
def fields_for_serializer(
|
||||||
serializer,
|
serializer,
|
||||||
only_fields,
|
only_fields,
|
||||||
exclude_fields,
|
exclude_fields,
|
||||||
is_input=False,
|
is_input=False,
|
||||||
convert_choices_to_enum=True,
|
convert_choices_to_enum=True,
|
||||||
lookup_field=None,
|
lookup_field=None,
|
||||||
optional_fields=(),
|
optional_fields=(),
|
||||||
):
|
):
|
||||||
fields = OrderedDict()
|
fields = OrderedDict()
|
||||||
for name, field in serializer.fields.items():
|
for name, field in serializer.fields.items():
|
||||||
|
@ -50,11 +50,13 @@ def fields_for_serializer(
|
||||||
|
|
||||||
if is_not_in_only or is_excluded:
|
if is_not_in_only or is_excluded:
|
||||||
continue
|
continue
|
||||||
is_optional = name in optional_fields or '__all__' in optional_fields
|
is_optional = name in optional_fields or "__all__" in optional_fields
|
||||||
|
|
||||||
fields[name] = convert_serializer_field(
|
fields[name] = convert_serializer_field(
|
||||||
field, is_input=is_input, convert_choices_to_enum=convert_choices_to_enum,
|
field,
|
||||||
force_optional=is_optional
|
is_input=is_input,
|
||||||
|
convert_choices_to_enum=convert_choices_to_enum,
|
||||||
|
force_optional=is_optional,
|
||||||
)
|
)
|
||||||
return fields
|
return fields
|
||||||
|
|
||||||
|
@ -69,17 +71,16 @@ class SerializerMutation(ClientIDMutation):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def __init_subclass_with_meta__(
|
def __init_subclass_with_meta__(
|
||||||
cls,
|
cls,
|
||||||
lookup_field=None,
|
lookup_field=None,
|
||||||
serializer_class=None,
|
serializer_class=None,
|
||||||
model_class=None,
|
model_class=None,
|
||||||
model_operations=("create", "update"),
|
model_operations=("create", "update"),
|
||||||
only_fields=(),
|
only_fields=(),
|
||||||
exclude_fields=(),
|
exclude_fields=(),
|
||||||
convert_choices_to_enum=True,
|
convert_choices_to_enum=True,
|
||||||
_meta=None,
|
_meta=None,
|
||||||
optional_fields=()
|
optional_fields=() ** options,
|
||||||
**options
|
|
||||||
):
|
):
|
||||||
if not serializer_class:
|
if not serializer_class:
|
||||||
raise Exception("serializer_class is required for the SerializerMutation")
|
raise Exception("serializer_class is required for the SerializerMutation")
|
||||||
|
@ -103,7 +104,7 @@ class SerializerMutation(ClientIDMutation):
|
||||||
is_input=True,
|
is_input=True,
|
||||||
convert_choices_to_enum=convert_choices_to_enum,
|
convert_choices_to_enum=convert_choices_to_enum,
|
||||||
lookup_field=lookup_field,
|
lookup_field=lookup_field,
|
||||||
optional_fields=optional_fields
|
optional_fields=optional_fields,
|
||||||
)
|
)
|
||||||
output_fields = fields_for_serializer(
|
output_fields = fields_for_serializer(
|
||||||
serializer,
|
serializer,
|
||||||
|
|
|
@ -18,8 +18,9 @@ def get_graphene_type_from_serializer_field(field):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def convert_serializer_field(field, is_input=True, convert_choices_to_enum=True,
|
def convert_serializer_field(
|
||||||
force_optional=False):
|
field, is_input=True, convert_choices_to_enum=True, force_optional=False
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Converts a django rest frameworks field to a graphql field
|
Converts a django rest frameworks field to a graphql field
|
||||||
and marks the field as required if we are creating an input type
|
and marks the field as required if we are creating an input type
|
||||||
|
@ -32,7 +33,10 @@ def convert_serializer_field(field, is_input=True, convert_choices_to_enum=True,
|
||||||
graphql_type = get_graphene_type_from_serializer_field(field)
|
graphql_type = get_graphene_type_from_serializer_field(field)
|
||||||
|
|
||||||
args = []
|
args = []
|
||||||
kwargs = {"description": field.help_text, "required": is_input and field.required and not force_optional}
|
kwargs = {
|
||||||
|
"description": field.help_text,
|
||||||
|
"required": is_input and field.required and not force_optional,
|
||||||
|
}
|
||||||
|
|
||||||
# if it is a tuple or a list it means that we are returning
|
# if it is a tuple or a list it means that we are returning
|
||||||
# the graphql type and the child type
|
# the graphql type and the child type
|
||||||
|
|
|
@ -104,6 +104,7 @@ def test_exclude_fields():
|
||||||
assert "cool_name" in MyMutation.Input._meta.fields
|
assert "cool_name" in MyMutation.Input._meta.fields
|
||||||
assert "created" not in MyMutation.Input._meta.fields
|
assert "created" not in MyMutation.Input._meta.fields
|
||||||
|
|
||||||
|
|
||||||
def test_model_serializer_optional_fields():
|
def test_model_serializer_optional_fields():
|
||||||
class MyMutation(SerializerMutation):
|
class MyMutation(SerializerMutation):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user