def add_to_cart_ajax_request(request): if request.method == 'POST': position = int(request.POST['position']) session = request.COOKIES.get('sessionid', None) if not session: new_session = str(uuid.uuid4()) basket = Basket(session=new_session, positions=[position], ordered=False, who=request.META.get('REMOTE_ADDR')) basket.save() response = soups_from_basket_json(basket) response.set_cookie('sessionid', new_session) return response basket = Basket.objects.get_or_create(session=session, ordered=False, who=request.META.get('REMOTE_ADDR'))[0] try: if not basket.positions: basket.positions = [position] else: basket.positions.append(position) basket.save() return HttpResponse(soups_from_basket_json(basket), mimetype='application/json') except Exception: return Http404() return HttpResponse(json.dumps({'result': 'failure'}))
def delete_from_cart_ajax_request(request): if request.method == 'POST': position = int(request.POST['position']) session = request.COOKIES.get('sessionid', None) if not session: return Http404() basket = Basket.objects.get(session=session, ordered=False) basket.positions.remove(position) basket.save() return HttpResponse(soups_from_basket_json(basket), mimetype='application/json')
def get_cart_ajax_request(request): if request.method == 'POST': session = request.COOKIES['sessionid'] if not session: session = uuid.uuid4() basket = Basket(session=session, positions=[], ordered=False, who=request.META.get('REMOTE_ADDR')) basket.save() response = HttpResponse() response.set_cookie('sessionid', session) return response try: basket = Basket.objects.get(session=session, ordered=False) return HttpResponse(soups_from_basket_json(basket), mimetype='application/json') except Basket.DoesNotExist: basket = Basket(session=session, positions=[], ordered=False, who=request.META.get('REMOTE_ADDR')) basket.save() return HttpResponse()