Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Functions naming refactor and docstrings
Anna Siwika8074145bad8
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.pyindex a954bee7fa..d1e9516a0c 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 fill_group_with_partition+from ..order.utils import delivery_group_fill_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)- fill_group_with_partition(+ delivery_group_fill_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 ddee1c5f72..8c04aeec18 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -15,8 +15,9 @@ from ...discount.models import Voucherfrom ...order import OrderStatusfrom ...order.models import DeliveryGroup, Order, OrderLine, OrderNotefrom ...order.utils import (- add_variant_to_delivery_group, cancel_order, cancel_delivery_group,- change_order_line_quantity, merge_duplicated_lines, order_recalculate)+ delivery_group_add_variant, delivery_group_cancel, order_cancel,+ order_line_change_quantity, order_line_merge_with_duplicates,+ order_recalculate)from ...product.models import Product, ProductVariant, Stock@@ -179,7 +180,7 @@ class ChangeQuantityForm(forms.ModelForm):# update stock allocationdelta = quantity - self.initial_quantityStock.objects.allocate_stock(stock, delta)- change_order_line_quantity(self.instance, quantity)+ order_line_change_quantity(self.instance, quantity)order_recalculate(self.instance.delivery_group.order)return self.instance@@ -225,7 +226,7 @@ class CancelGroupForm(forms.Form):super(CancelGroupForm, self).__init__(*args, **kwargs)def cancel_group(self):- cancel_delivery_group(self.delivery_group)+ delivery_group_cancel(self.delivery_group)class CancelOrderForm(forms.Form):@@ -243,8 +244,8 @@ class CancelOrderForm(forms.Form):'This order can\'t be cancelled'))return data- def cancel_order(self):- cancel_order(self.order)+ def order_cancel(self):+ order_cancel(self.order)class RemoveVoucherForm(forms.Form):@@ -323,7 +324,7 @@ class ChangeStockForm(forms.ModelForm):stock.location.name if stock.location else '')Stock.objects.allocate_stock(stock, quantity)super(ChangeStockForm, self).save(commit)- merge_duplicated_lines(self.instance)+ order_line_merge_with_duplicates(self.instance)return self.instance@@ -364,5 +365,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')- add_variant_to_delivery_group(self.group, variant, quantity)+ delivery_group_add_variant(self.group, variant, quantity)order_recalculate(self.group.order)diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.pyindex 7b492157db..9736bd8b51 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.cancel_order, name='order-cancel'),+ views.order_cancel, 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.cancel_delivery_group, name='cancel-delivery-group'),+ views.delivery_group_cancel, 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 e58c536e51..f35acb8cb4 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -278,7 +278,7 @@ def ship_delivery_group(request, order_pk, group_pk):@staff_member_required@permission_required('order.edit_order')-def cancel_delivery_group(request, order_pk, group_pk):+def delivery_group_cancel(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)@@ -361,14 +361,14 @@ def address_view(request, order_pk, address_type):@staff_member_required@permission_required('order.edit_order')-def cancel_order(request, order_pk):+def order_cancel(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.cancel_order()+ form.order_cancel()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 80c6b647cd..163ceb0557 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -22,6 +22,8 @@ logger = logging.getLogger(__name__)def check_order_status(func):+ """Preserves execution of function if order is fully paid by redirecting+ to order's details page."""@wraps(func)def decorator(*args, **kwargs):token = kwargs.pop('token')@@ -36,6 +38,7 @@ def check_order_status(func):@receiver(status_changed)def order_status_change(sender, instance, **kwargs):+ """Handles payment status change and sets suitable order status."""order = instance.orderif order.is_fully_paid():order.status = OrderStatus.FULLY_PAID@@ -51,7 +54,17 @@ def order_status_change(sender, instance, **kwargs):logger.exception('Recording order in analytics failed')+def order_cancel(order):+ """Cancells order by cancelling all associated shipment groups."""+ for group in order.groups.all():+ delivery_group_cancel(group, cancel_order=False)+ order.status = OrderStatus.CANCELLED+ order.save()++def order_recalculate(order):+ """Recalculates and assigns total price of order.+ Total price is a sum of items and shippings in order shipment groups. """prices = [group.get_total() for group in orderif group.status != OrderStatus.CANCELLED]@@ -69,7 +82,8 @@ def order_recalculate(order):order.save()-def attach_order_to_user(order, user):+def order_attach_to_user(order, user):+ """Associates existing order with user account."""order.user = userstore_user_address(user, order.billing_address, billing=True)if order.shipping_address:@@ -77,20 +91,18 @@ def attach_order_to_user(order, user):order.save(update_fields=['user'])-def fill_group_with_partition(group, partition, discounts=None):- """- Fills shipment group with order lines created from partition items.+def delivery_group_fill_with_partition(group, partition, discounts=None):+ """Fills shipment group with order lines created from partition items."""for item in partition:- add_variant_to_delivery_group(+ delivery_group_add_variant(group, item.variant, item.get_quantity(), discounts,add_to_existing=False)-def add_variant_to_delivery_group(+def delivery_group_add_variant(group, variant, total_quantity, discounts=None, add_to_existing=True):- """- Adds total_quantity of variant to group.+ """Adds total_quantity of variant to group.Raises InsufficientStock exception if quantity could not be fulfilled.By default, first adds variant to existing lines with same variant.@@ -100,7 +112,7 @@ def add_variant_to_delivery_group(as long as total_quantity of variant will be added."""quantity_left = (- add_variant_to_existing_lines(group, variant, total_quantity)+ delivery_group_add_variant_to_existing_lines(group, variant, total_quantity)if add_to_existing else total_quantity)price = variant.get_price_per_item(discounts)while quantity_left > 0:@@ -127,9 +139,8 @@ def add_variant_to_delivery_group(quantity_left -= quantity-def add_variant_to_existing_lines(group, variant, total_quantity):- """- Adds variant to existing lines with same variant.+def delivery_group_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,as long as total_quantity of variant will be added@@ -158,7 +169,8 @@ def add_variant_to_existing_lines(group, variant, total_quantity):return quantity_left-def cancel_delivery_group(group, cancel_order=True):+def delivery_group_cancel(group, cancel_order=True):+ """Cancells shipment group and (optionally) it's order if necessary."""for line in group:if line.stock:Stock.objects.deallocate_stock(line.stock, line.quantity)@@ -173,15 +185,8 @@ def cancel_delivery_group(group, cancel_order=True):group.order.save(update_fields=['status'])-def cancel_order(order):- for group in order.groups.all():- cancel_delivery_group(group, cancel_order=False)- order.status = OrderStatus.CANCELLED- order.save()---def merge_duplicated_lines(line):- """ Merges duplicated lines in shipment group into one (given) line.+def order_line_merge_with_duplicates(line):+ """Merges duplicated lines in shipment group into one (given) line.If there are no duplicates, nothing will happen."""lines = line.delivery_group.lines.filter(@@ -193,7 +198,7 @@ def merge_duplicated_lines(line):lines.exclude(pk=line.pk).delete()-def change_order_line_quantity(line, new_quantity):+def order_line_change_quantity(line, new_quantity):"""Change the quantity of ordered items in a order line."""line.quantity = new_quantityline.save()diff --git a/saleor/order/views.py b/saleor/order/views.pyindex 995ade1437..22344ec1fc 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, attach_order_to_user+from .utils import check_order_status, order_attach_to_userfrom ..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)- attach_order_to_user(order, user)+ order_attach_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))- attach_order_to_user(order, request.user)+ order_attach_to_user(order, request.user)messages.success(request, pgettext_lazy('storefront message',diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex b685d62386..7312c3b611 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 (- add_variant_to_existing_lines, change_order_line_quantity,- fill_group_with_partition)+ delivery_group_add_variant_to_existing_lines, order_line_change_quantity,+ delivery_group_fill_with_partition)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_cancel_order_line(admin_client, order_with_lines_and_stock):+def test_view_order_cancel_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_cancel_order_line(admin_client, order_with_lines_and_stock):@pytest.mark.integration@pytest.mark.django_db-def test_view_change_order_line_quantity(admin_client, order_with_lines_and_stock):+def test_view_order_line_change_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_change_order_line_quantity(admin_client, order_with_lines_and_stoc@pytest.mark.integration@pytest.mark.django_db-def test_view_change_order_line_quantity_with_invalid_data(+def test_view_order_line_change_quantity_with_invalid_data(admin_client, order_with_lines_and_stock):"""user goes to order details page@@ -129,7 +129,7 @@ def test_view_change_order_line_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)- fill_group_with_partition(group, cart.lines.all())+ delivery_group_fill_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()- change_order_line_quantity(lines[0], 0)- change_order_line_quantity(lines[1], 0)- change_order_line_quantity(lines[2], 0)+ order_line_change_quantity(lines[0], 0)+ order_line_change_quantity(lines[1], 0)+ order_line_change_quantity(lines[2], 0)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_add_variant_to_existing_lines_one_line(+def test_delivery_group_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_add_variant_to_existing_lines_one_line(quantity_before = line.quantityvariant = ProductVariant.objects.get(sku='SKU_A')- quantity_left = add_variant_to_existing_lines(+ quantity_left = delivery_group_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_add_variant_to_existing_lines_one_line(assert line.quantity == 4-def test_add_variant_to_existing_lines_multiple_lines(+def test_delivery_group_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_add_variant_to_existing_lines_multiple_lines(line_2 = lines.get(stock_location='Warehouse 2')variant = ProductVariant.objects.get(sku='SKU_A')- quantity_left = add_variant_to_existing_lines(+ quantity_left = delivery_group_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_add_variant_to_existing_lines_multiple_lines(assert line_2.quantity == 5-def test_add_variant_to_existing_lines_multiple_lines_with_rest(+def test_delivery_group_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_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 = add_variant_to_existing_lines(+ quantity_left = delivery_group_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_add_variant_to_existing_lines_multiple_lines_with_rest(assert line_2.quantity == 5-def test_view_add_variant_to_delivery_group(+def test_view_delivery_group_add_variant(admin_client, order_with_variant_from_different_stocks):order = order_with_variant_from_different_stocksgroup = order.groups.get()diff --git a/tests/test_order.py b/tests/test_order.pyindex d7d35fe0dc..c9131d74f4 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 (- add_variant_to_delivery_group, fill_group_with_partition)+ delivery_group_add_variant, delivery_group_fill_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)- fill_group_with_partition(delivery_group, cart.lines.all())+ delivery_group_fill_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)- fill_group_with_partition(+ delivery_group_fill_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_add_variant_to_delivery_group_adds_line_for_new_variant(+def test_delivery_group_add_variant_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()- add_variant_to_delivery_group(group, variant, 1)+ delivery_group_add_variant(group, variant, 1)line = group.lines.last()assert group.lines.count() == lines_before + 1@@ -64,20 +64,20 @@ def test_add_variant_to_delivery_group_adds_line_for_new_variant(assert line.quantity == 1-def test_add_variant_to_delivery_group_allocates_stock_for_new_variant(+def test_delivery_group_add_variant_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- add_variant_to_delivery_group(group, variant, 1)+ delivery_group_add_variant(group, variant, 1)stock.refresh_from_db()assert stock.quantity_allocated == stock_before + 1-def test_add_variant_to_delivery_group_edits_line_for_existing_variant(+def test_delivery_group_add_variant_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_add_variant_to_delivery_group_edits_line_for_existing_variant(lines_before = group.lines.count()line_quantity_before = existing_line.quantity- add_variant_to_delivery_group(group, variant, 1)+ delivery_group_add_variant(group, variant, 1)existing_line.refresh_from_db()assert group.lines.count() == lines_before@@ -94,7 +94,7 @@ def test_add_variant_to_delivery_group_edits_line_for_existing_variant(assert existing_line.quantity == line_quantity_before + 1-def test_add_variant_to_delivery_group_allocates_stock_for_existing_variant(+def test_delivery_group_add_variant_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_add_variant_to_delivery_group_allocates_stock_for_existing_variant(stock = existing_line.stockstock_before = stock.quantity_allocated- add_variant_to_delivery_group(group, variant, 1)+ delivery_group_add_variant(group, variant, 1)stock.refresh_from_db()assert stock.quantity_allocated == stock_before + 1