I need an api that is essentially a single product express checkout, so to speak. I've developed a view that is a combination of the basket.addproductview and checkoutview. I'm new to Django/oscar (recovering C# developer) and I'd like to know if this is the right approach for such a feature. Thanks in advance.
def post(self, request, format=None):
# deserialize the product from json
p_ser = self.add_product_serializer_class(
data=request.data['addproduct'], context={'request': request})
if p_ser.is_valid():
# create a basket
basket = operations.get_basket(request)
# load the validate the product
product = p_ser.validated_data['url']
# load the validated quantity
quantity = p_ser.validated_data['quantity']
# load any options
options = p_ser.validated_data.get('options', [])
#validate the basket
basket_valid, message = self.validate(
basket, product, quantity, options)
if not basket_valid:
return Response(
{'reason': message},
status=status.HTTP_406_NOT_ACCEPTABLE)
# add the product to the validated basket
basket.add_product(product, quantity=quantity, options=options)
# apply offers
operations.apply_offers(request, basket)
###### from oscarapi.views.checkout
# deserialize the checkout object and complete the order
co_data = request.data['checkout']
co_data['basket'] = "http://127.0.0.1:8000/oscar-api/baskets/"+str(basket.pk)+"/"
c_ser = self.checkout_serializer(
data=co_data, context={'request': request})
if c_ser.is_valid():
order = c_ser.save()
basket.freeze()
o_ser = self.order_serializer_class(
order, context={'request': request})
oscarapi_post_checkout.send(
sender=self, order=order, user=request.user,
request=request, response=response)
return response.Response(o_ser.data)
return response.Response(c_ser.errors, status.HTTP_406_NOT_ACCEPTABLE)
{
"addproduct": {
"url": "http://127.0.0.1:8000/oscar-api/products/1553/",
"quantity": 1
},
"checkout": {
"guest_email": "[email protected]",
"total": "60.00",
"shipping_method_code": "no-shipping-required",
"first_name": "M",
"last_name": "T",
"phone_number": "12343455",
"shipping_charge": {
"currency": "USD",
"incl_tax": "0.00",
"excl_tax": "0.00",
"tax": "0.0"
},
"shipping_address": {
"country": "http://127.0.0.1:8000/oscar-api/countries/US/",
"line1": "123 B St",
"line2": "A",
"line3": "B",
"line4": "C",
"notes": "Hello",
"postcode": "94704",
"state": "TX",
"title": "Mr"
}
}
}
HTTP 200 OK
Allow: POST, OPTIONS
Content-Type: application/json
Vary: Accept
{
"number": "100035",
"basket": "http://127.0.0.1:8000/oscar-api/baskets/35/",
"url": "http://127.0.0.1:8000/oscar-api/orders/1/",
"lines": "http://127.0.0.1:8000/oscar-api/orders/1/lines/",
"owner": null,
"billing_address": null,
"currency": "USD",
"total_incl_tax": "60.00",
"total_excl_tax": "60.00",
"shipping_incl_tax": "0.00",
"shipping_excl_tax": "0.00",
"shipping_address": {
"id": 1,
"country": "http://127.0.0.1:8000/oscar-api/countries/US/",
"title": "Mr",
"first_name": "",
"last_name": "",
"line1": "123 B St",
"line2": "A",
"line3": "B",
"line4": "C",
"state": "TX",
"postcode": "94704",
"search_text": "123 B St A B C TX 94704 United States of America",
"phone_number": null,
"notes": "Hello"
},
"shipping_method": "Free shipping",
"shipping_code": "free-shipping",
"status": "new",
"guest_email": "[email protected]",
"date_placed": "2017-03-23T07:01:03.672889Z",
"payment_url": "You need to implement a view named 'api-payment' which redirects to the payment provider and sets up the callbacks.",
"offer_discounts": [],
"voucher_discounts": []
}