Example #1
0
def create_source_from_state(state, **kwargs):
    joc = JsonOrderCreator()
    source = joc.create_source_from_state(state, **kwargs)
    if not source:
        raise ValidationError(list(joc.errors))
    return source
Example #2
0
def update_order_from_state(state, order_to_update, **kwargs):
    joc = JsonOrderCreator()
    order = joc.update_order_from_state(state, order_to_update, **kwargs)
    if not order:
        raise ValidationError(list(joc.errors))
    return order
Example #3
0
    def create(self, request, *args, **kwargs):
        post_data = request.data

        # Revise. We should not need to mutate the data for the serializer.
        # Also one problem is that we use text lines later at the create
        text_lines = set()
        for idx, line in enumerate(post_data.get("lines", [])):
            if "product" not in line:
                line["product"] = None
            if line.get("type") == "text":
                line["type"] = "other"
                text_lines.add(idx)

        request.data["orderer"] = None
        request.data["modified_by"] = None
        request.data["creator"] = request.user.pk

        for attr in [
                "shipping_method", "payment_method", "account_manager",
                "tax_group"
        ]:
            if attr not in post_data:
                post_data[attr] = None

        if "customer_groups" not in post_data:
            post_data["customer_groups"] = []

        # Revise. Create should happen in the serializer, but then again
        # we need to return the errors from JsonOrderCreator in some sane way.
        serializer = self.get_serializer(data=post_data)
        serializer.is_valid(raise_exception=True)

        shop = serializer.validated_data["shop"]
        customer = serializer.validated_data["customer"]
        lines = [{
            "id": (idx + 1),
            "quantity":
            line["quantity"],
            "product": {
                "id": getattr(line["product"], "id", None)
            },
            "baseUnitPrice":
            line.get("base_unit_price_value"),
            "unitPrice":
            line.get("base_unit_price_value")
            if line["type"].label == "other" else None,
            "discountAmount":
            line.get("discount_amount_value", 0),
            "sku":
            line.get("sku"),
            "text":
            line.get("text"),
            "type":
            force_text(line["type"].label) if idx not in text_lines else "text"
        } for idx, line in enumerate(serializer.validated_data["lines"])]

        data = {
            "shop": {
                "selected": {
                    "id": shop.id,
                    "name": shop.name,
                    "currency": shop.currency,
                    "priceIncludeTaxes": shop.prices_include_tax
                }
            },
            "methods": {
                "shippingMethod": {
                    "id":
                    getattr(serializer.validated_data["shipping_method"], "id",
                            None)
                },
                "paymentMethod": {
                    "id":
                    getattr(serializer.validated_data["payment_method"], "id",
                            None)
                },
            },
            "lines": lines
        }
        if customer:
            data["customer"] = {
                "id":
                serializer.validated_data["customer"].id,
                "billingAddress":
                encode_address(customer.default_billing_address),
                "shippingAddress":
                encode_address(customer.default_shipping_address),
            }

        joc = JsonOrderCreator()
        order = joc.create_order_from_state(data, creator=request.user)
        if not order:
            return JsonResponse(
                {
                    "status":
                    "error",
                    "errors": [{
                        "message": err.message,
                        "code": err.code
                    } for err in joc.errors]
                },
                status=400)

        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=headers)
Example #4
0
    def create(self, request, *args, **kwargs):
        post_data = request.data

        # Revise. We should not need to mutate the data for the serializer.
        # Also one problem is that we use text lines later at the create
        text_lines = set()
        for idx, line in enumerate(post_data.get("lines", [])):
            if "product" not in line:
                line["product"] = None
            if line.get("type") == "text":
                line["type"] = "other"
                text_lines.add(idx)

        request.data["orderer"] = None
        request.data["modified_by"] = None
        request.data["creator"] = request.user.pk

        for attr in ["shipping_method", "payment_method", "account_manager", "tax_group"]:
            if attr not in post_data:
                post_data[attr] = None

        if "customer_groups" not in post_data:
            post_data["customer_groups"] = []

        # Revise. Create should happen in the serializer, but then again
        # we need to return the errors from JsonOrderCreator in some sane way.
        serializer = self.get_serializer(data=post_data)
        serializer.is_valid(raise_exception=True)

        shop = serializer.validated_data["shop"]
        customer = serializer.validated_data["customer"]
        lines = [{
            "id": (idx + 1),
            "quantity": line["quantity"],
            "product": {
                "id": getattr(line["product"], "id", None)
            },
            "baseUnitPrice": line.get("base_unit_price_value"),
            "unitPrice": line.get("base_unit_price_value") if line["type"].label == "other" else None,
            "discountAmount": line.get("discount_amount_value", 0),
            "sku": line.get("sku"),
            "text": line.get("text"),
            "type": force_text(line["type"].label) if idx not in text_lines else "text"
        } for idx, line in enumerate(serializer.validated_data["lines"])]

        data = {
            "shop": {
                "selected": {
                    "id": shop.id,
                    "name": shop.name,
                    "currency": shop.currency,
                    "priceIncludeTaxes": shop.prices_include_tax
                }
            },
            "methods": {
                "shippingMethod": {"id": getattr(serializer.validated_data["shipping_method"], "id", None)},
                "paymentMethod": {"id": getattr(serializer.validated_data["payment_method"], "id", None)},
            },
            "lines": lines
        }
        if customer:
            data["customer"] = {
                "id": serializer.validated_data["customer"].id,
                "billingAddress": encode_address(customer.default_billing_address),
                "shippingAddress": encode_address(customer.default_shipping_address),
            }

        joc = JsonOrderCreator()
        order = joc.create_order_from_state(data, creator=request.user)
        if not order:
            return JsonResponse({
                "status": "error",
                "errors": [{
                    "message": err.message,
                    "code": err.code
                } for err in joc.errors]
            }, status=400)

        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
