-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
mutations.py
1188 lines (1009 loc) · 40.3 KB
/
mutations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os.path
import secrets
from collections.abc import Collection, Iterable
from enum import Enum
from itertools import chain
from typing import (
Any,
Optional,
TypeVar,
Union,
cast,
overload,
)
from uuid import UUID
import graphene
from django.core.exceptions import (
NON_FIELD_ERRORS,
ImproperlyConfigured,
ValidationError,
)
from django.core.files.storage import default_storage
from django.core.files.uploadedfile import UploadedFile
from django.db.models import Model, Q, QuerySet
from django.db.models.fields.files import FileField
from graphene import ObjectType
from graphene.types.mutation import MutationOptions
from graphql.error import GraphQLError
from ...core.db.connection import allow_writer
from ...core.error_codes import MetadataErrorCode
from ...core.exceptions import PermissionDenied
from ...core.utils.events import call_event
from ...permission.auth_filters import AuthorizationFilters
from ...permission.enums import BasePermissionEnum
from ...permission.utils import (
all_permissions_required,
message_one_of_permissions_required,
one_of_permissions_or_auth_filter_required,
)
from ..account.utils import get_user_accessible_channels
from ..app.dataloaders import get_app_promise
from ..core.doc_category import DOC_CATEGORY_MAP
from ..core.validators import validate_one_of_args_is_in_mutation
from ..meta.permissions import PRIVATE_META_PERMISSION_MAP, PUBLIC_META_PERMISSION_MAP
from ..payment.utils import metadata_contains_empty_key
from ..plugins.dataloaders import get_plugin_manager_promise
from ..utils import get_nodes, resolve_global_ids_to_primary_keys
from . import ResolveInfo
from .context import disallow_replica_in_context, setup_context_user
from .descriptions import DEPRECATED_IN_3X_FIELD
from .types import (
TYPES_WITH_DOUBLE_ID_AVAILABLE,
File,
ModelObjectType,
NonNullList,
Upload,
UploadError,
)
from .utils import (
WebhookEventInfo,
ext_ref_to_global_id_or_error,
from_global_id_or_error,
message_webhook_events,
snake_to_camel_case,
)
from .utils.error_codes import get_error_code_from_error
def get_model_name(model):
"""Return name of the model with first letter lowercase."""
model_name = model.__name__
return model_name[:1].lower() + model_name[1:]
def get_error_fields(error_type_class, error_type_field, deprecation_reason=None):
error_field = graphene.Field(
NonNullList(
error_type_class,
description="List of errors that occurred executing the mutation.",
),
default_value=[],
required=True,
)
if deprecation_reason is not None:
error_field.deprecation_reason = deprecation_reason
return {error_type_field: error_field}
def validation_error_to_error_type(
validation_error: ValidationError, error_type_class
) -> list:
"""Convert a ValidationError into a list of Error types."""
err_list = []
error_class_fields = set(error_type_class._meta.fields.keys())
if hasattr(validation_error, "error_dict"):
# convert field errors
for field_label, field_errors in validation_error.error_dict.items():
field = (
None
if field_label == NON_FIELD_ERRORS
else snake_to_camel_case(field_label)
)
for err in field_errors:
error = error_type_class(
field=field,
message=err.messages[0],
code=get_error_code_from_error(err),
)
attach_error_params(error, err.params, error_class_fields)
err_list.append(error)
else:
# convert non-field errors
for err in validation_error.error_list:
error = error_type_class(
message=err.messages[0],
code=get_error_code_from_error(err),
)
attach_error_params(error, err.params, error_class_fields)
err_list.append(error)
return err_list
def attach_error_params(error, params: Optional[dict], error_class_fields: set):
if not params:
return {}
# If some of the params key overlap with error class fields
# attach param value to the error
error_fields_in_params = set(params.keys()) & error_class_fields
for error_field in error_fields_in_params:
setattr(error, error_field, params[error_field])
class ModelMutationOptions(MutationOptions):
doc_category = None
exclude = None
model = None
object_type = None
return_field_name = None
MT = TypeVar("MT", bound=Model)
class BaseMutation(graphene.Mutation):
class Meta:
abstract = True
@classmethod
def _validate_permissions(cls, permissions):
if not permissions:
return
if not isinstance(permissions, tuple):
raise ImproperlyConfigured(
f"Permissions should be a tuple in Meta class: {permissions}"
)
for p in permissions:
if not isinstance(p, Enum):
raise ImproperlyConfigured(f"Permission should be an enum: {p}.")
@classmethod
def __init_subclass_with_meta__(
cls,
auto_permission_message=True,
description=None,
doc_category=None,
permissions: Optional[Collection[BasePermissionEnum]] = None,
_meta=None,
error_type_class=None,
error_type_field=None,
errors_mapping=None,
support_meta_field=False,
support_private_meta_field=False,
auto_webhook_events_message: bool = True,
webhook_events_info: Optional[list[WebhookEventInfo]] = None,
**options,
):
if not _meta:
_meta = MutationOptions(cls)
if not description:
raise ImproperlyConfigured("No description provided in Meta")
if not error_type_class:
raise ImproperlyConfigured("No error_type_class provided in Meta.")
cls._validate_permissions(permissions)
_meta.auto_permission_message = auto_permission_message
_meta.error_type_class = error_type_class
_meta.error_type_field = error_type_field
_meta.errors_mapping = errors_mapping
_meta.permissions = permissions
_meta.support_meta_field = support_meta_field
_meta.support_private_meta_field = support_private_meta_field
if permissions and auto_permission_message:
permissions_msg = message_one_of_permissions_required(permissions)
description = f"{description} {permissions_msg}"
if webhook_events_info and auto_webhook_events_message:
description += message_webhook_events(webhook_events_info)
cls.webhook_events_info = webhook_events_info
cls.doc_category = doc_category
super().__init_subclass_with_meta__(
description=description, _meta=_meta, **options
)
if error_type_field:
deprecated_msg = f"{DEPRECATED_IN_3X_FIELD} Use `errors` field instead."
cls._meta.fields.update(
get_error_fields(
error_type_class,
error_type_field,
deprecated_msg,
)
)
cls._meta.fields.update(get_error_fields(error_type_class, "errors"))
@classmethod
def _update_mutation_arguments_and_fields(cls, arguments, fields):
cls._meta.arguments.update(arguments)
cls._meta.fields.update(fields)
@classmethod
def _get_node_by_pk(
cls,
info: ResolveInfo,
graphene_type: type[ModelObjectType[MT]],
pk: Union[int, str],
qs=None,
) -> Optional[MT]:
"""Attempt to resolve a node from the given internal ID.
Whether by using the provided query set object or by calling type's get_node().
"""
if qs is not None:
lookup = Q(pk=pk)
if pk is not None and str(graphene_type) in TYPES_WITH_DOUBLE_ID_AVAILABLE:
# This is temporary solution that allows fetching objects with use of
# new and old id.
try:
UUID(str(pk))
except ValueError:
lookup = (
Q(number=pk) & Q(use_old_id=True)
if str(graphene_type) == "Order"
else Q(old_id=pk) & Q(old_id__isnull=False)
)
return qs.filter(lookup).first()
get_node = getattr(graphene_type, "get_node", None)
if get_node:
return get_node(info, pk)
return None
@classmethod
def get_global_id_or_error(
cls,
id: str,
only_type: Union[ObjectType, str, None] = None,
field: str = "id",
):
try:
_object_type, pk = from_global_id_or_error(id, only_type, raise_error=True)
except GraphQLError as e:
raise ValidationError(
{field: ValidationError(str(e), code="graphql_error")}
)
return pk
@overload
@classmethod
def get_node_or_error(
cls,
info: ResolveInfo,
node_id: str,
*,
field: str = "id",
only_type: type[ModelObjectType[MT]],
qs: Any = None,
code: str = "not_found",
) -> MT: ...
@overload
@classmethod
def get_node_or_error(
cls,
info: ResolveInfo,
node_id: Optional[str],
*,
field: str = "id",
only_type: type[ModelObjectType[MT]],
qs: Any = None,
code: str = "not_found",
) -> Optional[MT]: ...
@overload
@classmethod
def get_node_or_error(
cls,
info: ResolveInfo,
node_id: str,
*,
field: str = "id",
only_type: None,
qs: QuerySet[MT],
code: str = "not_found",
) -> MT: ...
@overload
@classmethod
def get_node_or_error(
cls,
info: ResolveInfo,
node_id: str,
*,
field: str = "id",
only_type: None = None,
qs: Any = None,
code: str = "not_found",
) -> Model: ...
@overload
@classmethod
def get_node_or_error(
cls,
info: ResolveInfo,
node_id: Optional[str],
*,
field: str = "id",
only_type: Any = None,
qs: Any = None,
code: str = "not_found",
) -> Optional[Model]: ...
@classmethod
def get_node_or_error(
cls,
info: ResolveInfo,
node_id: Optional[str],
*,
field: str = "id",
only_type: Optional[type[ObjectType]] = None,
qs: Optional[QuerySet] = None,
code: str = "not_found",
) -> Optional[Model]:
if not node_id:
# FIXME: this is weird behavior and we should drop it
# the function now has three possible outcomes:
# * Null
# * the object you asked for
# * ValidationError
return None
try:
object_type, pk = from_global_id_or_error(
node_id, only_type, raise_error=True
)
if isinstance(object_type, str):
object_type = info.schema.get_type(object_type).graphene_type
node = cls._get_node_by_pk(info, graphene_type=object_type, pk=pk, qs=qs)
except (AssertionError, GraphQLError) as e:
raise ValidationError(
{field: ValidationError(str(e), code="graphql_error")}
)
else:
if node is None:
raise ValidationError(
{
field: ValidationError(
f"Couldn't resolve to a node: {node_id}", code=code
)
}
)
return node
@classmethod
def get_global_ids_or_error(
cls,
ids: Iterable[str],
only_type: Union[ObjectType, str, None] = None,
field: str = "ids",
):
try:
_nodes_type, pks = resolve_global_ids_to_primary_keys(
ids, only_type, raise_error=True
)
except GraphQLError as e:
raise ValidationError(
{field: ValidationError(str(e), code="graphql_error")}
)
return pks
@overload
@classmethod
def get_nodes_or_error(
cls, ids, field, only_type: type[ModelObjectType[MT]], qs=None, schema=None
) -> list[MT]: ...
@overload
@classmethod
def get_nodes_or_error(
cls, ids, field, only_type: Optional[ObjectType] = None, qs=None, schema=None
) -> list[Model]: ...
@classmethod
def get_nodes_or_error(cls, ids, field, only_type=None, qs=None, schema=None):
try:
instances = get_nodes(ids, only_type, qs=qs, schema=schema)
except GraphQLError as e:
raise ValidationError(
{field: ValidationError(str(e), code="graphql_error")}
)
return instances
@staticmethod
def remap_error_fields(validation_error, field_map) -> None:
"""Rename validation_error fields according to provided field_map.
Skips renaming fields from field_map that are not on validation_error.
"""
for old_field, new_field in field_map.items():
try:
validation_error.error_dict[new_field] = (
validation_error.error_dict.pop(old_field)
)
except KeyError:
pass
@classmethod
def clean_instance(cls, info: ResolveInfo, instance, /) -> None:
"""Clean the instance that was created using the input data.
Once an instance is created, this method runs `full_clean()` to perform
model validation.
"""
try:
instance.full_clean()
except ValidationError as error:
if hasattr(cls._meta, "exclude"):
# Ignore validation errors for fields that are specified as
# excluded.
new_error_dict = {}
for field, errors in error.error_dict.items():
if field not in cls._meta.exclude:
new_error_dict[field] = errors
error.error_dict = new_error_dict
if cls._meta.errors_mapping:
cls.remap_error_fields(error, cls._meta.errors_mapping)
if error.error_dict:
raise error
@classmethod
def construct_instance(cls, instance, cleaned_data):
"""Fill instance fields with cleaned data.
The `instance` argument is either an empty instance of a already
existing one which was fetched from the database. `cleaned_data` is
data to be set in instance fields. Returns `instance` with filled
fields, but not saved to the database.
"""
from django.db import models
opts = instance._meta
for f in opts.fields:
if any(
[
not f.editable,
isinstance(f, models.AutoField),
f.name not in cleaned_data,
]
):
continue
data = cleaned_data[f.name]
if data is None:
# We want to reset the file field value when None was passed
# in the input, but `FileField.save_form_data` ignores None
# values. In that case we manually pass False which clears
# the file.
if isinstance(f, FileField):
data = False
if not f.null:
data = f._get_default()
f.save_form_data(instance, data)
return instance
@classmethod
def check_permissions(
cls, context, permissions=None, require_all_permissions=False, **data
):
"""Determine whether user or app has rights to perform this mutation.
Default implementation assumes that account is allowed to perform any
mutation. By overriding this method or defining required permissions
in the meta-class, you can restrict access to it.
The `context` parameter is the Context instance associated with the request.
"""
all_permissions = permissions or cls._meta.permissions
if not all_permissions:
return True
if require_all_permissions:
return all_permissions_required(context, all_permissions)
return one_of_permissions_or_auth_filter_required(context, all_permissions)
@classmethod
@allow_writer()
def mutate(cls, root, info: ResolveInfo, **data):
disallow_replica_in_context(info.context)
setup_context_user(info.context)
if not cls.check_permissions(info.context, data=data):
raise PermissionDenied(permissions=cls._meta.permissions)
manager = get_plugin_manager_promise(info.context).get()
result = manager.perform_mutation(
mutation_cls=cls, root=root, info=info, data=data
)
if result is not None:
return result
try:
response = cls.perform_mutation(root, info, **data)
if response.errors is None:
response.errors = []
return response
except ValidationError as e:
return cls.handle_errors(e)
@classmethod
def perform_mutation(cls, _root, _info: ResolveInfo, /):
raise NotImplementedError()
@classmethod
def handle_errors(cls, error: ValidationError, **extra):
error_list = validation_error_to_error_type(error, cls._meta.error_type_class)
return cls.handle_typed_errors(error_list, **extra)
@classmethod
def handle_typed_errors(cls, errors: list, **extra):
"""Return class instance with errors."""
if cls._meta.error_type_field is not None:
extra.update({cls._meta.error_type_field: errors})
return cls(errors=errors, **extra)
@staticmethod
def call_event(func_obj, *func_args, **kwargs):
return call_event(func_obj, *func_args, **kwargs)
@classmethod
def update_metadata(cls, instance, meta_data_list: list, is_private: bool = False):
if is_private:
instance.store_value_in_private_metadata(
{data.key: data.value for data in meta_data_list}
)
else:
instance.store_value_in_metadata(
{data.key: data.value for data in meta_data_list}
)
@classmethod
def validate_metadata_keys(cls, metadata_list: list[dict]):
if metadata_contains_empty_key(metadata_list):
raise ValidationError(
{
"input": ValidationError(
"Metadata key cannot be empty.",
code=MetadataErrorCode.REQUIRED.value,
)
}
)
@classmethod
def validate_and_update_metadata(
cls, instance, metadata_list, private_metadata_list
):
if cls._meta.support_meta_field and metadata_list is not None:
cls.validate_metadata_keys(metadata_list)
cls.update_metadata(instance, metadata_list)
if cls._meta.support_private_meta_field and private_metadata_list is not None:
cls.validate_metadata_keys(private_metadata_list)
cls.update_metadata(instance, private_metadata_list, is_private=True)
@classmethod
def check_metadata_permissions(cls, info: ResolveInfo, object_id, private=False):
type_name, db_id = graphene.Node.from_global_id(object_id)
if private:
meta_permission = PRIVATE_META_PERMISSION_MAP.get(type_name)
else:
meta_permission = PUBLIC_META_PERMISSION_MAP.get(type_name)
if not meta_permission:
raise NotImplementedError(
f"Couldn't resolve permission to item type: {type_name}. "
)
@classmethod
def check_channel_permissions(
cls, info: ResolveInfo, channel_ids: Iterable[Union[UUID, int]]
):
# App has access to all channels
if get_app_promise(info.context).get():
return
accessible_channels = get_user_accessible_channels(info, info.context.user)
accessible_channel_ids = {str(channel.id) for channel in accessible_channels}
channel_ids = {str(channel_id) for channel_id in channel_ids}
invalid_channel_ids = channel_ids - accessible_channel_ids
if invalid_channel_ids:
raise PermissionDenied(
message="You don't have access to some objects' channel."
)
def is_list_of_ids(field) -> bool:
if isinstance(field.type, graphene.List):
of_type = field.type.of_type
if isinstance(of_type, graphene.NonNull):
of_type = of_type.of_type
return of_type == graphene.ID
return False
def is_id_field(field) -> bool:
return (
field.type == graphene.ID
or isinstance(field.type, graphene.NonNull)
and field.type.of_type == graphene.ID
)
def is_upload_field(field) -> bool:
if hasattr(field.type, "of_type"):
return field.type.of_type == Upload
return field.type == Upload
class ModelMutation(BaseMutation):
class Meta:
abstract = True
@classmethod
def __init_subclass_with_meta__(
cls,
arguments=None,
model=None,
exclude=None,
return_field_name=None,
object_type=None,
_meta=None,
**options,
):
if not model:
raise ImproperlyConfigured("model is required for ModelMutation")
if not _meta:
_meta = ModelMutationOptions(cls)
doc_category_key = f"{model._meta.app_label}.{model.__name__}"
if "doc_category" not in options and doc_category_key in DOC_CATEGORY_MAP:
options["doc_category"] = DOC_CATEGORY_MAP[doc_category_key]
if exclude is None:
exclude = []
if not return_field_name:
return_field_name = get_model_name(model)
if arguments is None:
arguments = {}
_meta.model = model
_meta.object_type = object_type
_meta.return_field_name = return_field_name
_meta.exclude = exclude
super().__init_subclass_with_meta__(_meta=_meta, **options)
model_type = cls.get_type_for_model()
if not model_type:
raise ImproperlyConfigured(
f"GraphQL type for model {cls._meta.model.__name__} could not be "
f"resolved for {cls.__name__}"
)
fields = {return_field_name: graphene.Field(model_type)}
cls._update_mutation_arguments_and_fields(arguments=arguments, fields=fields)
@classmethod
def clean_input(cls, info: ResolveInfo, instance, data, *, input_cls=None):
"""Clean input data received from mutation arguments.
Fields containing IDs or lists of IDs are automatically resolved into
model instances. `instance` argument is the model instance the mutation
is operating on (before setting the input data). `input` is raw input
data the mutation receives.
Override this method to provide custom transformations of incoming
data.
"""
if not input_cls:
input_cls = getattr(cls.Arguments, "input")
cleaned_input = {}
for field_name, field_item in input_cls._meta.fields.items():
if field_name in data:
value = data[field_name]
# handle list of IDs field
if value is not None and is_list_of_ids(field_item):
instances = (
cls.get_nodes_or_error(value, field_name, schema=info.schema)
if value
else []
)
cleaned_input[field_name] = instances
# handle ID field
elif value is not None and is_id_field(field_item):
instance = cls.get_node_or_error(info, value, field=field_name)
cleaned_input[field_name] = instance
# handle uploaded files
elif value is not None and is_upload_field(field_item):
value = info.context.FILES.get(value)
cleaned_input[field_name] = value
# handle other fields
else:
cleaned_input[field_name] = value
return cleaned_input
@classmethod
def _save_m2m(cls, _info: ResolveInfo, instance, cleaned_data):
opts = instance._meta
for f in chain(opts.many_to_many, opts.private_fields):
if not hasattr(f, "save_form_data"):
continue
if f.name in cleaned_data and cleaned_data[f.name] is not None:
f.save_form_data(instance, cleaned_data[f.name])
@classmethod
def success_response(cls, instance):
"""Return a success response."""
return cls(**{cls._meta.return_field_name: instance, "errors": []})
@classmethod
def save(cls, _info: ResolveInfo, instance, _cleaned_input, /):
instance.save()
@classmethod
def get_type_for_model(cls):
if not cls._meta.object_type:
raise ImproperlyConfigured(
f"Either GraphQL type for model {cls._meta.model.__name__} needs to be "
f"specified on object_type option or {cls.__name__} needs to define "
"custom get_type_for_model() method."
)
return cls._meta.object_type
@classmethod
def get_instance(cls, info: ResolveInfo, **data):
"""Retrieve an instance from the supplied global id.
The expected graphene type can be lazy (str).
"""
object_id = data.get("id")
qs = data.get("qs")
if object_id:
model_type = cls.get_type_for_model()
instance = cls.get_node_or_error(
info, object_id, only_type=model_type, qs=qs
)
else:
instance = cls._meta.model()
return instance
@classmethod
def post_save_action(cls, info: ResolveInfo, instance, cleaned_input):
"""Perform an action after saving an object and its m2m."""
@classmethod
def perform_mutation(cls, _root, info: ResolveInfo, /, **data):
"""Perform model mutation.
Depending on the input data, `mutate` either creates a new instance or
updates an existing one. If `id` argument is present, it is assumed
that this is an "update" mutation. Otherwise, a new instance is
created based on the model associated with this mutation.
"""
instance = cls.get_instance(info, **data)
data = data.get("input")
cleaned_input = cls.clean_input(info, instance, data)
metadata_list = cleaned_input.pop("metadata", None)
private_metadata_list = cleaned_input.pop("private_metadata", None)
instance = cls.construct_instance(instance, cleaned_input)
cls.validate_and_update_metadata(instance, metadata_list, private_metadata_list)
cls.clean_instance(info, instance)
cls.save(info, instance, cleaned_input)
cls._save_m2m(info, instance, cleaned_input)
# add to cleaned_input popped metadata to allow running post save events
# that depends on the metadata inputs
if metadata_list:
cleaned_input["metadata"] = metadata_list
if private_metadata_list:
cleaned_input["private_metadata"] = private_metadata_list
cls.post_save_action(info, instance, cleaned_input)
return cls.success_response(instance)
class ModelWithExtRefMutation(ModelMutation):
class Meta:
abstract = True
@classmethod
def get_object_id(cls, **data):
"""Resolve object id by given id or external reference."""
object_id, ext_ref = data.get("id"), data.get("external_reference")
validate_one_of_args_is_in_mutation(
"id", object_id, "external_reference", ext_ref
)
if ext_ref and not object_id:
object_id = ext_ref_to_global_id_or_error(cls._meta.model, ext_ref)
return object_id
@classmethod
def get_instance(cls, info, **data):
"""Retrieve an instance from the supplied global id.
The expected graphene type can be lazy (str).
"""
object_id = cls.get_object_id(**data)
qs = data.get("qs")
if object_id:
model_type = cls.get_type_for_model()
return cls.get_node_or_error(info, object_id, only_type=model_type, qs=qs)
class ModelWithRestrictedChannelAccessMutation(ModelMutation):
class Meta:
abstract = True
@classmethod
def perform_mutation(cls, _root, info: ResolveInfo, /, **data):
"""Perform model mutation.
Depending on the input data, `mutate` either creates a new instance or
updates an existing one. If `id` argument is present, it is assumed
that this is an "update" mutation. Otherwise, a new instance is
created based on the model associated with this mutation.
"""
instance = cls.get_instance(info, **data)
channel_id = cls.get_instance_channel_id(instance, **data)
cls.check_channel_permissions(info, [channel_id])
data = data.get("input")
cleaned_input = cls.clean_input(info, instance, data)
metadata_list = cleaned_input.pop("metadata", None)
private_metadata_list = cleaned_input.pop("private_metadata", None)
instance = cls.construct_instance(instance, cleaned_input)
cls.validate_and_update_metadata(instance, metadata_list, private_metadata_list)
cls.clean_instance(info, instance)
cls.save(info, instance, cleaned_input)
cls._save_m2m(info, instance, cleaned_input)
cls.post_save_action(info, instance, cleaned_input)
return cls.success_response(instance)
@classmethod
def get_instance_channel_id(cls, instance, **data) -> Union[UUID, int]:
"""Retrieve the instance channel id for channel permission accessible check."""
raise NotImplementedError()
class ModelDeleteMutation(ModelMutation):
class Meta:
abstract = True
@classmethod
def clean_instance(cls, _info: ResolveInfo, _instance, /):
"""Perform additional logic before deleting the model instance.
Override this method to raise custom validation error and abort
the deletion process.
"""
@classmethod
def perform_mutation( # type: ignore[override]
cls, _root, info: ResolveInfo, /, *, external_reference=None, id=None
):
"""Perform a mutation that deletes a model instance."""
instance = cls.get_instance(info, external_reference=external_reference, id=id)
cls.clean_instance(info, instance)
db_id = instance.id
instance.delete()
# After the instance is deleted, set its ID to the original database's
# ID so that the success response contains ID of the deleted object.
instance.id = db_id
cls.post_save_action(info, instance, None)
return cls.success_response(instance)
class ModelDeleteWithRestrictedChannelAccessMutation(ModelDeleteMutation):
class Meta:
abstract = True
@classmethod
def perform_mutation( # type: ignore[override]
cls, _root, info: ResolveInfo, /, *, external_reference=None, id=None
):
"""Perform a mutation that deletes a model instance."""
instance = cls.get_instance(info, external_reference=external_reference, id=id)
channel_id = cls.get_instance_channel_id(instance)
cls.check_channel_permissions(info, [channel_id])
cls.clean_instance(info, instance)
db_id = instance.id
instance.delete()
# After the instance is deleted, set its ID to the original database's
# ID so that the success response contains ID of the deleted object.
instance.id = db_id
cls.post_save_action(info, instance, None)
return cls.success_response(instance)
@classmethod
def get_instance_channel_id(cls, instance) -> Union[UUID, int]:
"""Retrieve the instance channel id for channel permission accessible check."""
raise NotImplementedError()
class BaseBulkMutation(BaseMutation):
count = graphene.Int(
required=True, description="Returns how many objects were affected."
)
class Meta:
abstract = True
@classmethod
def __init_subclass_with_meta__(
cls, model=None, object_type=None, _meta=None, **kwargs
):
if not model:
raise ImproperlyConfigured("model is required for bulk mutation")
if not _meta:
_meta = ModelMutationOptions(cls)
_meta.model = model
_meta.object_type = object_type
doc_category_key = f"{model._meta.app_label}.{model.__name__}"
if "doc_category" not in kwargs and doc_category_key in DOC_CATEGORY_MAP:
kwargs["doc_category"] = DOC_CATEGORY_MAP[doc_category_key]
super().__init_subclass_with_meta__(_meta=_meta, **kwargs)
@classmethod
def get_type_for_model(cls):
if not cls._meta.object_type:
raise ImproperlyConfigured(
f"Either GraphQL type for model {cls._meta.model.__name__} needs to be "
f"specified on object_type option or {cls.__name__} needs to define "
"custom get_type_for_model() method."
)
return cls._meta.object_type
@classmethod
def clean_instance(cls, _info: ResolveInfo, _instance, /):
"""Perform additional logic.
Override this method to raise custom validation error and prevent
bulk action on the instance.
"""
@classmethod
def bulk_action(cls, _info: ResolveInfo, _queryset: QuerySet, /):
"""Implement action performed on queryset."""
raise NotImplementedError
@classmethod
def perform_mutation( # type: ignore[override]
cls, _root, info: ResolveInfo, /, *, ids, **data
) -> tuple[int, Optional[ValidationError]]:
"""Perform a mutation that deletes a list of model instances."""
clean_instance_ids = []
errors_dict: dict[str, list[ValidationError]] = {}