Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Name functions more natural
Anna Siwike48d03454a54
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.pyindex d1e9516a0c..a954bee7fa 100644--- a/saleor/checkout/core.py+++ b/saleor/checkout/core.py@@ -16,7 +16,7 @@ from ..cart.utils import get_or_empty_db_cartfrom ..core import analyticsfrom ..discount.models import NotApplicable, Voucherfrom ..order.models import Order-from ..order.utils import delivery_group_fill_with_partition+from ..order.utils import fill_group_with_partitionfrom ..shipping.models import ANY_COUNTRY, ShippingMethodCountryfrom ..userprofile.models import Addressfrom ..userprofile.utils import store_user_address@@ -331,7 +331,7 @@ class Checkout(object):group = order.groups.create(shipping_price=shipping_price,shipping_method_name=shipping_method_name)- delivery_group_fill_with_partition(+ fill_group_with_partition(group, partition, discounts=self.cart.discounts)if voucher is not None:diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex a48167a99e..14253eb22d 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -15,10 +15,10 @@ from ...discount.models import Voucherfrom ...order import OrderStatusfrom ...order.models import DeliveryGroup, OrderLine, OrderNotefrom ...order.utils import (- delivery_group_add_variant, delivery_group_cancel, order_cancel,- order_line_change_quantity, order_line_merge_with_duplicates,- order_line_move_to_group, order_line_remove_empty_groups,- order_recalculate)+ add_variant_to_delivery_group, cancel_delivery_group, cancel_order,+ change_order_line_quantity, merge_duplicates_into_order_line,+ move_order_line_to_group, recalculate_order, remove_empty_groups+)from ...product.models import Product, ProductVariant, Stock@@ -127,7 +127,7 @@ class MoveLinesForm(forms.Form):target_group = self.old_group.order.groups.create(status=self.old_group.status,shipping_method_name=self.old_group.shipping_method_name)- order_line_move_to_group(self.line, target_group, how_many)+ move_order_line_to_group(self.line, target_group, how_many)return target_group@@ -142,8 +142,8 @@ class CancelLinesForm(forms.Form):Stock.objects.deallocate_stock(self.line.stock, self.line.quantity)order = self.line.delivery_group.orderself.line.quantity = 0- order_line_remove_empty_groups(self.line)- order_recalculate(order)+ remove_empty_groups(self.line)+ recalculate_order(order)class ChangeQuantityForm(forms.ModelForm):@@ -181,8 +181,8 @@ class ChangeQuantityForm(forms.ModelForm):# update stock allocationdelta = quantity - self.initial_quantityStock.objects.allocate_stock(stock, delta)- order_line_change_quantity(self.instance, quantity)- order_recalculate(self.instance.delivery_group.order)+ change_order_line_quantity(self.instance, quantity)+ recalculate_order(self.instance.delivery_group.order)return self.instance@@ -227,7 +227,7 @@ class CancelGroupForm(forms.Form):super(CancelGroupForm, self).__init__(*args, **kwargs)def cancel_group(self):- delivery_group_cancel(self.delivery_group)+ cancel_delivery_group(self.delivery_group)class CancelOrderForm(forms.Form):@@ -245,8 +245,8 @@ class CancelOrderForm(forms.Form):'This order can\'t be cancelled'))return data- def order_cancel(self):- order_cancel(self.order)+ def cancel_order(self):+ cancel_order(self.order)class RemoveVoucherForm(forms.Form):@@ -270,7 +270,7 @@ class RemoveVoucherForm(forms.Form):voucher = self.order.voucherVoucher.objects.decrease_usage(voucher)self.order.voucher = None- order_recalculate(self.order)+ recalculate_order(self.order)ORDER_STATUS_CHOICES = [@@ -325,7 +325,7 @@ class ChangeStockForm(forms.ModelForm):stock.location.name if stock.location else '')Stock.objects.allocate_stock(stock, quantity)super(ChangeStockForm, self).save(commit)- order_line_merge_with_duplicates(self.instance)+ merge_duplicates_into_order_line(self.instance)return self.instance@@ -366,5 +366,5 @@ class AddVariantToDeliveryGroupForm(forms.Form):""" Adds variant to target group. Updates stocks and order. """variant = self.cleaned_data.get('variant')quantity = self.cleaned_data.get('quantity')- delivery_group_add_variant(self.group, variant, quantity)- order_recalculate(self.group.order)+ add_variant_to_delivery_group(self.group, variant, quantity)+ recalculate_order(self.group.order)diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.pyindex 9736bd8b51..7b492157db 100644--- a/saleor/dashboard/order/urls.py+++ b/saleor/dashboard/order/urls.py@@ -12,7 +12,7 @@ urlpatterns = [url(r'^(?P<order_pk>\d+)/add-note/$',views.order_add_note, name='order-add-note'),url(r'^(?P<order_pk>\d+)/cancel/$',- views.order_cancel, name='order-cancel'),+ views.cancel_order, name='order-cancel'),url(r'^(?P<order_pk>\d+)/address/(?P<address_type>billing|shipping)/$',views.address_view, name='address-edit'),@@ -36,7 +36,7 @@ urlpatterns = [url(r'^(?P<order_pk>\d+)/shipment/(?P<group_pk>\d+)/ship/$',views.ship_delivery_group, name='ship-delivery-group'),url(r'^(?P<order_pk>\d+)/shipment/(?P<group_pk>\d+)/cancel/$',- views.delivery_group_cancel, name='cancel-delivery-group'),+ views.cancel_delivery_group, name='cancel-delivery-group'),url(r'^(?P<order_pk>\d+)/shipment/(?P<group_pk>\d+)/add-variant/$',views.add_variant_to_group, name='add-variant-to-group'),url(r'^(?P<order_pk>\d+)/invoice/$',diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex a4534d9614..1227559f62 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -277,7 +277,7 @@ def ship_delivery_group(request, order_pk, group_pk):@staff_member_required@permission_required('order.edit_order')-def delivery_group_cancel(request, order_pk, group_pk):+def cancel_delivery_group(request, order_pk, group_pk):order = get_object_or_404(Order, pk=order_pk)group = get_object_or_404(order.groups.all(), pk=group_pk)form = CancelGroupForm(request.POST or None, delivery_group=group)@@ -360,14 +360,14 @@ def address_view(request, order_pk, address_type):@staff_member_required@permission_required('order.edit_order')-def order_cancel(request, order_pk):+def cancel_order(request, order_pk):status = 200order = get_object_or_404(Order, pk=order_pk)form = CancelOrderForm(request.POST or None, order=order)if form.is_valid():msg = pgettext_lazy('Dashboard message', 'Cancelled order')with transaction.atomic():- form.order_cancel()+ form.cancel_order()order.create_history_entry(comment=msg, user=request.user)messages.success(request, 'Order cancelled')return redirect('dashboard:order-details', order_pk=order.pk)diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 946dc78c2e..b9798fd5d5 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -54,15 +54,15 @@ def order_status_change(sender, instance, **kwargs):logger.exception('Recording order in analytics failed')-def order_cancel(order):+def cancel_order(order):"""Cancells order by cancelling all associated shipment groups."""for group in order.groups.all():- delivery_group_cancel(group, cancel_order=False)+ cancel_delivery_group(group, cancel_order=False)order.status = OrderStatus.CANCELLEDorder.save()-def order_recalculate(order):+def recalculate_order(order):"""Recalculates and assigns total price of order.Total price is a sum of items and shippings in order shipment groups. """prices = [@@ -82,7 +82,7 @@ def order_recalculate(order):order.save()-def order_attach_to_user(order, user):+def attach_order_to_user(order, user):"""Associates existing order with user account."""order.user = userstore_user_address(user, order.billing_address, billing=True)@@ -91,16 +91,16 @@ def order_attach_to_user(order, user):order.save(update_fields=['user'])-def delivery_group_fill_with_partition(group, partition, discounts=None):+def fill_group_with_partition(group, partition, discounts=None):"""Fills shipment group with order lines created from partition items."""for item in partition:- delivery_group_add_variant(+ add_variant_to_delivery_group(group, item.variant, item.get_quantity(), discounts,add_to_existing=False)-def delivery_group_add_variant(+def add_variant_to_delivery_group(group, variant, total_quantity, discounts=None, add_to_existing=True):"""Adds total_quantity of variant to group.Raises InsufficientStock exception if quantity could not be fulfilled.@@ -112,7 +112,7 @@ def delivery_group_add_variant(as long as total_quantity of variant will be added."""quantity_left = (- delivery_group_add_variant_to_existing_lines(+ add_variant_to_existing_lines(group, variant, total_quantity) if add_to_existingelse total_quantity)price = variant.get_price_per_item(discounts)@@ -140,8 +140,7 @@ def delivery_group_add_variant(quantity_left -= quantity-def delivery_group_add_variant_to_existing_lines(- group, variant, total_quantity):+def add_variant_to_existing_lines(group, variant, total_quantity):"""Adds variant to existing lines with same variant.Variant is added by increasing quantity of lines with same variant,@@ -171,7 +170,7 @@ def delivery_group_add_variant_to_existing_lines(return quantity_left-def delivery_group_cancel(group, cancel_order=True):+def cancel_delivery_group(group, cancel_order=True):"""Cancells shipment group and (optionally) it's order if necessary."""for line in group:if line.stock:@@ -187,7 +186,7 @@ def delivery_group_cancel(group, cancel_order=True):group.order.save(update_fields=['status'])-def order_line_merge_with_duplicates(line):+def merge_duplicates_into_order_line(line):"""Merges duplicated lines in shipment group into one (given) line.If there are no duplicates, nothing will happen."""@@ -200,7 +199,7 @@ def order_line_merge_with_duplicates(line):lines.exclude(pk=line.pk).delete()-def order_line_change_quantity(line, new_quantity):+def change_order_line_quantity(line, new_quantity):"""Change the quantity of ordered items in a order line."""line.quantity = new_quantityline.save()@@ -217,9 +216,9 @@ def order_line_change_quantity(line, new_quantity):'Order cancelled. No items in order'))-def order_line_remove_empty_groups(line, force=False):+def remove_empty_groups(line, force=False):"""Removes order line and associated shipment group and order.- Remove is done only if quantity of order line or items in group and order+ Remove is done only if quantity of order line or items in group or in orderis equal to 0."""source_group = line.delivery_grouporder = source_group.order@@ -238,7 +237,7 @@ def order_line_remove_empty_groups(line, force=False):'Order cancelled. No items in order'))-def order_line_move_to_group(line, target_group, quantity):+def move_order_line_to_group(line, target_group, quantity):"""Moves given quantity of order line to another shipment group."""try:target_line = target_group.lines.get(@@ -256,4 +255,4 @@ def order_line_move_to_group(line, target_group, quantity):target_line.quantity += quantitytarget_line.save()line.quantity -= quantity- order_line_remove_empty_groups(line)+ remove_empty_groups(line)diff --git a/saleor/order/views.py b/saleor/order/views.pyindex 22344ec1fc..ca543f7249 100644--- a/saleor/order/views.py+++ b/saleor/order/views.py@@ -14,7 +14,7 @@ from payments import PaymentStatus, RedirectNeededfrom .forms import PaymentDeleteForm, PaymentMethodsForm, PasswordFormfrom .models import Order, Payment-from .utils import check_order_status, order_attach_to_user+from .utils import attach_order_to_user, check_order_statusfrom ..core.utils import get_client_ipfrom ..registration.forms import LoginFormfrom ..userprofile.models import User@@ -148,7 +148,7 @@ def create_password(request, token):user = auth.authenticate(request=request, email=email,password=password)auth.login(request, user)- order_attach_to_user(order, user)+ attach_order_to_user(order, user)return redirect('order:details', token=token)ctx = {'form': register_form, 'email': email, 'order': order,'login_form': login_form}@@ -159,7 +159,7 @@ def create_password(request, token):def connect_order_with_user(request, token):order = get_object_or_404(Order.objects.filter(user_email=request.user.email, token=token))- order_attach_to_user(order, request.user)+ attach_order_to_user(order, request.user)messages.success(request, pgettext_lazy('storefront message',diff --git a/tests/conftest.py b/tests/conftest.pyindex af15b85e0d..f7461956c7 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -20,7 +20,7 @@ from saleor.cart.models import Cartfrom saleor.checkout.core import Checkoutfrom saleor.discount.models import Voucher, Salefrom saleor.order.models import Order, OrderLine, DeliveryGroup-from saleor.order.utils import order_recalculate+from saleor.order.utils import recalculate_orderfrom saleor.product.models import (AttributeChoiceValue, Category, Product, ProductAttribute, ProductClass,ProductVariant, ProductImage, Stock, StockLocation)@@ -441,7 +441,7 @@ def order_with_lines_and_stock(order, product_class):stock=stock,stock_location=stock.location.name)- order_recalculate(order)+ recalculate_order(order)order.refresh_from_db()return orderdiff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex 6fc362dd7c..514aaae0e3 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -10,15 +10,15 @@ from saleor.order import OrderStatusfrom saleor.order.models import (DeliveryGroup, Order, OrderLine, OrderHistoryEntry)from saleor.order.utils import (- delivery_group_add_variant_to_existing_lines, order_line_change_quantity,- delivery_group_fill_with_partition, order_line_remove_empty_groups)+ add_variant_to_existing_lines, change_order_line_quantity,+ fill_group_with_partition, remove_empty_groups)from saleor.product.models import ProductVariant, Stock, StockLocationfrom tests.utils import get_redirect_location, get_url_path@pytest.mark.integration@pytest.mark.django_db-def test_view_order_cancel_line(admin_client, order_with_lines_and_stock):+def test_view_cancel_order_line(admin_client, order_with_lines_and_stock):lines_before = order_with_lines_and_stock.get_lines()lines_before_count = lines_before.count()line = lines_before.first()@@ -60,7 +60,7 @@ def test_view_order_cancel_line(admin_client, order_with_lines_and_stock):@pytest.mark.integration@pytest.mark.django_db-def test_view_order_line_change_quantity(admin_client, order_with_lines_and_stock):+def test_view_change_order_line_quantity(admin_client, order_with_lines_and_stock):"""user goes to order details pageuser selects first order line with quantity 3 and changes it to 2@@ -108,7 +108,7 @@ def test_view_order_line_change_quantity(admin_client, order_with_lines_and_stoc@pytest.mark.integration@pytest.mark.django_db-def test_view_order_line_change_quantity_with_invalid_data(+def test_view_change_order_line_quantity_with_invalid_data(admin_client, order_with_lines_and_stock):"""user goes to order details page@@ -129,7 +129,7 @@ def test_view_order_line_change_quantity_with_invalid_data(def test_dashboard_change_quantity_form(request_cart_with_item, order):cart = request_cart_with_itemgroup = DeliveryGroup.objects.create(order=order)- delivery_group_fill_with_partition(group, cart.lines.all())+ fill_group_with_partition(group, cart.lines.all())order_line = group.lines.get()# Check max quantity validation@@ -273,9 +273,9 @@ def test_view_split_order_line_with_invalid_data(admin_client, order_with_lines_def test_ordered_item_change_quantity(transactional_db, order_with_lines):assert list(order_with_lines.history.all()) == []lines = order_with_lines.groups.all()[0].lines.all()- order_line_change_quantity(lines[0], 0)- order_line_change_quantity(lines[1], 0)- order_line_change_quantity(lines[2], 0)+ change_order_line_quantity(lines[0], 0)+ change_order_line_quantity(lines[1], 0)+ change_order_line_quantity(lines[2], 0)history = list(order_with_lines.history.all())assert len(history) == 1assert history[0].status == OrderStatus.CANCELLED@@ -286,7 +286,7 @@ def test_ordered_item_remove_empty_group_with_force(transactional_db, order_with_lines):group = order_with_lines.groups.all()[0]lines = group.lines.all()- order_line_remove_empty_groups(lines[0], force=True)+ remove_empty_groups(lines[0], force=True)history = list(order_with_lines.history.all())assert len(history) == 1assert history[0].status == OrderStatus.CANCELLED@@ -446,7 +446,7 @@ def test_view_change_order_line_stock_merges_lines(assert stock.quantity_allocated == 0-def test_delivery_group_add_variant_to_existing_lines_one_line(+def test_add_variant_to_existing_lines_one_line(order_with_variant_from_different_stocks):order = order_with_variant_from_different_stockslines = order.get_lines().filter(product_sku='SKU_A')@@ -455,7 +455,7 @@ def test_delivery_group_add_variant_to_existing_lines_one_line(quantity_before = line.quantityvariant = ProductVariant.objects.get(sku='SKU_A')- quantity_left = delivery_group_add_variant_to_existing_lines(+ quantity_left = add_variant_to_existing_lines(line.delivery_group, variant, 2)lines_after = order.get_lines().filter(product_sku='SKU_A').count()@@ -465,7 +465,7 @@ def test_delivery_group_add_variant_to_existing_lines_one_line(assert line.quantity == 4-def test_delivery_group_add_variant_to_existing_lines_multiple_lines(+def test_add_variant_to_existing_lines_multiple_lines(order_with_variant_from_different_stocks):order = order_with_variant_from_different_stockslines = order.get_lines().filter(product_sku='SKU_A')@@ -474,7 +474,7 @@ def test_delivery_group_add_variant_to_existing_lines_multiple_lines(line_2 = lines.get(stock_location='Warehouse 2')variant = ProductVariant.objects.get(sku='SKU_A')- quantity_left = delivery_group_add_variant_to_existing_lines(+ quantity_left = add_variant_to_existing_lines(line_1.delivery_group, variant, 4)lines_after = order.get_lines().filter(product_sku='SKU_A').count()@@ -486,7 +486,7 @@ def test_delivery_group_add_variant_to_existing_lines_multiple_lines(assert line_2.quantity == 5-def test_delivery_group_add_variant_to_existing_lines_multiple_lines_with_rest(+def test_add_variant_to_existing_lines_multiple_lines_with_rest(order_with_variant_from_different_stocks):order = order_with_variant_from_different_stockslines = order.get_lines().filter(product_sku='SKU_A')@@ -495,7 +495,7 @@ def test_delivery_group_add_variant_to_existing_lines_multiple_lines_with_rest(line_2 = lines.get(stock_location='Warehouse 2')variant = ProductVariant.objects.get(sku='SKU_A')- quantity_left = delivery_group_add_variant_to_existing_lines(+ quantity_left = add_variant_to_existing_lines(line_1.delivery_group, variant, 7)lines_after = order.get_lines().filter(product_sku='SKU_A').count()@@ -507,7 +507,7 @@ def test_delivery_group_add_variant_to_existing_lines_multiple_lines_with_rest(assert line_2.quantity == 5-def test_view_delivery_group_add_variant(+def test_view_add_variant_to_delivery_group(admin_client, order_with_variant_from_different_stocks):order = order_with_variant_from_different_stocksgroup = order.groups.get()diff --git a/tests/dashboard/test_permissions.py b/tests/dashboard/test_permissions.pyindex 649f1e39f0..663d28b60e 100644--- a/tests/dashboard/test_permissions.py+++ b/tests/dashboard/test_permissions.py@@ -624,7 +624,7 @@ def test_staff_group_member_can_view_order_add_note(assert response.status_code == 200-def test_staff_group_member_can_view_order_cancel(+def test_staff_group_member_can_view_cancel_order(staff_client, staff_user, staff_group, permission_edit_order,order):assert not staff_user.has_perm("order.edit_order")diff --git a/tests/test_order.py b/tests/test_order.pyindex c9131d74f4..d7d35fe0dc 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -5,7 +5,7 @@ from prices import Pricefrom saleor.cart.models import Cartfrom saleor.order import modelsfrom saleor.order.utils import (- delivery_group_add_variant, delivery_group_fill_with_partition)+ add_variant_to_delivery_group, fill_group_with_partition)def test_total_property():@@ -35,7 +35,7 @@ def test_stock_allocation(billing_address, product_in_stock):cart.add(variant, quantity=2)order = models.Order.objects.create(billing_address=billing_address)delivery_group = models.DeliveryGroup.objects.create(order=order)- delivery_group_fill_with_partition(delivery_group, cart.lines.all())+ fill_group_with_partition(delivery_group, cart.lines.all())order_line = delivery_group.lines.get()stock = order_line.stockassert stock.quantity_allocated == 2@@ -44,19 +44,19 @@ def test_stock_allocation(billing_address, product_in_stock):def test_order_discount(sale, order, request_cart_with_item):cart = request_cart_with_itemgroup = models.DeliveryGroup.objects.create(order=order)- delivery_group_fill_with_partition(+ fill_group_with_partition(group, cart.lines.all(), discounts=cart.discounts)line = group.lines.first()assert line.get_price_per_item() == Price(currency="USD", net=5)-def test_delivery_group_add_variant_adds_line_for_new_variant(+def test_add_variant_to_delivery_group_adds_line_for_new_variant(order_with_lines, product_in_stock):group = order_with_lines.groups.get()variant = product_in_stock.variants.get()lines_before = group.lines.count()- delivery_group_add_variant(group, variant, 1)+ add_variant_to_delivery_group(group, variant, 1)line = group.lines.last()assert group.lines.count() == lines_before + 1@@ -64,20 +64,20 @@ def test_delivery_group_add_variant_adds_line_for_new_variant(assert line.quantity == 1-def test_delivery_group_add_variant_allocates_stock_for_new_variant(+def test_add_variant_to_delivery_group_allocates_stock_for_new_variant(order_with_lines, product_in_stock):group = order_with_lines.groups.get()variant = product_in_stock.variants.get()stock = variant.select_stockrecord()stock_before = stock.quantity_allocated- delivery_group_add_variant(group, variant, 1)+ add_variant_to_delivery_group(group, variant, 1)stock.refresh_from_db()assert stock.quantity_allocated == stock_before + 1-def test_delivery_group_add_variant_edits_line_for_existing_variant(+def test_add_variant_to_delivery_group_edits_line_for_existing_variant(order_with_lines_and_stock):order = order_with_lines_and_stockgroup = order.groups.get()@@ -86,7 +86,7 @@ def test_delivery_group_add_variant_edits_line_for_existing_variant(lines_before = group.lines.count()line_quantity_before = existing_line.quantity- delivery_group_add_variant(group, variant, 1)+ add_variant_to_delivery_group(group, variant, 1)existing_line.refresh_from_db()assert group.lines.count() == lines_before@@ -94,7 +94,7 @@ def test_delivery_group_add_variant_edits_line_for_existing_variant(assert existing_line.quantity == line_quantity_before + 1-def test_delivery_group_add_variant_allocates_stock_for_existing_variant(+def test_add_variant_to_delivery_group_allocates_stock_for_existing_variant(order_with_lines_and_stock):order = order_with_lines_and_stockgroup = order.groups.get()@@ -103,7 +103,7 @@ def test_delivery_group_add_variant_allocates_stock_for_existing_variant(stock = existing_line.stockstock_before = stock.quantity_allocated- delivery_group_add_variant(group, variant, 1)+ add_variant_to_delivery_group(group, variant, 1)stock.refresh_from_db()assert stock.quantity_allocated == stock_before + 1