mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-11 00:32:24 +03:00
Refactor validation function to reduce complexity
This commit is contained in:
parent
34db851230
commit
98fd498959
|
@ -62,10 +62,7 @@ def construct_fields(
|
||||||
return fields
|
return fields
|
||||||
|
|
||||||
|
|
||||||
def validate_fields(type_, model, fields, only_fields, exclude_fields):
|
def validate_only_fields(only_fields, all_field_names, model, type_):
|
||||||
# Validate the given fields against the model's fields and custom fields
|
|
||||||
all_field_names = set(fields.keys())
|
|
||||||
only_fields = only_fields if only_fields is not ALL_FIELDS else ()
|
|
||||||
for name in only_fields or ():
|
for name in only_fields or ():
|
||||||
if name in all_field_names:
|
if name in all_field_names:
|
||||||
continue
|
continue
|
||||||
|
@ -83,8 +80,8 @@ def validate_fields(type_, model, fields, only_fields, exclude_fields):
|
||||||
type_=type_,
|
type_=type_,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
else:
|
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
(
|
(
|
||||||
'Field name "{field_name}" doesn\'t exist on Django model "{app_label}.{object_name}". '
|
'Field name "{field_name}" doesn\'t exist on Django model "{app_label}.{object_name}". '
|
||||||
|
@ -97,6 +94,7 @@ def validate_fields(type_, model, fields, only_fields, exclude_fields):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def validate_exclude_fields(exclude_fields, all_field_names, model, type_):
|
||||||
# Validate exclude fields
|
# Validate exclude fields
|
||||||
for name in exclude_fields or ():
|
for name in exclude_fields or ():
|
||||||
if name in all_field_names:
|
if name in all_field_names:
|
||||||
|
@ -107,7 +105,8 @@ def validate_fields(type_, model, fields, only_fields, exclude_fields):
|
||||||
'Either remove the custom field or remove the field from the "exclude" list.'
|
'Either remove the custom field or remove the field from the "exclude" list.'
|
||||||
).format(field_name=name, type_=type_)
|
).format(field_name=name, type_=type_)
|
||||||
)
|
)
|
||||||
else:
|
continue
|
||||||
|
|
||||||
if not hasattr(model, name):
|
if not hasattr(model, name):
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
(
|
(
|
||||||
|
@ -121,6 +120,14 @@ def validate_fields(type_, model, fields, only_fields, exclude_fields):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def validate_fields(type_, model, fields, only_fields, exclude_fields):
|
||||||
|
# Validate the given fields against the model's fields and custom fields
|
||||||
|
all_field_names = set(fields.keys())
|
||||||
|
only_fields = only_fields if only_fields is not ALL_FIELDS else ()
|
||||||
|
|
||||||
|
validate_only_fields(only_fields, all_field_names, model, type_)
|
||||||
|
validate_exclude_fields(exclude_fields, all_field_names, model, type_)
|
||||||
|
|
||||||
|
|
||||||
class DjangoObjectTypeOptions(ObjectTypeOptions):
|
class DjangoObjectTypeOptions(ObjectTypeOptions):
|
||||||
model = None # type: Type[Model]
|
model = None # type: Type[Model]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user