Skip to content

Commit

Permalink
Fixed some minors bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernesto Perez Amigo committed Nov 30, 2017
1 parent 55a30cd commit dde1f1c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion graphene_django_extras/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .paginations import LimitOffsetGraphqlPagination, PageGraphqlPagination, CursorGraphqlPagination
from .types import DjangoObjectType, DjangoInputObjectType, DjangoListObjectType, DjangoSerializerType

VERSION = (0, 1, 3, 'alpha', '1')
VERSION = (0, 1, 3, 'final', '')

__version__ = get_version(VERSION)

Expand Down
2 changes: 1 addition & 1 deletion graphene_django_extras/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from graphene_django.fields import DjangoListField
from graphene_django.utils import import_single_dispatch

from .base_types import Date, GenericForeignKeyType, GenericForeignKeyInputType
from .fields import DjangoFilterListField
from .utils import is_required, get_model_fields, get_related_model
from .base_types import Date, GenericForeignKeyType, GenericForeignKeyInputType

singledispatch = import_single_dispatch()

Expand Down
18 changes: 15 additions & 3 deletions graphene_django_extras/mutation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from collections import OrderedDict

from django.core.exceptions import ValidationError
from graphene import Boolean, List, Field, ID, Argument, ObjectType
from graphene.types.base import BaseOptions
from graphene.utils.deprecated import warn_deprecation
Expand All @@ -11,7 +12,7 @@
from .base_types import object_type_factory, input_object_type_factory
from .registry import get_global_registry
from .types import DjangoObjectType, DjangoInputObjectType
from .utils import get_Object_or_None, kwargs_formatter as native_kwargs_formatter
from .utils import get_Object_or_None, kwargs_formatter as native_kwargs_formatter, parse_validation_exc


class SerializerMutationOptions(BaseOptions):
Expand Down Expand Up @@ -162,8 +163,19 @@ def create_mutation(cls, root, info, **kwargs):
serializer = cls._meta.serializer_class(data=new_obj)

if serializer.is_valid():
obj = serializer.save()
return cls.perform_mutate(obj, info)
try:
cls._meta.model(**serializer.validated_data).full_clean()
obj = serializer.save()
return cls.perform_mutate(obj, info)

except ValidationError as e:
errors_list = parse_validation_exc(e)

errors = [
ErrorType(**errors)
for errors in errors_list
]
return cls.get_errors(errors)

else:
errors = [
Expand Down
12 changes: 11 additions & 1 deletion graphene_django_extras/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_obj(app_label, model_name, object_id):
except TypeError as e:
raise TypeError(e.__str__())
except Exception as e:
return e.__str__()
raise Exception(e.__str__())


def create_obj(model, new_obj_key=None, *args, **kwargs):
Expand Down Expand Up @@ -328,3 +328,13 @@ def queryset_factory(manager, fields_asts=None, fragments=None, **kwargs):
elif select_related and not prefetch_related:
return _get_queryset(manager.select_related(*select_related))
return _get_queryset(manager)


def parse_validation_exc(validation_exc):

errors_list = []
for key, value in validation_exc.error_dict.items():
for exc in value:
errors_list.append({"field": key, "messages": exc.messages})

return errors_list

0 comments on commit dde1f1c

Please sign in to comment.