예제 #1
0
def shopping_cart(request):
    if request.method != "GET":
        return JsonResponse(data={"message": "Wrong method"}, status=400)
    # is_active is a field for user model and is_authenticated is a function for user model.
    if request.user.is_authenticated and request.user.is_active:
        order = Order.initiate(request.user.customer)
        return JsonResponse(order.to_dict(), status=200)
    else:
        return JsonResponse(data={"message": "You are not logged in."}, status=403)
예제 #2
0
def add_items(request):
    if request.method != "POST":
        return JsonResponse(data={"message": "Wrong method"}, status=400)
    try:
        if request.user.is_authenticated and request.user.is_active:
            data = json.loads(request.body.decode('utf-8'))
            try:
                if not isinstance(data, list):
                    raise Exception()
            except:
                return JsonResponse(data={"message": "Can't read message body"}, status=400)
            order = Order.initiate(request.user.customer)
            errors = []
            for item in data:
                if 'code' in item and 'amount' not in item:
                    errors.append({'code': item['code'],
                                   'message': "Item has no 'amount' property."
                                   })
                    continue
                if 'code' not in item:
                    errors.append({'code': "???",
                                   'message': "Item has no 'code' property."
                                   })
                    continue
                try:
                    if Product.objects.filter(code=item['code']).exists():
                        order.add_product(Product.objects.get(code=item['code']), item['amount'])
                    else:
                        raise Exception("Product not found.")
                except Exception as e:
                    errors.append({'code': item['code'],
                                   'message': str(e)
                                   })
            if errors:
                return JsonResponse(order.to_dict(errors), status=400)
            else:
                return JsonResponse(order.to_dict(), status=200)
        else:
            return JsonResponse(data={"message": "You are not logged in."}, status=403)
    except Exception as e:
        return JsonResponse(data=str(e), status=400)
예제 #3
0
def submit(request):
    if request.method != "POST":
        return JsonResponse(data={"message": "Wrong method"}, status=400)
    try:
        if len(json.loads(request.body.decode('utf-8'))):
            raise Exception("")
    except:
        return JsonResponse({"message": "Can't read request body."}, status=400)
    try:
        if request.user.is_active and request.user.is_authenticated:
            data = json.loads(request.body.decode('utf-8'))
            if not isinstance(data, dict):
                return JsonResponse({'message': 'Not able to read your request body.'}, status=404)
            order = Order.initiate(request.user.customer)
            try:
                order.submit()
            except Exception as e:
                return JsonResponse({"message": str(e)}, status=400)
            return JsonResponse(order.toDict(), status=200)
        else:
            return JsonResponse(data={"message": "You are not logged in."}, status=403)
    except Exception as e:
        return JsonResponse(data=str(e), status=400)