コード例 #1
0
def create_order(request):
    customer = request.data['customer']
    data = request.data['list']
    cost = 0
    customer = CustomerProfile.objects.filter(
        Q(customer_id=customer['customer_id']))[0]
    total_count = 0
    order = Order(customer_id=customer.pk)
    order.save()
    for p in data:
        for m in data[p]:
            price = SchemaSeries.objects.filter(name=p)[0].price
            cost += int(data[p][m]) * int(price)
            total_count += int(data[p][m])
            new_melody = ShopItem(melody_name=m,
                                  order=order,
                                  series=p,
                                  price=int(price),
                                  ordered_count=int(data[p][m]),
                                  order_admin_verified_count=int(data[p][m]),
                                  sell_admin_verified_count=int(data[p][m]))
            new_melody.save(force_insert=True)
    order.status = Order.ORDER_ADMIN
    order.cost = cost
    order.last_change_date = timezone.now()
    order.order_id = get_new_order_id()
    order.save()
    return Response()
コード例 #2
0
def checkout(request):
    user = request.user
    data = request.data['list']
    cost = 0
    customer = CustomerProfile.objects.get(user=user)
    order = Order.objects.filter(customer=customer).all().last()
    total_count = 0
    if not order or order.is_checked_out:
        order = Order(customer_id=customer.pk)
        order.save()
    else:
        ShopItem.objects.filter(order=order).all().delete()
    for p in data:
        for m in data[p]:
            price = Series.objects.filter(name=p)[0].price
            cost += int(data[p][m]) * int(price)
            melody = ShopItem.objects.filter(melody_name=m, order=order, series=p).all().first()
            total_count += int(data[p][m])
            if melody:
                melody.ordered_count = int(data[p][m])
                melody.order_admin_verified_count = int(data[p][m])
                melody.save(force_update=True)
            else:
                new_melody = ShopItem(melody_name=m, order=order,
                                      series=p, price=int(price),
                                      ordered_count=int(data[p][m]),
                                      order_admin_verified_count=int(data[p][m])
                                      )
                new_melody.save(force_insert=True)
    order.cost = cost
    order.order_id = get_new_order_id()
    order.save()
    discount_percent = apply_promotions(order, total_count)
    order.discount = discount_percent
    order.save()
    return Response(status=status.HTTP_200_OK)