Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add voucher model
Michał Ociepka4da8f9a0c8ea
diff --git a/saleor/discount/migrations/0002_voucher.py b/saleor/discount/migrations/0002_voucher.pynew file mode 100644index 0000000000..4c599e2828--- /dev/null+++ b/saleor/discount/migrations/0002_voucher.py@@ -0,0 +1,38 @@+# -*- coding: utf-8 -*-+# Generated by Django 1.9.1 on 2016-02-04 17:07+from __future__ import unicode_literals++import datetime+from django.db import migrations, models+import django.db.models.deletion+import django_prices.models+++class Migration(migrations.Migration):++ dependencies = [+ ('product', '0010_auto_20160129_0826'),+ ('discount', '0001_initial'),+ ]++ operations = [+ migrations.CreateModel(+ name='Voucher',+ fields=[+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),+ ('type', models.CharField(choices=[('product', 'Product'), ('category', 'Category'), ('shipping', 'Shipping'), ('basket', 'Baskets over')], max_length=20)),+ ('name', models.CharField(blank=True, max_length=255, null=True)),+ ('code', models.CharField(db_index=True, max_length=12, unique=True)),+ ('usage_limit', models.PositiveIntegerField(blank=True, help_text='Unlimited if empty', null=True)),+ ('used', models.PositiveIntegerField(default=0, editable=False)),+ ('start_date', models.DateField(default=datetime.date.today)),+ ('end_date', models.DateField(blank=True, help_text='Never expire if empty', null=True)),+ ('discount_value_type', models.CharField(choices=[('fixed', 'USD'), ('percentage', '%')], default='fixed', max_length=10)),+ ('discount_value', models.DecimalField(decimal_places=2, max_digits=12)),+ ('apply_to', models.CharField(blank=True, max_length=20, null=True)),+ ('limit', django_prices.models.PriceField(blank=True, currency='USD', decimal_places=2, max_digits=12, null=True)),+ ('category', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='product.Category')),+ ('product', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='product.Product')),+ ],+ ),+ ]diff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex b5096ec771..3dca69802e 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -1,9 +1,11 @@from __future__ import unicode_literals+from datetime import datefrom django.conf import settingsfrom django.db import modelsfrom django.utils.translation import pgettext_lazyfrom django.utils.encoding import python_2_unicode_compatible+from django_prices.models import PriceFieldfrom prices import FixedDiscount, percentage_discount, Price@@ -11,14 +13,70 @@ class NotApplicable(ValueError):pass+@python_2_unicode_compatible+class Voucher(models.Model):++ APPLY_TO_ONE_PRODUCT = 'one'+ APPLY_TO_ALL_PRODUCTS = 'all'++ DISCOUNT_VALUE_FIXED = 'fixed'+ DISCOUNT_VALUE_PERCENTAGE = 'percentage'++ DISCOUNT_VALUE_TYPE_CHOICES = (+ (DISCOUNT_VALUE_FIXED, pgettext_lazy('voucher_model', settings.DEFAULT_CURRENCY)), # noqa+ (DISCOUNT_VALUE_PERCENTAGE, pgettext_lazy('voucher_model', '%')))++ PRODUCT_TYPE = 'product'+ CATEGORY_TYPE = 'category'+ SHIPPING_TYPE = 'shipping'+ BASKET_TYPE = 'basket'++ TYPE_CHOICES = (+ (PRODUCT_TYPE, pgettext_lazy('voucher_model', 'Product')),+ (CATEGORY_TYPE, pgettext_lazy('voucher_model', 'Category')),+ (SHIPPING_TYPE, pgettext_lazy('voucher_model', 'Shipping')),+ (BASKET_TYPE, pgettext_lazy('voucher_model', 'Baskets over'))+ )++ FIELDS_DEPENDED_ON_TYPE = ('product', 'category', 'apply_to', 'limit')++ type = models.CharField(max_length=20, choices=TYPE_CHOICES)+ name = models.CharField(max_length=255, null=True, blank=True)+ code = models.CharField(max_length=12, unique=True, db_index=True)+ usage_limit = models.PositiveIntegerField(+ null=True, blank=True,+ help_text=pgettext_lazy('voucher_model', 'Unlimited if empty'))+ used = models.PositiveIntegerField(default=0, editable=False)+ start_date = models.DateField(default=date.today)+ end_date = models.DateField(null=True, blank=True, help_text=pgettext_lazy(+ 'voucher_model', 'Never expire if empty'))++ discount_value_type = models.CharField(+ max_length=10, choices=DISCOUNT_VALUE_TYPE_CHOICES, default=DISCOUNT_VALUE_FIXED)+ discount_value = models.DecimalField(max_digits=12, decimal_places=2)++ # not mandatory fields, usage depends on type+ product = models.ForeignKey('product.Product', blank=True, null=True)+ category = models.ForeignKey('product.Category', blank=True, null=True)+ apply_to = models.CharField(max_length=20, blank=True, null=True)+ limit = PriceField(max_digits=12, decimal_places=2, null=True,+ blank=True, currency=settings.DEFAULT_CURRENCY)+++ def __str__(self):+ if self.name:+ return self.name+ return self.type++@python_2_unicode_compatibleclass Sale(models.Model):FIXED = 'fixed'PERCENTAGE = 'percentage'DISCOUNT_TYPE_CHOICES = (- (FIXED, pgettext_lazy('discount type', 'Fixed amount')),- (PERCENTAGE, pgettext_lazy('discount_type', 'Percentage discount')))+ (FIXED, pgettext_lazy('discount_type', settings.DEFAULT_CURRENCY)),+ (PERCENTAGE, pgettext_lazy('discount_type', '%')))name = models.CharField(max_length=255)type = models.CharField(max_length=10, choices=DISCOUNT_TYPE_CHOICES,