def clean_input(cls, info, instance, input, errors): shipping_address = input.pop('shipping_address', None) billing_address = input.pop('billing_address', None) cleaned_input = super().clean_input(info, instance, input, errors) lines = input.pop('lines', None) if lines: variant_ids = [line.get('variant_id') for line in lines] variants = get_nodes(ids=variant_ids, graphene_type=ProductVariant) quantities = [line.get('quantity') for line in lines] line_errors = check_lines_quantity(variants, quantities) if line_errors: for err in line_errors: cls.add_error(errors, field=err[0], message=err[1]) else: cleaned_input['variants'] = variants cleaned_input['quantities'] = quantities cleaned_input['status'] = OrderStatus.DRAFT display_gross_prices = info.context.site.settings.display_gross_prices cleaned_input['display_gross_prices'] = display_gross_prices # Set up default addresses if possible user = cleaned_input.get('user') if user and not shipping_address: cleaned_input[ 'shipping_address'] = user.default_shipping_address if user and not billing_address: cleaned_input[ 'billing_address'] = user.default_billing_address if shipping_address: shipping_address = Address(**shipping_address) cls.clean_instance(shipping_address, errors) cleaned_input['shipping_address'] = shipping_address if billing_address: billing_address = Address(**billing_address) cls.clean_instance(billing_address, errors) cleaned_input['billing_address'] = billing_address return cleaned_input
def perform_mutation(cls, root, info, **data): store_type_id = data.pop("store_type_id", None) data["input"]["store_type_id"] = store_type_id retval = super().perform_mutation(root, info, **data) # user = info.context.user # if not user.is_superuser: # user.store_id = retval.store.id # if user.is_authenticated: # user.save() user = User() user.is_supplier = True user.store_id = retval.store.id user.email = data["input"]["email"] user.first_name = data["input"]["first_name"] user.last_name = data["input"]["last_name"] password = data["input"]["password"] user.set_password(password) user.save() permissions = get_permissions_default() for permission in permissions: base_name = permission.codename.split("_")[1:] group_name = " ".join(base_name) group_name += " management" group_name = group_name.capitalize() cls.create_group_data(group_name, [permission], [user]) address = Address( first_name=data["input"]["first_name"], last_name=data["input"]["last_name"], ) address.save() manager = get_plugins_manager() store_user_address(user, address, AddressType.BILLING, manager) store_user_address(user, address, AddressType.SHIPPING, manager) return retval
def test_checkout_shipping_address_setter(checkout): address = Address(first_name='Jan', last_name='Kowalski') assert checkout._shipping_address is None checkout.shipping_address = address assert checkout._shipping_address == address assert checkout.storage['shipping_address'] == { 'city': '', 'city_area': '', 'company_name': '', 'country': '', 'country_area': '', 'first_name': 'Jan', 'id': None, 'last_name': 'Kowalski', 'phone': '', 'postal_code': '', 'street_address_1': '', 'street_address_2': '' }
def test_draft_order_create(staff_api_client, permission_manage_orders, customer_user, product_without_shipping, shipping_method, variant, voucher): variant_0 = variant query = """ mutation draftCreate( $user: ID, $discount: Decimal, $lines: [OrderLineCreateInput], $shippingAddress: AddressInput, $shippingMethod: ID, $voucher: ID) { draftOrderCreate( input: {user: $user, discount: $discount, lines: $lines, shippingAddress: $shippingAddress, shippingMethod: $shippingMethod, voucher: $voucher}) { errors { field message } order { discountAmount { amount } discountName lines { productName productSku quantity } status voucher { code } } } } """ user_id = graphene.Node.to_global_id('User', customer_user.id) variant_0_id = graphene.Node.to_global_id('ProductVariant', variant_0.id) variant_1 = product_without_shipping.variants.first() variant_1.quantity = 2 variant_1.save() variant_1_id = graphene.Node.to_global_id('ProductVariant', variant_1.id) discount = '10' variant_list = [{ 'variantId': variant_0_id, 'quantity': 2 }, { 'variantId': variant_1_id, 'quantity': 1 }] shipping_address = {'firstName': 'John', 'country': 'PL'} shipping_id = graphene.Node.to_global_id('ShippingMethod', shipping_method.id) voucher_id = graphene.Node.to_global_id('Voucher', voucher.id) variables = { 'user': user_id, 'discount': discount, 'lines': variant_list, 'shippingAddress': shipping_address, 'shippingMethod': shipping_id, 'voucher': voucher_id } response = staff_api_client.post_graphql( query, variables, permissions=[permission_manage_orders]) content = get_graphql_content(response) data = content['data']['draftOrderCreate']['order'] assert data['status'] == OrderStatus.DRAFT.upper() assert data['voucher']['code'] == voucher.code order = Order.objects.first() assert order.user == customer_user assert order.billing_address == customer_user.default_billing_address assert order.shipping_method == shipping_method assert order.shipping_address == Address(**{ 'first_name': 'John', 'country': 'PL' })