Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add shipping models
Michał Ociepkafd44e87b6c3c
diff --git a/saleor/shipping/__init__.py b/saleor/shipping/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/shipping/migrations/0001_initial.py b/saleor/shipping/migrations/0001_initial.pynew file mode 100644index 0000000000..e051b52576--- /dev/null+++ b/saleor/shipping/migrations/0001_initial.py@@ -0,0 +1,43 @@+# -*- coding: utf-8 -*-+# Generated by Django 1.9.1 on 2016-01-19 10:18+from __future__ import unicode_literals++from django.db import migrations, models+import django.db.models.deletion+import django.db.models.manager+import django_prices.models+++class Migration(migrations.Migration):++ initial = True++ dependencies = [+ ]++ operations = [+ migrations.CreateModel(+ name='ShippingMethod',+ fields=[+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),+ ('name', models.CharField(max_length=100)),+ ('description', models.TextField(blank=True, default=b'')),+ ],+ ),+ migrations.CreateModel(+ name='ShippingMethodCountry',+ fields=[+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),+ ('country_code', models.CharField(blank=True, choices=[(b'', b'Any country'), ('AF', 'Afghanistan'), ('AX', '\xc5land Islands'), ('AL', 'Albania'), ('DZ', 'Algeria'), ('AS', 'American Samoa'), ('AD', 'Andorra'), ('AO', 'Angola'), ('AI', 'Anguilla'), ('AQ', 'Antarctica'), ('AG', 'Antigua And Barbuda'), ('AR', 'Argentina'), ('AM', 'Armenia'), ('AW', 'Aruba'), ('AU', 'Australia'), ('AT', 'Austria'), ('AZ', 'Azerbaijan'), ('BS', 'Bahamas'), ('BH', 'Bahrain'), ('BD', 'Bangladesh'), ('BB', 'Barbados'), ('BY', 'Belarus'), ('BE', 'Belgium'), ('BZ', 'Belize'), ('BJ', 'Benin'), ('BM', 'Bermuda'), ('BT', 'Bhutan'), ('BO', 'Bolivia'), ('BQ', 'Bonaire, Saint Eustatius And Saba'), ('BA', 'Bosnia And Herzegovina'), ('BW', 'Botswana'), ('BV', 'Bouvet Island'), ('BR', 'Brazil'), ('IO', 'British Indian Ocean Territory'), ('BN', 'Brunei Darussalam'), ('BG', 'Bulgaria'), ('BF', 'Burkina Faso'), ('BI', 'Burundi'), ('KH', 'Cambodia'), ('CM', 'Cameroon'), ('CA', 'Canada'), ('CV', 'Cape Verde'), ('KY', 'Cayman Islands'), ('CF', 'Central African Republic'), ('TD', 'Chad'), ('CL', 'Chile'), ('CN', 'China'), ('CX', 'Christmas Island'), ('CC', 'Cocos (Keeling) Islands'), ('CO', 'Colombia'), ('KM', 'Comoros'), ('CG', 'Congo'), ('CD', 'Congo, The Democratic Republic of the'), ('CK', 'Cook Islands'), ('CR', 'Costa Rica'), ('CI', "C\xf4te D'Ivoire"), ('HR', 'Croatia'), ('CU', 'Cuba'), ('CW', 'Cura\xe7o'), ('CY', 'Cyprus'), ('CZ', 'Czech Republic'), ('DK', 'Denmark'), ('DJ', 'Djibouti'), ('DM', 'Dominica'), ('DO', 'Dominican Republic'), ('EC', 'Ecuador'), ('EG', 'Egypt'), ('SV', 'El Salvador'), ('GQ', 'Equatorial Guinea'), ('ER', 'Eritrea'), ('EE', 'Estonia'), ('ET', 'Ethiopia'), ('FK', 'Falkland Islands (Malvinas)'), ('FO', 'Faroe Islands'), ('FJ', 'Fiji'), ('FI', 'Finland'), ('FR', 'France'), ('GF', 'French Guiana'), ('PF', 'French Polynesia'), ('TF', 'French Southern Territories'), ('GA', 'Gabon'), ('GM', 'Gambia'), ('GE', 'Georgia'), ('DE', 'Germany'), ('GH', 'Ghana'), ('GI', 'Gibraltar'), ('GR', 'Greece'), ('GL', 'Greenland'), ('GD', 'Grenada'), ('GP', 'Guadeloupe'), ('GU', 'Guam'), ('GT', 'Guatemala'), ('GG', 'Guernsey'), ('GN', 'Guinea'), ('GW', 'Guinea-Bissau'), ('GY', 'Guyana'), ('HT', 'Haiti'), ('HM', 'Heard Island And Mcdonald Islands'), ('VA', 'Holy See (Vatican City State)'), ('HN', 'Honduras'), ('HK', 'Hong Kong'), ('HU', 'Hungary'), ('IS', 'Iceland'), ('IN', 'India'), ('ID', 'Indonesia'), ('IR', 'Iran, Islamic Republic of'), ('IQ', 'Iraq'), ('IE', 'Ireland'), ('IM', 'Isle of Man'), ('IL', 'Israel'), ('IT', 'Italy'), ('JM', 'Jamaica'), ('JP', 'Japan'), ('JE', 'Jersey'), ('JO', 'Jordan'), ('KZ', 'Kazakhstan'), ('KE', 'Kenya'), ('KI', 'Kiribati'), ('KP', "Korea, Democratic People's Republic of"), ('KR', 'Korea, Republic of'), ('KW', 'Kuwait'), ('KG', 'Kyrgyzstan'), ('LA', "Lao People's Democratic Republic"), ('LV', 'Latvia'), ('LB', 'Lebanon'), ('LS', 'Lesotho'), ('LR', 'Liberia'), ('LY', 'Libya'), ('LI', 'Liechtenstein'), ('LT', 'Lithuania'), ('LU', 'Luxembourg'), ('MO', 'Macao'), ('MK', 'Macedonia, The Former Yugoslav Republic of'), ('MG', 'Madagascar'), ('MW', 'Malawi'), ('MY', 'Malaysia'), ('MV', 'Maldives'), ('ML', 'Mali'), ('MT', 'Malta'), ('MH', 'Marshall Islands'), ('MQ', 'Martinique'), ('MR', 'Mauritania'), ('MU', 'Mauritius'), ('YT', 'Mayotte'), ('MX', 'Mexico'), ('FM', 'Micronesia, Federated States of'), ('MD', 'Moldova, Republic of'), ('MC', 'Monaco'), ('MN', 'Mongolia'), ('ME', 'Montenegro'), ('MS', 'Montserrat'), ('MA', 'Morocco'), ('MZ', 'Mozambique'), ('MM', 'Myanmar'), ('NA', 'Namibia'), ('NR', 'Nauru'), ('NP', 'Nepal'), ('NL', 'Netherlands'), ('NC', 'New Caledonia'), ('NZ', 'New Zealand'), ('NI', 'Nicaragua'), ('NE', 'Niger'), ('NG', 'Nigeria'), ('NU', 'Niue'), ('NF', 'Norfolk Island'), ('MP', 'Northern Mariana Islands'), ('NO', 'Norway'), ('OM', 'Oman'), ('PK', 'Pakistan'), ('PW', 'Palau'), ('PS', 'Palestinian Territory, Occupied'), ('PA', 'Panama'), ('PG', 'Papua New Guinea'), ('PY', 'Paraguay'), ('PE', 'Peru'), ('PH', 'Philippines'), ('PN', 'Pitcairn'), ('PL', 'Poland'), ('PT', 'Portugal'), ('PR', 'Puerto Rico'), ('QA', 'Qatar'), ('RE', 'R\xe9union'), ('RO', 'Romania'), ('RU', 'Russian Federation'), ('RW', 'Rwanda'), ('BL', 'Saint Barth\xe9lemy'), ('SH', 'Saint Helena, Ascension And Tristan Da Cunha'), ('KN', 'Saint Kitts And Nevis'), ('LC', 'Saint Lucia'), ('MF', 'Saint Martin (French Part)'), ('PM', 'Saint Pierre And Miquelon'), ('VC', 'Saint Vincent And the Grenadines'), ('WS', 'Samoa'), ('SM', 'San Marino'), ('ST', 'Sao Tome And Principe'), ('SA', 'Saudi Arabia'), ('SN', 'Senegal'), ('RS', 'Serbia'), ('SC', 'Seychelles'), ('SL', 'Sierra Leone'), ('SG', 'Singapore'), ('SX', 'Sint Maarten (Dutch Part)'), ('SK', 'Slovakia'), ('SI', 'Slovenia'), ('SB', 'Solomon Islands'), ('SO', 'Somalia'), ('ZA', 'South Africa'), ('GS', 'South Georgia and the South Sandwich Islands'), ('ES', 'Spain'), ('LK', 'Sri Lanka'), ('SD', 'Sudan'), ('SR', 'Suriname'), ('SJ', 'Svalbard and Jan Mayen'), ('SZ', 'Swaziland'), ('SE', 'Sweden'), ('CH', 'Switzerland'), ('SY', 'Syria'), ('TW', 'Taiwan'), ('TJ', 'Tajikistan'), ('TZ', 'Tanzania'), ('TH', 'Thailand'), ('TL', 'Timor-Leste'), ('TG', 'Togo'), ('TK', 'Tokelau'), ('TO', 'Tonga'), ('TT', 'Trinidad And Tobago'), ('TN', 'Tunisia'), ('TR', 'Turkey'), ('TM', 'Turkmenistan'), ('TC', 'Turks And Caicos Islands'), ('TV', 'Tuvalu'), ('UG', 'Uganda'), ('UA', 'Ukraine'), ('AE', 'United Arab Emirates'), ('GB', 'United Kingdom'), ('US', 'United States'), ('UM', 'United States Minor Outlying Islands'), ('UY', 'Uruguay'), ('UZ', 'Uzbekistan'), ('VU', 'Vanuatu'), ('VE', 'Venezuela'), ('VN', 'Viet Nam'), ('VG', 'Virgin Islands, British'), ('VI', 'Virgin Islands, U.S.'), ('WF', 'Wallis And Futuna'), ('EH', 'Western Sahara'), ('YE', 'Yemen'), ('ZM', 'Zambia'), ('ZW', 'Zimbabwe')], default=b'', max_length=2)),+ ('price', django_prices.models.PriceField(currency='USD', decimal_places=2, max_digits=12, verbose_name='price')),+ ('shipping_method', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='price_per_country', to='shipping.ShippingMethod')),+ ],+ managers=[+ ('object', django.db.models.manager.Manager()),+ ],+ ),+ migrations.AlterUniqueTogether(+ name='shippingmethodcountry',+ unique_together=set([('country_code', 'shipping_method')]),+ ),+ ]diff --git a/saleor/shipping/migrations/__init__.py b/saleor/shipping/migrations/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/shipping/models.py b/saleor/shipping/models.pynew file mode 100644index 0000000000..7dd2d1f405--- /dev/null+++ b/saleor/shipping/models.py@@ -0,0 +1,84 @@+from __future__ import absolute_import+from itertools import groupby+from operator import itemgetter++from django.conf import settings+from django.db import models+from django.db.models import Q+from django.utils.encoding import python_2_unicode_compatible+from django.utils.translation import pgettext_lazy, gettext as _+from django_prices.models import PriceField+from prices import PriceRange++from django_countries.data import COUNTRIES+++@python_2_unicode_compatible+class ShippingMethod(models.Model):++ name = models.CharField(max_length=100)+ description = models.TextField(blank=True, default='')++ def __str__(self):+ return self.name++ @property+ def countries(self):+ countries = [str(country) for country in self.price_per_country.all()]+ return countries++ @property+ def price_range(self):+ prices = list(self.price_per_country.values_list('price', flat=True))+ if prices:+ return PriceRange(min(prices), max(prices))+++class ShippingMethodCountryQueryset(models.QuerySet):++ def unique_for_country_code(self, country_code):+ shipping = self.filter(+ Q(country_code=country_code) |+ Q(country_code=ShippingMethodCountry.ANY_COUNTRY))+ shipping = shipping.order_by('shipping_method_id')+ shipping = shipping.values_list('shipping_method_id', 'id', 'country_code')+ grouped_shipping = groupby(shipping, itemgetter(0))+ any_country = ShippingMethodCountry.ANY_COUNTRY++ ids = []++ for shipping_method_id, method_values in grouped_shipping:+ method_values = list(method_values)+ # if there is any country choice and specific one remove any country choice+ if len(method_values) == 2:+ method = [val for val in method_values if val[2] != any_country][0]+ else:+ method = method_values[0]+ ids.append(method[1])+ return self.filter(id__in=ids)+++@python_2_unicode_compatible+class ShippingMethodCountry(models.Model):++ ANY_COUNTRY = ''+ COUNTRY_CODE_CHOICES = [(ANY_COUNTRY, _('Any country'))] + list(COUNTRIES.items())++ country_code = models.CharField(+ choices=COUNTRY_CODE_CHOICES, max_length=2, blank=True, default=ANY_COUNTRY)+ price = PriceField(+ pgettext_lazy('Shipping method region field', 'price'),+ currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2)+ shipping_method = models.ForeignKey(ShippingMethod, related_name='price_per_country')++ objects = ShippingMethodCountryQueryset.as_manager()++ class Meta:+ unique_together = ('country_code', 'shipping_method')++ def __str__(self):+ # https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.get_FOO_display # noqa+ return "%s %s" % (self.shipping_method, self.get_country_code_display())++ def get_total(self):+ return self.price