Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactored handle_inventory to track_inventory; moved and edited texts
NyanKiyoshidd75cc90cc14
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 479988045c..960be5d86d 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -386,7 +386,7 @@ class CancelOrderLineForm(forms.Form):super().__init__(*args, **kwargs)def cancel_line(self):- if self.line.variant and self.line.variant.handle_stock:+ if self.line.variant and self.line.variant.track_inventory:deallocate_stock(self.line.variant, self.line.quantity)order = self.line.orderself.line.delete()@@ -427,7 +427,7 @@ class ChangeQuantityForm(forms.ModelForm):def save(self):quantity = self.cleaned_data['quantity']variant = self.instance.variant- if variant and variant.handle_stock:+ if variant and variant.track_inventory:# update stock allocationdelta = quantity - self.initial_quantityallocate_stock(variant, delta)diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.pyindex efe462064b..b4b52079bb 100644--- a/saleor/dashboard/order/utils.py+++ b/saleor/dashboard/order/utils.py@@ -47,7 +47,7 @@ def create_packing_slip_pdf(order, fulfillment, absolute_url):def fulfill_order_line(order_line, quantity):"""Fulfill order line with given quantity."""- if order_line.variant.handle_stock:+ if order_line.variant.track_inventory:decrease_stock(order_line.variant, quantity)order_line.quantity_fulfilled += quantityorder_line.save(update_fields=['quantity_fulfilled'])diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex d638e7045e..ac1c86df9b 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -274,27 +274,31 @@ class ProductVariantForm(forms.ModelForm, AttributesMixin):class Meta:model = ProductVariantfields = [- 'sku', 'price_override', 'handle_stock', 'quantity', 'cost_price']+ 'sku', 'price_override', 'track_inventory', 'quantity', 'cost_price']labels = {'sku': pgettext_lazy('SKU', 'SKU'),'price_override': pgettext_lazy('Override price', 'Selling price override'),'quantity': pgettext_lazy('Integer number', 'Number in stock'),'cost_price': pgettext_lazy('Currency amount', 'Cost price'),- 'handle_stock': pgettext_lazy(- 'Manage stock field', 'Automatically manage the stock')}-- def __init__(self, *args, handle_stock_by_default=True, **kwargs):+ 'track_inventory': pgettext_lazy(+ 'Track inventory field', 'Track inventory')}+ help_texts = {+ 'track_inventory': pgettext_lazy(+ 'product variant handle stock field help text',+ 'Automatically track this product\'s inventory')}++ def __init__(self, *args, track_inventory_by_default=True, **kwargs):super().__init__(*args, **kwargs)- self.initial['handle_stock'] = handle_stock_by_default+ self.initial['track_inventory'] = track_inventory_by_defaultif self.instance.product.pk:self.fields['price_override'].widget.attrs['placeholder'] = self.instance.product.price.amountself.available_attributes = (self.instance.product.product_type.variant_attributes.all()- .prefetch_related('values'))+ .prefetch_related('values'))self.prepare_fields_for_attributes()if include_taxes_in_prices():@@ -328,6 +332,7 @@ class CachingModelChoiceField(forms.ModelChoiceField):if hasattr(self, '_choices'):return self._choicesreturn CachingModelChoiceIterator(self)+choices = property(_get_choices, forms.ChoiceField._set_choices)@@ -453,7 +458,7 @@ class ReorderProductImagesForm(forms.ModelForm):class UploadImageForm(forms.ModelForm):class Meta:model = ProductImage- fields = ('image', )+ fields = ('image',)labels = {'image': pgettext_lazy('Product image', 'Image')}diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex fabd57481c..bbf28ca41e 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -110,7 +110,7 @@ def product_create(request, type_pk):variant = ProductVariant(product=product)variant_form = forms.ProductVariantForm(request.POST or None,- handle_stock_by_default=site_settings.handle_stock_by_default,+ track_inventory_by_default=site_settings.track_inventory_by_default,instance=variant, prefix='variant')variant_errors = not variant_form.is_valid()else:@@ -320,7 +320,7 @@ def variant_create(request, product_pk):variant = ProductVariant(product=product)form = forms.ProductVariantForm(request.POST or None,- handle_stock_by_default=site_settings.handle_stock_by_default,+ track_inventory_by_default=site_settings.track_inventory_by_default,instance=variant)if form.is_valid():form.save()diff --git a/saleor/dashboard/sites/forms.py b/saleor/dashboard/sites/forms.pyindex e42e244e19..f554682a0f 100644--- a/saleor/dashboard/sites/forms.py+++ b/saleor/dashboard/sites/forms.py@@ -19,15 +19,20 @@ class SiteForm(forms.ModelForm):class SiteSettingsForm(forms.ModelForm):class Meta:model = SiteSettings- fields = ['header_text', 'description', 'handle_stock_by_default']+ fields = ['header_text', 'description', 'track_inventory_by_default']labels = {'header_text': pgettext_lazy('Header text', 'Header text'),'description': pgettext_lazy('Description', 'Description'),- 'handle_stock_by_default': pgettext_lazy(- 'Manage the stock by default settings toggle label',- 'Manage the stock by default')}+ 'track_inventory_by_default': pgettext_lazy(+ 'Inventory tracking by default settings toggle label',+ 'Enable inventory tracking for newly created products')}+ help_texts = {+ 'track_inventory_by_default': pgettext_lazy(+ 'handle stock by default settings field help text',+ 'This will set the default value of stock handling '+ 'on product and variant creation')}class AuthorizationKeyForm(forms.ModelForm):diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 8462e2ed2d..6c6e340128 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -165,7 +165,7 @@ def add_variant_to_order(order, variant, quantity, discounts=None, taxes=None):unit_price=variant.get_price(discounts, taxes),tax_rate=get_tax_rate_by_name(variant.product.tax_rate, taxes))- if variant.handle_stock:+ if variant.track_inventory:allocate_stock(variant, quantity)@@ -181,7 +181,7 @@ def change_order_line_quantity(line, new_quantity):def restock_order_lines(order):"""Return ordered products to corresponding stocks."""for line in order:- if line.variant and line.variant.handle_stock:+ if line.variant and line.variant.track_inventory:if line.quantity_unfulfilled > 0:deallocate_stock(line.variant, line.quantity_unfulfilled)if line.quantity_fulfilled > 0:@@ -195,6 +195,6 @@ def restock_order_lines(order):def restock_fulfillment_lines(fulfillment):"""Return fulfilled products to corresponding stocks."""for line in fulfillment:- if line.order_line.variant and line.order_line.variant.handle_stock:+ if line.order_line.variant and line.order_line.variant.track_inventory:increase_stock(line.order_line.variant, line.quantity, allocate=True)diff --git a/saleor/product/migrations/0064_productvariant_handle_stock.py b/saleor/product/migrations/0064_productvariant_handle_stock.pyindex 336277b995..ad8466d03b 100644--- a/saleor/product/migrations/0064_productvariant_handle_stock.py+++ b/saleor/product/migrations/0064_productvariant_handle_stock.py@@ -12,7 +12,7 @@ class Migration(migrations.Migration):operations = [migrations.AddField(model_name='productvariant',- name='handle_stock',- field=models.BooleanField(default=True, help_text='Allow or not to automatically handle stock units from orders'),+ name='track_inventory',+ field=models.BooleanField(default=True),),]diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 3168718e1d..b366fbc986 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -185,11 +185,7 @@ class ProductVariant(models.Model):Product, related_name='variants', on_delete=models.CASCADE)attributes = HStoreField(default={}, blank=True)images = models.ManyToManyField('ProductImage', through='VariantImage')- handle_stock = models.BooleanField(- default=True,- help_text=pgettext_lazy(- 'product variant handle stock field help text',- 'Allow or not to automatically handle stock units from orders'))+ track_inventory = models.BooleanField(default=True)quantity = models.IntegerField(validators=[MinValueValidator(0)], default=Decimal(1))quantity_allocated = models.IntegerField(@@ -212,7 +208,7 @@ class ProductVariant(models.Model):""" Check if there is at least the given quantity in stockif stock handling is enabled."""- if self.handle_stock and quantity > self.quantity_available:+ if self.track_inventory and quantity > self.quantity_available:raise InsufficientStock(self)@propertydiff --git a/saleor/site/migrations/0015_sitesettings_handle_stock_by_default.py b/saleor/site/migrations/0015_sitesettings_handle_stock_by_default.pyindex 0589e6e5f0..dc975c4599 100644--- a/saleor/site/migrations/0015_sitesettings_handle_stock_by_default.py+++ b/saleor/site/migrations/0015_sitesettings_handle_stock_by_default.py@@ -12,7 +12,7 @@ class Migration(migrations.Migration):operations = [migrations.AddField(model_name='sitesettings',- name='handle_stock_by_default',- field=models.BooleanField(default=True, help_text='This will set the default value of stock handling on product and variant creation'),+ name='track_inventory_by_default',+ field=models.BooleanField(default=True),),]diff --git a/saleor/site/models.py b/saleor/site/models.pyindex c0104640ba..df64a8d912 100644--- a/saleor/site/models.py+++ b/saleor/site/models.py@@ -22,12 +22,7 @@ class SiteSettings(models.Model):include_taxes_in_prices = models.BooleanField(default=True)display_gross_prices = models.BooleanField(default=True)charge_taxes_on_shipping = models.BooleanField(default=True)- handle_stock_by_default = models.BooleanField(- default=True,- help_text=pgettext_lazy(- 'handle stock by default settings field help text',- 'This will set the default value of stock handling '- 'on product and variant creation'))+ track_inventory_by_default = models.BooleanField(default=True)class Meta:permissions = (diff --git a/templates/dashboard/product/form.html b/templates/dashboard/product/form.htmlindex 07b4caf193..bb47b70a9d 100644--- a/templates/dashboard/product/form.html+++ b/templates/dashboard/product/form.html@@ -98,7 +98,7 @@{{ variant_form.quantity|materializecss }}</div><div class="row">- {{ variant_form.handle_stock|materializecss }}+ {{ variant_form.track_inventory|materializecss }}</div>{% endif %}<div class="row">diff --git a/templates/dashboard/product/product_variant/form.html b/templates/dashboard/product/product_variant/form.htmlindex 2aab4c0e84..29e850ef33 100644--- a/templates/dashboard/product/product_variant/form.html+++ b/templates/dashboard/product/product_variant/form.html@@ -96,7 +96,7 @@{{ form.cost_price|materializecss }}</div><div class="row">- {{ form.handle_stock|materializecss }}+ {{ form.track_inventory|materializecss }}</div></div></div>diff --git a/templates/dashboard/sites/form.html b/templates/dashboard/sites/form.htmlindex 88e318d642..fcdc9649ee 100644--- a/templates/dashboard/sites/form.html+++ b/templates/dashboard/sites/form.html@@ -50,7 +50,7 @@{{ site_settings_form.description|materializecss }}</div><div class="row">- {{ site_settings_form.handle_stock_by_default|materializecss }}+ {{ site_settings_form.track_inventory_by_default|materializecss }}</div></div><div class="card-action right-align">diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex 41e977f8bd..c5f8c375d9 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -378,15 +378,15 @@ def test_view_release_order_invalid_payment_input_status(@pytest.mark.integration[email protected]('handle_stock', (True, False))-def test_view_cancel_order_line(admin_client, draft_order, handle_stock):[email protected]('track_inventory', (True, False))+def test_view_cancel_order_line(admin_client, draft_order, track_inventory):lines_before = draft_order.lines.all()lines_before_count = lines_before.count()line = lines_before.first()line_quantity = line.quantityquantity_allocated_before = line.variant.quantity_allocated- line.variant.handle_stock = handle_stock+ line.variant.track_inventory = track_inventoryline.variant.save()url = reverse(@@ -407,7 +407,7 @@ def test_view_cancel_order_line(admin_client, draft_order, handle_stock):# check stock deallocationline.variant.refresh_from_db()- if handle_stock:+ if track_inventory:assert line.variant.quantity_allocated == (quantity_allocated_before - line_quantity)else:@@ -425,14 +425,14 @@ def test_view_cancel_order_line(admin_client, draft_order, handle_stock):@pytest.mark.integration[email protected]('handle_stock', (True, False))[email protected]('track_inventory', (True, False))def test_view_change_order_line_quantity(- admin_client, draft_order, handle_stock):+ admin_client, draft_order, track_inventory):lines_before_quantity_change = draft_order.lines.all()lines_before_quantity_change_count = lines_before_quantity_change.count()line = lines_before_quantity_change.first()- line.variant.handle_stock = handle_stock+ line.variant.track_inventory = track_inventoryline.variant.save()url = reverse(@@ -453,7 +453,7 @@ def test_view_change_order_line_quantity(assert lines_before_quantity_change_count == lines_after.count()line.variant.refresh_from_db()- if handle_stock:+ if track_inventory:# stock allocation should be 2 nowassert line.variant.quantity_allocated == 2else:diff --git a/tests/test_cart.py b/tests/test_cart.pyindex 07a006a7aa..0c69633f3c 100644--- a/tests/test_cart.py+++ b/tests/test_cart.py@@ -375,10 +375,10 @@ def test_form_when_variant_does_not_exist():assert not form.is_valid()[email protected]('handle_stock', (True, False))-def test_add_to_cart_form_when_insufficient_stock(product, handle_stock):[email protected]('track_inventory', (True, False))+def test_add_to_cart_form_when_insufficient_stock(product, track_inventory):variant = product.variants.first()- variant.handle_stock = handle_stock+ variant.track_inventory = track_inventoryvariant.save()cart_lines = []@@ -389,7 +389,7 @@ def test_add_to_cart_form_when_insufficient_stock(product, handle_stock):form = forms.AddToCartForm(data={'quantity': 1}, cart=cart, product=Mock())form.get_variant = Mock(return_value=variant)- if handle_stock:+ if track_inventory:assert not form.is_valid()else:assert form.is_valid()diff --git a/tests/test_order.py b/tests/test_order.pyindex 8a07a44438..3b57b3b885 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -81,11 +81,11 @@ def test_add_variant_to_order_adds_line_for_new_variant(assert line.tax_rate == taxes[product.tax_rate]['value'][email protected]('handle_stock', (True, False))[email protected]('track_inventory', (True, False))def test_add_variant_to_order_allocates_stock_for_new_variant(- order_with_lines, product, handle_stock):+ order_with_lines, product, track_inventory):variant = product.variants.get()- variant.handle_stock = handle_stock+ variant.track_inventory = track_inventoryvariant.save()stock_before = variant.quantity_allocated@@ -93,7 +93,7 @@ def test_add_variant_to_order_allocates_stock_for_new_variant(add_variant_to_order(order_with_lines, variant, 1)variant.refresh_from_db()- if handle_stock:+ if track_inventory:assert variant.quantity_allocated == stock_before + 1else:assert variant.quantity_allocated == stock_before@@ -170,15 +170,15 @@ def test_add_note_to_order(order_with_lines):assert order.notes.first().content == 'test_note'[email protected]('handle_stock', (True, False))-def test_restock_order_lines(order_with_lines, handle_stock):[email protected]('track_inventory', (True, False))+def test_restock_order_lines(order_with_lines, track_inventory):order = order_with_linesline_1 = order.lines.first()line_2 = order.lines.last()- line_1.variant.handle_stock = handle_stock- line_2.variant.handle_stock = handle_stock+ line_1.variant.track_inventory = track_inventory+ line_2.variant.track_inventory = track_inventoryline_1.variant.save()line_2.variant.save()@@ -194,7 +194,7 @@ def test_restock_order_lines(order_with_lines, handle_stock):line_1.variant.refresh_from_db()line_2.variant.refresh_from_db()- if handle_stock:+ if track_inventory:assert line_1.variant.quantity_allocated == (stock_1_quantity_allocated_before - line_1.quantity)assert line_2.variant.quantity_allocated == (