2017-02-11 17:06:19 +00:00
|
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
|
|
from rest_framework import permissions, status
|
|
|
|
from rest_framework.response import Response
|
2017-02-27 21:43:31 +00:00
|
|
|
from django_countries import countries
|
2017-02-17 09:03:21 +00:00
|
|
|
from longclaw.longclawshipping import serializers, models
|
2017-02-12 16:05:03 +00:00
|
|
|
from longclaw.longclawsettings.models import LongclawSettings
|
2017-02-11 17:06:19 +00:00
|
|
|
|
|
|
|
class InvalidShippingRate(Exception):
|
|
|
|
pass
|
|
|
|
|
2017-02-12 16:05:03 +00:00
|
|
|
|
2017-02-11 17:06:19 +00:00
|
|
|
class InvalidShippingCountry(Exception):
|
|
|
|
pass
|
|
|
|
|
2017-02-12 16:05:03 +00:00
|
|
|
|
|
|
|
def get_shipping_cost(country_code, option, settings):
|
2017-02-11 17:06:19 +00:00
|
|
|
try:
|
2017-03-12 18:05:00 +00:00
|
|
|
qrs = models.ShippingRate.objects.filter(countries__contains=country_code)
|
2017-02-12 16:05:03 +00:00
|
|
|
try:
|
2017-03-12 21:19:07 +00:00
|
|
|
if qrs.count() > 1:
|
|
|
|
shipping_rate = qrs.filter(name=option)[0]
|
|
|
|
else:
|
|
|
|
shipping_rate = qrs[0]
|
2017-02-12 16:05:03 +00:00
|
|
|
return {
|
|
|
|
"rate": shipping_rate.rate,
|
|
|
|
"description": shipping_rate.description,
|
|
|
|
"carrier": shipping_rate.carrier
|
|
|
|
}
|
|
|
|
except models.ShippingRate.DoesNotExist:
|
2017-02-11 17:06:19 +00:00
|
|
|
raise InvalidShippingRate
|
|
|
|
|
2017-02-27 21:43:31 +00:00
|
|
|
except models.ShippingRate.DoesNotExist:
|
2017-02-12 16:05:03 +00:00
|
|
|
if settings.default_shipping_enabled:
|
|
|
|
return {"rate": settings.default_shipping_rate,
|
2017-02-11 17:06:19 +00:00
|
|
|
"description": "Standard shipping to rest of world",
|
2017-02-12 16:05:03 +00:00
|
|
|
"carrier": settings.default_shipping_rate}
|
2017-02-11 17:06:19 +00:00
|
|
|
else:
|
|
|
|
raise InvalidShippingCountry
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
|
|
|
@permission_classes({permissions.AllowAny})
|
|
|
|
def shipping_cost(request):
|
|
|
|
''' Returns the shipping cost for a given country
|
|
|
|
If the shipping cost for the given country has not been set, it will
|
|
|
|
fallback to the default shipping cost if it has been enabled in the app
|
|
|
|
settings
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
code = request.query_params.get('country_code')
|
|
|
|
except AttributeError:
|
|
|
|
return Response(data={"message": "No country code supplied"},
|
|
|
|
status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
2017-02-12 16:05:03 +00:00
|
|
|
option = request.query_params.get('shipping_rate_name', 'standard')
|
2017-02-11 17:06:19 +00:00
|
|
|
try:
|
2017-02-12 16:05:03 +00:00
|
|
|
settings = LongclawSettings.for_site(request.site)
|
|
|
|
data = get_shipping_cost(code, option, settings)
|
2017-02-11 17:06:19 +00:00
|
|
|
response = Response(data=data, status=status.HTTP_200_OK)
|
|
|
|
except InvalidShippingRate:
|
|
|
|
response = Response(data={"message": "Shipping option {} is invalid".format(option)},
|
|
|
|
status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
except InvalidShippingCountry:
|
|
|
|
response = Response(data={"message": "Shipping to {} is not available".format(code)},
|
|
|
|
status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(["GET"])
|
|
|
|
@permission_classes([permissions.AllowAny])
|
|
|
|
def shipping_countries(request):
|
|
|
|
''' Get all shipping countries
|
|
|
|
'''
|
2017-03-12 18:05:00 +00:00
|
|
|
queryset = models.ShippingRate.objects.all()
|
|
|
|
country_data = [(c.name, c.code) for obj in queryset for c in obj.countries]
|
2017-02-27 21:43:31 +00:00
|
|
|
return Response(data=country_data, status=status.HTTP_200_OK)
|