kopia lustrzana https://github.com/longclawshop/longclaw
Fixes #24
rodzic
f57550fd36
commit
73d32872df
|
@ -1,6 +1,7 @@
|
|||
from rest_framework.decorators import api_view, permission_classes
|
||||
from rest_framework import permissions, status
|
||||
from rest_framework.response import Response
|
||||
from django_countries import countries
|
||||
from longclaw.longclawshipping import serializers, models
|
||||
from longclaw.longclawsettings.models import LongclawSettings
|
||||
|
||||
|
@ -14,9 +15,9 @@ class InvalidShippingCountry(Exception):
|
|||
|
||||
def get_shipping_cost(country_code, option, settings):
|
||||
try:
|
||||
obj = models.ShippingCountry.objects.get(country=country_code)
|
||||
qrs = models.ShippingRate.objects.filter(country=country_code)
|
||||
try:
|
||||
shipping_rate = obj.shipping_rates.get(name=option)
|
||||
shipping_rate = qrs.filter(name=option)[0]
|
||||
return {
|
||||
"rate": shipping_rate.rate,
|
||||
"description": shipping_rate.description,
|
||||
|
@ -25,7 +26,7 @@ def get_shipping_cost(country_code, option, settings):
|
|||
except models.ShippingRate.DoesNotExist:
|
||||
raise InvalidShippingRate
|
||||
|
||||
except models.ShippingCountry.DoesNotExist:
|
||||
except models.ShippingRate.DoesNotExist:
|
||||
if settings.default_shipping_enabled:
|
||||
return {"rate": settings.default_shipping_rate,
|
||||
"description": "Standard shipping to rest of world",
|
||||
|
@ -68,6 +69,7 @@ def shipping_cost(request):
|
|||
def shipping_countries(request):
|
||||
''' Get all shipping countries
|
||||
'''
|
||||
queryset = models.ShippingCountry.objects.all()
|
||||
serializer = serializers.ShippingCountrySerializer(queryset, many=True)
|
||||
return Response(data=serializer.data, status=status.HTTP_200_OK)
|
||||
queryset = models.ShippingRate.objects.values_list('countries').distinct()
|
||||
country_dict = dict(countries)
|
||||
country_data = [(country_dict[code[0]], code[0]) for code in queryset]
|
||||
return Response(data=country_data, status=status.HTTP_200_OK)
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2017-02-27 20:17
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('longclawshipping', '0008_auto_20170212_1422'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='shippingcountry',
|
||||
name='page_ptr',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='shippingcountry',
|
||||
name='id',
|
||||
field=models.AutoField(auto_created=True, default=1, primary_key=True, serialize=False, verbose_name='ID'),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='shippingrate',
|
||||
name='shipping_country',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='shipping_rates', to='longclawshipping.ShippingCountry'),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,24 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2017-02-27 20:23
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('longclawshipping', '0009_auto_20170227_2017'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='shippingrate',
|
||||
name='shipping_country',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='shippingrate',
|
||||
name='countries',
|
||||
field=models.ManyToManyField(related_name='shipping_rates', to='longclawshipping.ShippingCountry'),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,26 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2017-02-27 20:48
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
import django_countries.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('longclawshipping', '0010_auto_20170227_2023'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='shippingrate',
|
||||
name='countries',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='shippingrate',
|
||||
name='countries',
|
||||
field=django_countries.fields.CountryField(default='UK', max_length=599, multiple=True),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
|
@ -0,0 +1,18 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2017-02-27 21:43
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('longclawshipping', '0011_auto_20170227_2048'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.DeleteModel(
|
||||
name='ShippingCountry',
|
||||
),
|
||||
]
|
|
@ -1,7 +1,6 @@
|
|||
from django.db import models
|
||||
from modelcluster.fields import ParentalKey
|
||||
from wagtail.wagtailcore.models import Page
|
||||
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
|
||||
from django import forms
|
||||
from wagtail.wagtailadmin.edit_handlers import FieldPanel
|
||||
from django_countries.fields import CountryField
|
||||
|
||||
|
||||
|
@ -25,31 +24,24 @@ class Address(models.Model):
|
|||
def __str__(self):
|
||||
return "{}, {}, {}".format(self.name, self.city, self.country)
|
||||
|
||||
|
||||
class ShippingCountry(Page):
|
||||
''' Standard and premimum rate shipping for
|
||||
individual countries.
|
||||
'''
|
||||
parent_page_types = ['wagtailcore.Page']
|
||||
country = CountryField()
|
||||
|
||||
content_panels = Page.content_panels + [
|
||||
FieldPanel('country'),
|
||||
InlinePanel('shipping_rates', label='Shipping rates')
|
||||
]
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = "shipping countries"
|
||||
|
||||
|
||||
class ShippingRate(models.Model):
|
||||
|
||||
'''
|
||||
An individual shipping rate. This can be applied to
|
||||
multiple countries.
|
||||
'''
|
||||
name = models.CharField(max_length=32)
|
||||
rate = models.DecimalField(max_digits=12, decimal_places=2)
|
||||
carrier = models.CharField(max_length=64)
|
||||
description = models.CharField(max_length=128)
|
||||
shipping_country = ParentalKey(
|
||||
ShippingCountry, related_name="shipping_rates")
|
||||
countries = CountryField(multiple=True)
|
||||
|
||||
panels = [
|
||||
FieldPanel('name'),
|
||||
FieldPanel('rate'),
|
||||
FieldPanel('carrier'),
|
||||
FieldPanel('description'),
|
||||
FieldPanel('countries')
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from rest_framework import serializers
|
||||
from longclaw.longclawshipping.models import Address, ShippingCountry, ShippingRate
|
||||
from longclaw.longclawshipping.models import Address, ShippingRate
|
||||
|
||||
class AddressSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
@ -13,12 +13,4 @@ class ShippingRateSerializer(serializers.ModelSerializer):
|
|||
model = ShippingRate
|
||||
fields = "__all__"
|
||||
|
||||
class ShippingCountrySerializer(serializers.ModelSerializer):
|
||||
|
||||
shipping_rates = ShippingRateSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
model = ShippingCountry
|
||||
fields = "__all__"
|
||||
|
||||
|
|
@ -1,25 +1,17 @@
|
|||
from wagtail.contrib.modeladmin.options import (
|
||||
ModelAdmin, modeladmin_register
|
||||
)
|
||||
from longclaw.longclawshipping.models import ShippingCountry
|
||||
from longclaw.longclawshipping.models import ShippingRate
|
||||
|
||||
|
||||
class ShippingCountryModelAdmin(ModelAdmin):
|
||||
model = ShippingCountry
|
||||
class ShippingRateModelAdmin(ModelAdmin):
|
||||
model = ShippingRate
|
||||
menu_label = 'Shipping'
|
||||
menu_order = 200
|
||||
menu_icon = 'site'
|
||||
add_to_settings_menu = False
|
||||
exclude_from_explorer = True
|
||||
list_display = ('country', 'country_code', 'shipping_rates')
|
||||
list_display = ('name', 'rate', 'carrier', 'description')
|
||||
|
||||
def flag(self, obj):
|
||||
return obj.country.flag
|
||||
|
||||
def country_code(self, obj):
|
||||
return obj.country.alpha3
|
||||
|
||||
def shipping_rates(self, obj):
|
||||
return ", ".join(str(rate) for rate in obj.shipping_rates.all())
|
||||
|
||||
modeladmin_register(ShippingCountryModelAdmin)
|
||||
modeladmin_register(ShippingRateModelAdmin)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
django-countries==4.0
|
||||
django-countries>=4.1
|
||||
django-extensions==1.7.5
|
||||
djangorestframework==3.5.4
|
||||
wagtail>=1.7
|
||||
|
|
2
setup.py
2
setup.py
|
@ -88,7 +88,7 @@ setup(
|
|||
install_requires=[
|
||||
'django>=1.8',
|
||||
'wagtail>=1.7',
|
||||
'django-countries>=4.0',
|
||||
'django-countries>=4.1',
|
||||
'django-extensions>=1.7.5',
|
||||
'djangorestframework>=3.5.4'
|
||||
],
|
||||
|
|
Ładowanie…
Reference in New Issue