-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
mutations.py
478 lines (416 loc) · 17.6 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
import graphene
from django.core.exceptions import ValidationError
from ...account import models as account_models
from ...channel import models as channel_models
from ...core.error_codes import ShopErrorCode
from ...core.permissions import GiftcardPermissions, OrderPermissions, SitePermissions
from ...core.utils.url import validate_storefront_url
from ...site import GiftCardSettingsExpiryType
from ...site.error_codes import GiftCardSettingsErrorCode
from ...site.models import DEFAULT_LIMIT_QUANTITY_PER_CHECKOUT
from ..account.i18n import I18nMixin
from ..account.types import AddressInput, StaffNotificationRecipient
from ..core.descriptions import ADDED_IN_31, DEPRECATED_IN_3X_INPUT, PREVIEW_FEATURE
from ..core.enums import WeightUnitsEnum
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.types import (
GiftCardSettingsError,
OrderSettingsError,
ShopError,
TimePeriodInputType,
)
from ..site.dataloaders import get_site_promise
from .enums import GiftCardSettingsExpiryTypeEnum
from .types import GiftCardSettings, OrderSettings, Shop
class ShopSettingsInput(graphene.InputObjectType):
header_text = graphene.String(description="Header text.")
description = graphene.String(description="SEO description.")
track_inventory_by_default = graphene.Boolean(
description="Enable inventory tracking."
)
default_weight_unit = WeightUnitsEnum(description="Default weight unit.")
automatic_fulfillment_digital_products = graphene.Boolean(
description="Enable automatic fulfillment for all digital products."
)
fulfillment_auto_approve = graphene.Boolean(
description="Enable automatic approval of all new fulfillments." + ADDED_IN_31
)
fulfillment_allow_unpaid = graphene.Boolean(
description=(
"Enable ability to approve fulfillments which are unpaid." + ADDED_IN_31
)
)
default_digital_max_downloads = graphene.Int(
description="Default number of max downloads per digital content URL."
)
default_digital_url_valid_days = graphene.Int(
description="Default number of days which digital content URL will be valid."
)
default_mail_sender_name = graphene.String(
description="Default email sender's name."
)
default_mail_sender_address = graphene.String(
description="Default email sender's address."
)
customer_set_password_url = graphene.String(
description="URL of a view where customers can set their password."
)
reserve_stock_duration_anonymous_user = graphene.Int(
description=(
"Default number of minutes stock will be reserved for "
"anonymous checkout. Enter 0 or null to disable." + ADDED_IN_31
)
)
reserve_stock_duration_authenticated_user = graphene.Int(
description=(
"Default number of minutes stock will be reserved for "
"authenticated checkout. Enter 0 or null to disable." + ADDED_IN_31
)
)
limit_quantity_per_checkout = graphene.Int(
description=(
"Default number of maximum line quantity "
"in single checkout. Minimum possible value is 1, default "
f"value is {DEFAULT_LIMIT_QUANTITY_PER_CHECKOUT}."
+ ADDED_IN_31
+ PREVIEW_FEATURE
)
)
# deprecated
include_taxes_in_prices = graphene.Boolean(
description=(
f"Include taxes in prices. {DEPRECATED_IN_3X_INPUT} Use "
"`taxConfigurationUpdate` mutation to configure this setting per channel "
"or country."
)
)
display_gross_prices = graphene.Boolean(
description=(
f"Display prices with tax in store. {DEPRECATED_IN_3X_INPUT} Use "
"`taxConfigurationUpdate` mutation to configure this setting per channel "
"or country."
)
)
charge_taxes_on_shipping = graphene.Boolean(
description=(
f"Charge taxes on shipping. {DEPRECATED_IN_3X_INPUT} To enable taxes for "
"a shipping method, assign a tax class to the shipping method with "
"`shippingPriceCreate` or `shippingPriceUpdate` mutations."
),
)
class SiteDomainInput(graphene.InputObjectType):
domain = graphene.String(description="Domain name for shop.")
name = graphene.String(description="Shop site name.")
class ShopSettingsUpdate(BaseMutation):
shop = graphene.Field(Shop, description="Updated shop.")
class Arguments:
input = ShopSettingsInput(
description="Fields required to update shop settings.", required=True
)
class Meta:
description = "Updates shop settings."
permissions = (SitePermissions.MANAGE_SETTINGS,)
error_type_class = ShopError
error_type_field = "shop_errors"
@classmethod
def clean_input(cls, _info, _instance, data):
if data.get("customer_set_password_url"):
try:
validate_storefront_url(data["customer_set_password_url"])
except ValidationError as error:
raise ValidationError(
{"customer_set_password_url": error}, code=ShopErrorCode.INVALID
)
if "reserve_stock_duration_anonymous_user" in data:
new_value = data["reserve_stock_duration_anonymous_user"]
if not new_value or new_value < 1:
data["reserve_stock_duration_anonymous_user"] = None
if "reserve_stock_duration_authenticated_user" in data:
new_value = data["reserve_stock_duration_authenticated_user"]
if not new_value or new_value < 1:
data["reserve_stock_duration_authenticated_user"] = None
if "limit_quantity_per_checkout" in data:
new_value = data["limit_quantity_per_checkout"]
if new_value is not None and new_value < 1:
raise ValidationError(
{
"limit_quantity_per_checkout": ValidationError(
"Quantity limit cannot be lower than 1.",
code=ShopErrorCode.INVALID.value,
)
}
)
if not new_value:
data["limit_quantity_per_checkout"] = None
return data
@classmethod
def construct_instance(cls, instance, cleaned_data):
for field_name, desired_value in cleaned_data.items():
current_value = getattr(instance, field_name)
if current_value != desired_value:
setattr(instance, field_name, desired_value)
return instance
@classmethod
def perform_mutation(cls, _root, info, **data):
site = get_site_promise(info.context).get()
instance = site.settings
data = data.get("input")
cleaned_input = cls.clean_input(info, instance, data)
instance = cls.construct_instance(instance, cleaned_input)
cls.clean_instance(info, instance)
instance.save()
return ShopSettingsUpdate(shop=Shop())
class ShopAddressUpdate(BaseMutation, I18nMixin):
shop = graphene.Field(Shop, description="Updated shop.")
class Arguments:
input = AddressInput(description="Fields required to update shop address.")
class Meta:
description = (
"Update the shop's address. If the `null` value is passed, the currently "
"selected address will be deleted."
)
permissions = (SitePermissions.MANAGE_SETTINGS,)
error_type_class = ShopError
error_type_field = "shop_errors"
@classmethod
def perform_mutation(cls, _root, info, **data):
site = get_site_promise(info.context).get()
data = data.get("input")
if data:
if not site.settings.company_address:
company_address = account_models.Address()
else:
company_address = site.settings.company_address
company_address = cls.validate_address(
data, instance=company_address, info=info
)
company_address.save()
site.settings.company_address = company_address
site.settings.save(update_fields=["company_address"])
else:
if site.settings.company_address:
site.settings.company_address.delete()
return ShopAddressUpdate(shop=Shop())
class ShopDomainUpdate(BaseMutation):
shop = graphene.Field(Shop, description="Updated shop.")
class Arguments:
input = SiteDomainInput(description="Fields required to update site.")
class Meta:
description = "Updates site domain of the shop."
permissions = (SitePermissions.MANAGE_SETTINGS,)
error_type_class = ShopError
error_type_field = "shop_errors"
@classmethod
def perform_mutation(cls, _root, info, **data):
site = get_site_promise(info.context).get()
data = data.get("input")
domain = data.get("domain")
name = data.get("name")
if domain is not None:
site.domain = domain
if name is not None:
site.name = name
cls.clean_instance(info, site)
site.save()
return ShopDomainUpdate(shop=Shop())
class ShopFetchTaxRates(BaseMutation):
shop = graphene.Field(Shop, description="Updated shop.")
class Meta:
description = "Fetch tax rates."
permissions = (SitePermissions.MANAGE_SETTINGS,)
error_type_class = ShopError
error_type_field = "shop_errors"
@classmethod
def perform_mutation(cls, _root, info):
# This mutation is deprecated and will be removed in Saleor 4.0.
return ShopFetchTaxRates(shop=Shop())
class StaffNotificationRecipientInput(graphene.InputObjectType):
user = graphene.ID(
required=False,
description="The ID of the user subscribed to email notifications..",
)
email = graphene.String(
required=False,
description="Email address of a user subscribed to email notifications.",
)
active = graphene.Boolean(
required=False, description="Determines if a notification active."
)
class StaffNotificationRecipientCreate(ModelMutation):
class Arguments:
input = StaffNotificationRecipientInput(
required=True,
description="Fields required to create a staff notification recipient.",
)
class Meta:
description = "Creates a new staff notification recipient."
model = account_models.StaffNotificationRecipient
object_type = StaffNotificationRecipient
permissions = (SitePermissions.MANAGE_SETTINGS,)
error_type_class = ShopError
error_type_field = "shop_errors"
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
cls.validate_input(instance, cleaned_input)
email = cleaned_input.pop("email", None)
if email:
staff_user = account_models.User.objects.filter(email=email).first()
if staff_user:
cleaned_input["user"] = staff_user
else:
cleaned_input["staff_email"] = email
return cleaned_input
@staticmethod
def validate_input(instance, cleaned_input):
email = cleaned_input.get("email")
user = cleaned_input.get("user")
if not email and not user:
if instance.id and "user" in cleaned_input or "email" in cleaned_input:
raise ValidationError(
{
"staff_notification": ValidationError(
"User and email cannot be set empty",
code=ShopErrorCode.INVALID,
)
}
)
if not instance.id:
raise ValidationError(
{
"staff_notification": ValidationError(
"User or email is required", code=ShopErrorCode.REQUIRED
)
}
)
if user and not user.is_staff:
raise ValidationError(
{
"user": ValidationError(
"User has to be staff user", code=ShopErrorCode.INVALID
)
}
)
class StaffNotificationRecipientUpdate(StaffNotificationRecipientCreate):
class Arguments:
id = graphene.ID(
required=True, description="ID of a staff notification recipient to update."
)
input = StaffNotificationRecipientInput(
required=True,
description="Fields required to update a staff notification recipient.",
)
class Meta:
description = "Updates a staff notification recipient."
model = account_models.StaffNotificationRecipient
object_type = StaffNotificationRecipient
permissions = (SitePermissions.MANAGE_SETTINGS,)
error_type_class = ShopError
error_type_field = "shop_errors"
class StaffNotificationRecipientDelete(ModelDeleteMutation):
class Arguments:
id = graphene.ID(
required=True, description="ID of a staff notification recipient to delete."
)
class Meta:
description = "Delete staff notification recipient."
model = account_models.StaffNotificationRecipient
object_type = StaffNotificationRecipient
permissions = (SitePermissions.MANAGE_SETTINGS,)
error_type_class = ShopError
error_type_field = "shop_errors"
class OrderSettingsUpdateInput(graphene.InputObjectType):
automatically_confirm_all_new_orders = graphene.Boolean(
required=False,
description="When disabled, all new orders from checkout "
"will be marked as unconfirmed. When enabled orders from checkout will "
"become unfulfilled immediately.",
)
automatically_fulfill_non_shippable_gift_card = graphene.Boolean(
required=False,
description="When enabled, all non-shippable gift card orders "
"will be fulfilled automatically.",
)
class OrderSettingsUpdate(BaseMutation):
order_settings = graphene.Field(OrderSettings, description="Order settings.")
class Arguments:
input = OrderSettingsUpdateInput(
required=True, description="Fields required to update shop order settings."
)
class Meta:
description = "Update shop order settings."
permissions = (OrderPermissions.MANAGE_ORDERS,)
error_type_class = OrderSettingsError
error_type_field = "order_settings_errors"
@classmethod
def perform_mutation(cls, _root, info, **data):
FIELDS = [
"automatically_confirm_all_new_orders",
"automatically_fulfill_non_shippable_gift_card",
]
site = get_site_promise(info.context).get()
instance = site.settings
update_fields = {}
for field in FIELDS:
value = data["input"].get(field)
if value is not None:
setattr(instance, field, value)
update_fields[field] = value
if update_fields:
instance.save(update_fields=update_fields.keys())
channel_models.Channel.objects.update(**update_fields)
return OrderSettingsUpdate(order_settings=instance)
class GiftCardSettingsUpdateInput(graphene.InputObjectType):
expiry_type = GiftCardSettingsExpiryTypeEnum(
description="Defines gift card default expiry settings."
)
expiry_period = TimePeriodInputType(description="Defines gift card expiry period.")
class GiftCardSettingsUpdate(BaseMutation):
gift_card_settings = graphene.Field(
GiftCardSettings, description="Gift card settings."
)
class Arguments:
input = GiftCardSettingsUpdateInput(
required=True, description="Fields required to update gift card settings."
)
class Meta:
description = "Update gift card settings."
permissions = (GiftcardPermissions.MANAGE_GIFT_CARD,)
error_type_class = GiftCardSettingsError
@classmethod
def perform_mutation(cls, _root, info, **data):
site = get_site_promise(info.context).get()
instance = site.settings
input = data["input"]
cls.clean_input(input, instance)
expiry_period = input.get("expiry_period")
instance.gift_card_expiry_period_type = (
expiry_period["type"] if expiry_period else None
)
instance.gift_card_expiry_period = (
expiry_period["amount"] if expiry_period else None
)
update_fields = ["gift_card_expiry_period", "gift_card_expiry_period_type"]
if expiry_type := input.get("expiry_type"):
instance.gift_card_expiry_type = expiry_type
update_fields.append("gift_card_expiry_type")
instance.save(update_fields=update_fields)
return GiftCardSettingsUpdate(gift_card_settings=instance)
@staticmethod
def clean_input(input, instance):
expiry_type = input.get("expiry_type") or instance.gift_card_expiry_type
if (
expiry_type == GiftCardSettingsExpiryType.EXPIRY_PERIOD
and input.get("expiry_period") is None
):
raise ValidationError(
{
"expiry_period": ValidationError(
"Expiry period settings are required for expiry period "
"gift card settings.",
code=GiftCardSettingsErrorCode.REQUIRED.value,
)
}
)
elif expiry_type == GiftCardSettingsExpiryType.NEVER_EXPIRE:
input["expiry_period"] = None