Exemplo n.º 1
0
def create_random_order(customer=None, products=(), completion_probability=0):
    if not customer:
        customer = Contact.objects.all().order_by("?").first()

    if not customer:
        raise ValueError("No valid contacts")

    shop = get_default_shop()

    request = apply_request_middleware(RequestFactory().get("/"),
                                       customer=customer)

    context = PriceTaxContext.from_request(request)
    source = OrderSource()
    source.customer = customer
    source.customer_comment = "Mock Order"

    if customer.default_billing_address and customer.default_shipping_address:
        source.billing_address = customer.default_billing_address
        source.shipping_address = customer.default_shipping_address
    else:
        source.billing_address = create_random_address()
        source.shipping_address = create_random_address()
    source.order_date = now() - datetime.timedelta(days=random.uniform(0, 400))

    source.shop = shop
    source.language = customer.language
    source.status = get_initial_order_status()

    if not products:
        products = list(Product.objects.list_visible(source.shop, customer).order_by("?")[:40])

    source.lines = []
    for i in range(random.randint(3, 10)):
        product = random.choice(products)
        quantity = random.randint(1, 5)
        price_info = product.get_price_info(context, quantity=quantity)
        shop_product = product.get_shop_instance(source.shop)
        supplier = shop_product.suppliers.first()
        line = SourceLine(
            type=OrderLineType.PRODUCT,
            product=product,
            supplier=supplier,
            quantity=quantity,
            unit_price=price_info.unit_base_price,
            total_discount=price_info.discount_amount,
            sku=product.sku,
            text=product.safe_translation_getter("name", any_language=True)
        )
        assert line.total_price == price_info.price
        source.lines.append(line)
    with atomic():
        oc = OrderCreator(request)
        order = oc.create_order(source)
        if random.random() < completion_probability:
            order.create_shipment_of_all_products()
            # also set complete
            order.status = OrderStatus.objects.get_default_complete()
            order.save(update_fields=("status",))
        return order
Exemplo n.º 2
0
def update_order_line_from_product(request, order_line, product, quantity=1, supplier=None):
    """
    This is a convenience method for simple applications.
    """

    if order_line.pk:  # pragma: no cover
        raise Exception("set_from_product may not be used on saved lines")

    if not product:  # pragma: no cover

        raise Exception("set_from_product may not be used without product")

    # TODO: (TAX) Taxes in update_order_line_from_product
    context = PriceTaxContext.from_request(request)
    price = product.get_price(context, quantity=quantity)
    order_line.supplier = supplier
    order_line.type = OrderLineType.PRODUCT
    order_line.product = product
    order_line.quantity = quantity
    order_line.sku = product.sku
    order_line.name = product.safe_translation_getter("name") or product.sku
    order_line.accounting_identifier = product.accounting_identifier
    order_line.require_verification = bool(getattr(product, "require_verification", False))
    order_line.verified = False
    order_line.unit_price = price
Exemplo n.º 3
0
def create_random_order(customer=None, products=(), completion_probability=0):
    if not customer:
        customer = Contact.objects.all().order_by("?").first()

    if not customer:
        raise ValueError("No valid contacts")

    shop = get_default_shop()

    request = apply_request_middleware(RequestFactory().get("/"),
                                       customer=customer)

    context = PriceTaxContext.from_request(request)
    source = OrderSource()
    source.customer = customer
    source.customer_comment = "Mock Order"

    if customer.default_billing_address and customer.default_shipping_address:
        source.billing_address = customer.default_billing_address
        source.shipping_address = customer.default_shipping_address
    else:
        source.billing_address = create_random_address()
        source.shipping_address = create_random_address()
    source.order_date = now() - datetime.timedelta(days=random.uniform(0, 400))

    source.shop = shop
    source.language = customer.language
    source.status = get_initial_order_status()

    if not products:
        products = list(
            Product.objects.list_visible(source.shop,
                                         customer).order_by("?")[:40])

    source.lines = []
    for i in range(random.randint(3, 10)):
        product = random.choice(products)
        quantity = random.randint(1, 5)
        price = product.get_price(context, quantity=quantity)
        shop_product = product.get_shop_instance(source.shop)
        supplier = shop_product.suppliers.first()
        line = SourceLine(type=OrderLineType.PRODUCT,
                          product=product,
                          supplier=supplier,
                          quantity=quantity,
                          unit_price=price,
                          sku=product.sku,
                          text=product.safe_translation_getter(
                              "name", any_language=True))
        source.lines.append(line)
    with atomic():
        oc = OrderCreator(request)
        order = oc.create_order(source)
        if random.random() < completion_probability:
            order.create_shipment_of_all_products()
            # also set complete
            order.status = OrderStatus.objects.get_default_complete()
            order.save(update_fields=("status", ))
        return order