|
1 |
| -from functools import partial |
| 1 | +from collections import OrderedDict |
2 | 2 |
|
3 |
| -import six |
4 | 3 | import graphene
|
5 |
| -from graphene import Field, Argument |
6 |
| -from graphene.types.mutation import MutationMeta |
7 |
| -from graphene.types.objecttype import ObjectTypeMeta |
8 |
| -from graphene.types.options import Options |
9 |
| -from graphene.types.utils import get_field_as, merge |
10 |
| -from graphene.utils.is_base_type import is_base_type |
| 4 | +from graphene import Field, InputField |
| 5 | +from graphene.relay.mutation import ClientIDMutation |
| 6 | +from graphene.types.mutation import MutationOptions |
| 7 | +from graphene.types.utils import yank_fields_from_attrs |
11 | 8 | from graphene_django.registry import get_global_registry
|
12 | 9 |
|
13 |
| -from .converter import convert_form_to_input_type |
| 10 | +from .converter import convert_form_field |
14 | 11 | from .types import ErrorType
|
15 | 12 |
|
16 | 13 |
|
17 |
| -class FormMutationMeta(MutationMeta): |
18 |
| - def __new__(cls, name, bases, attrs): |
19 |
| - if not is_base_type(bases, FormMutationMeta): |
20 |
| - return type.__new__(cls, name, bases, attrs) |
21 |
| - |
22 |
| - options = Options( |
23 |
| - attrs.pop('Meta', None), |
24 |
| - name=name, |
25 |
| - description=attrs.pop('__doc__', None), |
26 |
| - form_class=None, |
27 |
| - input_field_name='input', |
28 |
| - local_fields=None, |
29 |
| - only_fields=(), |
30 |
| - exclude_fields=(), |
31 |
| - interfaces=(), |
32 |
| - registry=None |
| 14 | +def fields_for_form(form, only_fields, exclude_fields): |
| 15 | + fields = OrderedDict() |
| 16 | + for name, field in form.fields.items(): |
| 17 | + is_not_in_only = only_fields and name not in only_fields |
| 18 | + is_excluded = ( |
| 19 | + name in exclude_fields # or |
| 20 | + # name in already_created_fields |
33 | 21 | )
|
34 | 22 |
|
35 |
| - if not options.form_class: |
36 |
| - raise Exception('Missing form_class') |
| 23 | + if is_not_in_only or is_excluded: |
| 24 | + continue |
37 | 25 |
|
38 |
| - cls = ObjectTypeMeta.__new__( |
39 |
| - cls, name, bases, dict(attrs, _meta=options) |
40 |
| - ) |
41 |
| - |
42 |
| - options.fields = merge( |
43 |
| - options.interface_fields, options.base_fields, options.local_fields, |
44 |
| - {'errors': get_field_as(cls.errors, Field)} |
45 |
| - ) |
46 |
| - |
47 |
| - cls.Input = convert_form_to_input_type(options.form_class) |
| 26 | + fields[name] = convert_form_field(field) |
| 27 | + return fields |
48 | 28 |
|
49 |
| - field_kwargs = {options.input_field_name: Argument(cls.Input, required=True)} |
50 |
| - cls.Field = partial( |
51 |
| - Field, |
52 |
| - cls, |
53 |
| - resolver=cls.mutate, |
54 |
| - **field_kwargs |
55 |
| - ) |
56 |
| - |
57 |
| - return cls |
58 | 29 |
|
59 |
| - |
60 |
| -class BaseFormMutation(graphene.Mutation): |
| 30 | +class BaseFormMutation(ClientIDMutation): |
| 31 | + class Meta: |
| 32 | + abstract = True |
61 | 33 |
|
62 | 34 | @classmethod
|
63 |
| - def mutate(cls, root, args, context, info): |
64 |
| - form = cls.get_form(root, args, context, info) |
| 35 | + def mutate_and_get_payload(cls, root, info, **input): |
| 36 | + form = cls._meta.form_class(data=input) |
65 | 37 |
|
66 | 38 | if form.is_valid():
|
67 |
| - return cls.form_valid(form, info) |
| 39 | + return cls.perform_mutate(form, info) |
68 | 40 | else:
|
69 |
| - return cls.form_invalid(form, info) |
| 41 | + errors = [ |
| 42 | + ErrorType(field=key, messages=value) |
| 43 | + for key, value in form.errors.items() |
| 44 | + ] |
70 | 45 |
|
71 |
| - @classmethod |
72 |
| - def form_valid(cls, form, info): |
73 |
| - form.save() |
74 |
| - return cls(errors=[]) |
| 46 | + return cls(errors=errors) |
75 | 47 |
|
76 |
| - @classmethod |
77 |
| - def form_invalid(cls, form, info): |
78 |
| - errors = [ |
79 |
| - ErrorType(field=key, messages=value) |
80 |
| - for key, value in form.errors.items() |
81 |
| - ] |
82 |
| - return cls(errors=errors) |
83 | 48 |
|
84 |
| - @classmethod |
85 |
| - def get_form(cls, root, args, context, info): |
86 |
| - form_data = args.get(cls._meta.input_field_name) |
87 |
| - kwargs = cls.get_form_kwargs(root, args, context, info) |
88 |
| - return cls._meta.form_class(data=form_data, **kwargs) |
| 49 | +class FormMutationOptions(MutationOptions): |
| 50 | + form_class = None |
89 | 51 |
|
90 |
| - @classmethod |
91 |
| - def get_form_kwargs(cls, root, args, context, info): |
92 |
| - return {} |
93 | 52 |
|
94 |
| - |
95 |
| -class FormMutation(six.with_metaclass(FormMutationMeta, BaseFormMutation)): |
| 53 | +class FormMutation(BaseFormMutation): |
| 54 | + class Meta: |
| 55 | + abstract = True |
96 | 56 |
|
97 | 57 | errors = graphene.List(ErrorType)
|
98 | 58 |
|
| 59 | + @classmethod |
| 60 | + def __init_subclass_with_meta__(cls, form_class=None, |
| 61 | + only_fields=(), exclude_fields=(), **options): |
99 | 62 |
|
100 |
| -class ModelFormMutationMeta(MutationMeta): |
101 |
| - def __new__(cls, name, bases, attrs): |
102 |
| - if not is_base_type(bases, ModelFormMutationMeta): |
103 |
| - return type.__new__(cls, name, bases, attrs) |
104 |
| - |
105 |
| - options = Options( |
106 |
| - attrs.pop('Meta', None), |
107 |
| - name=name, |
108 |
| - description=attrs.pop('__doc__', None), |
109 |
| - form_class=None, |
110 |
| - input_field_name='input', |
111 |
| - return_field_name=None, |
112 |
| - model=None, |
113 |
| - local_fields=None, |
114 |
| - only_fields=(), |
115 |
| - exclude_fields=(), |
116 |
| - interfaces=(), |
117 |
| - registry=None |
118 |
| - ) |
| 63 | + if not form_class: |
| 64 | + raise Exception('form_class is required for FormMutation') |
119 | 65 |
|
120 |
| - if not options.form_class: |
121 |
| - raise Exception('Missing form_class') |
| 66 | + form = form_class() |
| 67 | + input_fields = fields_for_form(form, only_fields, exclude_fields) |
| 68 | + output_fields = fields_for_form(form, only_fields, exclude_fields) |
122 | 69 |
|
123 |
| - cls = ObjectTypeMeta.__new__( |
124 |
| - cls, name, bases, dict(attrs, _meta=options) |
| 70 | + _meta = FormMutationOptions(cls) |
| 71 | + _meta.form_class = form_class |
| 72 | + _meta.fields = yank_fields_from_attrs( |
| 73 | + output_fields, |
| 74 | + _as=Field, |
125 | 75 | )
|
126 | 76 |
|
127 |
| - options.fields = merge( |
128 |
| - options.interface_fields, options.base_fields, options.local_fields, |
129 |
| - {'errors': get_field_as(cls.errors, Field)} |
| 77 | + input_fields = yank_fields_from_attrs( |
| 78 | + input_fields, |
| 79 | + _as=InputField, |
130 | 80 | )
|
| 81 | + super(FormMutation, cls).__init_subclass_with_meta__(_meta=_meta, input_fields=input_fields, **options) |
131 | 82 |
|
132 |
| - cls.Input = convert_form_to_input_type(options.form_class) |
| 83 | + @classmethod |
| 84 | + def perform_mutate(cls, form, info): |
| 85 | + form.save() |
| 86 | + return cls(errors=None) |
133 | 87 |
|
134 |
| - field_kwargs = {options.input_field_name: Argument(cls.Input, required=True)} |
135 |
| - cls.Field = partial( |
136 |
| - Field, |
137 |
| - cls, |
138 |
| - resolver=cls.mutate, |
139 |
| - **field_kwargs |
140 |
| - ) |
141 | 88 |
|
142 |
| - cls.model = options.model or options.form_class.Meta.model |
143 |
| - cls.return_field_name = cls._meta.return_field_name or cls.model._meta.model_name |
| 89 | +class ModelFormMutationOptions(FormMutationOptions): |
| 90 | + model = None |
| 91 | + return_field_name = None |
144 | 92 |
|
145 |
| - registry = get_global_registry() |
146 |
| - model_type = registry.get_type_for_model(cls.model) |
147 | 93 |
|
148 |
| - options.fields[cls.return_field_name] = graphene.Field(model_type) |
| 94 | +class ModelFormMutation(BaseFormMutation): |
| 95 | + class Meta: |
| 96 | + abstract = True |
149 | 97 |
|
150 |
| - return cls |
| 98 | + errors = graphene.List(ErrorType) |
151 | 99 |
|
| 100 | + @classmethod |
| 101 | + def __init_subclass_with_meta__(cls, form_class=None, model=None, return_field_name=None, |
| 102 | + only_fields=(), exclude_fields=(), **options): |
152 | 103 |
|
153 |
| -class ModelFormMutation(six.with_metaclass(ModelFormMutationMeta, BaseFormMutation)): |
| 104 | + if not form_class: |
| 105 | + raise Exception('form_class is required for ModelFormMutation') |
154 | 106 |
|
155 |
| - errors = graphene.List(ErrorType) |
| 107 | + if not model: |
| 108 | + model = form_class._meta.model |
| 109 | + |
| 110 | + if not model: |
| 111 | + raise Exception('model is required for ModelFormMutation') |
| 112 | + |
| 113 | + form = form_class() |
| 114 | + input_fields = fields_for_form(form, only_fields, exclude_fields) |
| 115 | + |
| 116 | + registry = get_global_registry() |
| 117 | + model_type = registry.get_type_for_model(model) |
| 118 | + return_field_name = return_field_name or model._meta.model_name |
| 119 | + output_fields = OrderedDict() |
| 120 | + output_fields[return_field_name] = graphene.Field(model_type) |
| 121 | + |
| 122 | + _meta = ModelFormMutationOptions(cls) |
| 123 | + _meta.form_class = form_class |
| 124 | + _meta.model = model |
| 125 | + _meta.return_field_name = return_field_name |
| 126 | + _meta.fields = yank_fields_from_attrs( |
| 127 | + output_fields, |
| 128 | + _as=Field, |
| 129 | + ) |
| 130 | + |
| 131 | + input_fields = yank_fields_from_attrs( |
| 132 | + input_fields, |
| 133 | + _as=InputField, |
| 134 | + ) |
| 135 | + super(ModelFormMutation, cls).__init_subclass_with_meta__(_meta=_meta, input_fields=input_fields, **options) |
156 | 136 |
|
157 | 137 | @classmethod
|
158 |
| - def form_valid(cls, form, info): |
| 138 | + def perform_mutate(cls, form, info): |
159 | 139 | obj = form.save()
|
160 |
| - kwargs = {cls.return_field_name: obj} |
161 |
| - return cls(errors=[], **kwargs) |
| 140 | + kwargs = {cls._meta.return_field_name: obj} |
| 141 | + return cls(errors=None, **kwargs) |
0 commit comments