2017-02-11 17:06:19 +00:00
|
|
|
from rest_framework.decorators import api_view, permission_classes
|
2017-03-30 08:03:30 +00:00
|
|
|
from rest_framework import permissions, status, viewsets
|
2017-02-11 17:06:19 +00:00
|
|
|
from rest_framework.response import Response
|
2017-03-30 08:03:30 +00:00
|
|
|
from longclaw.longclawshipping import models, utils, serializers
|
2017-02-12 16:05:03 +00:00
|
|
|
from longclaw.longclawsettings.models import LongclawSettings
|
2017-02-11 17:06:19 +00:00
|
|
|
|
2017-03-30 08:03:30 +00:00
|
|
|
class AddressViewSet(viewsets.ModelViewSet):
|
2017-10-01 10:19:37 +00:00
|
|
|
"""
|
2017-03-30 08:03:30 +00:00
|
|
|
Create, list and view Addresses
|
2017-10-01 10:19:37 +00:00
|
|
|
"""
|
2017-03-30 08:03:30 +00:00
|
|
|
queryset = models.Address.objects.all()
|
|
|
|
serializer_class = serializers.AddressSerializer
|
|
|
|
|
2017-02-11 17:06:19 +00:00
|
|
|
|
|
|
|
@api_view(['GET'])
|
|
|
|
@permission_classes({permissions.AllowAny})
|
|
|
|
def shipping_cost(request):
|
2017-10-01 10:19:37 +00:00
|
|
|
""" Returns the shipping cost for a given country
|
2017-02-11 17:06:19 +00:00
|
|
|
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
|
2017-10-01 10:19:37 +00:00
|
|
|
"""
|
2017-02-11 17:06:19 +00:00
|
|
|
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)
|
2017-08-09 13:46:48 +00:00
|
|
|
data = utils.get_shipping_cost(settings, code, option)
|
2017-02-11 17:06:19 +00:00
|
|
|
response = Response(data=data, status=status.HTTP_200_OK)
|
2017-03-14 08:58:01 +00:00
|
|
|
except utils.InvalidShippingRate:
|
2017-02-11 17:06:19 +00:00
|
|
|
response = Response(data={"message": "Shipping option {} is invalid".format(option)},
|
|
|
|
status=status.HTTP_400_BAD_REQUEST)
|
2017-03-14 08:58:01 +00:00
|
|
|
except utils.InvalidShippingCountry:
|
2017-02-11 17:06:19 +00:00
|
|
|
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):
|
2017-10-01 10:19:37 +00:00
|
|
|
""" Get all shipping countries
|
|
|
|
"""
|
2017-06-08 13:00:21 +00:00
|
|
|
queryset = models.Country.objects.exclude(shippingrate=None)
|
2017-06-08 13:26:25 +00:00
|
|
|
serializer = serializers.CountrySerializer(queryset, many=True)
|
|
|
|
return Response(data=serializer.data, status=status.HTTP_200_OK)
|
2017-05-26 16:20:18 +00:00
|
|
|
|
|
|
|
@api_view(["GET"])
|
|
|
|
@permission_classes([permissions.AllowAny])
|
|
|
|
def shipping_options(request, country):
|
2017-10-01 10:19:37 +00:00
|
|
|
"""
|
2017-05-26 16:20:18 +00:00
|
|
|
Get the shipping options for a given country
|
2017-10-01 10:19:37 +00:00
|
|
|
"""
|
2017-05-26 16:20:18 +00:00
|
|
|
qrs = models.ShippingRate.objects.filter(countries__in=[country])
|
|
|
|
serializer = serializers.ShippingRateSerializer(qrs, many=True)
|
|
|
|
return Response(
|
|
|
|
data=serializer.data,
|
|
|
|
status=status.HTTP_200_OK
|
|
|
|
)
|