Example #5
0
    def create(self, request, *args, **kwargs):
        text_lines = set()
        for idx, line in enumerate(request.data.get("lines", [])):
            if "product" not in line:
                line["product"] = None
            if line.get("type") == "text":
                line["type"] = "other"
                text_lines.add(idx)
        request.data["orderer"] = None
        request.data["modified_by"] = None
        request.data["creator"] = request.user.pk
        if 'shipping_method' not in request.data:
            request.data['shipping_method'] = None
        if 'payment_method' not in request.data:
            request.data['payment_method'] = None
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        shop = Shop.objects.get(pk=serializer.data["shop"])
        if serializer.data.get('customer'):
            customer = Contact.objects.get(pk=serializer.data["customer"])
        else:
            customer = None
        lines = [{
            "id": (idx + 1),
            "quantity": line["quantity"],
            "product": {
                "id": line["product"]
            },
            "baseUnitPrice": line.get("base_unit_price_value"),
            "unitPrice": line.get("base_unit_price_value") if line["type"].label == "other" else None,
            "discountAmount": line.get("discount_amount_value", 0),
            "sku": line.get("sku"),
            "text": line.get("text"),
            "type": force_text(line["type"].label) if idx not in text_lines else "text"
        } for idx, line in enumerate(serializer.data["lines"])]

        data = {
            "shop": {
                "selected": {
                    "id": shop.id,
                    "name": shop.name,
                    "currency": shop.currency,
                    "priceIncludeTaxes": shop.prices_include_tax
                }
            },
            "methods": {
                "shippingMethod": {"id": serializer.data["shipping_method"]},
                "paymentMethod": {"id": serializer.data["payment_method"]},
            },
            "lines": lines
        }
        if customer:
            data["customer"] = {
                "id": serializer.data["customer"],
                "billingAddress": encode_address(customer.default_billing_address),
                "shippingAddress": encode_address(customer.default_shipping_address),
            }
        joc = JsonOrderCreator()
        order = joc.create_order_from_state(data, creator=request.user)
        if not order:
            return JsonResponse({
                "status": "error",
                "errors": [{
                    "message": err.message,
                    "code": err.code
                } for err in joc.errors]
            }, status=400)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
Example #6
0
    def create(self, request, *args, **kwargs):
        text_lines = set()
        for idx, line in enumerate(request.data.get("lines", [])):
            if "product" not in line:
                line["product"] = None
            if line.get("type") == "text":
                line["type"] = "other"
                text_lines.add(idx)
        request.data["orderer"] = None
        request.data["modified_by"] = None
        request.data["creator"] = request.user.pk
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        shop = Shop.objects.get(pk=serializer.data["shop"])
        customer = Contact.objects.get(pk=serializer.data["customer"])
        lines = [{
            "id": (idx + 1),
            "quantity": line["quantity"],
            "product": {
                "id": line["product"]
            },
            "baseUnitPrice": line.get("base_unit_price_value"),
            "unitPrice": line.get("base_unit_price_value") if line["type"].label == "other" else None,
            "discountAmount": line.get("discount_amount_value", 0),
            "sku": line.get("sku"),
            "text": line.get("text"),
            "type": force_text(line["type"].label) if idx not in text_lines else "text"
        } for idx, line in enumerate(serializer.data["lines"])]

        data = {
            "shop": {
                "selected": {
                    "id": shop.id,
                    "name": shop.name,
                    "currency": shop.currency,
                    "priceIncludeTaxes": shop.prices_include_tax
                }
            },
            "methods": {
                "shippingMethod": {"id": serializer.data["shipping_method"]},
                "paymentMethod": {"id": serializer.data["payment_method"]},
            },
            "customer": {
                "id": serializer.data["customer"],
                "billingAddress": encode_address(customer.default_billing_address),
                "shippingAddress": encode_address(customer.default_shipping_address),
            },
            "lines": lines
        }
        joc = JsonOrderCreator()
        order = joc.create_order_from_state(data, creator=request.user)
        if not order:
            return JsonResponse({
                "status": "error",
                "errors": [{
                    "message": err.message,
                    "code": err.code
                } for err in joc.errors]
            }, status=400)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)