Esempio n. 1
0
def delete(request, order_id = 0):
    """ to delete order by self, who is sponsor
    """
    order = Order.objects.get(pk = order_id)
    if not order:
        raise Http404
    if order.sponsor != request.user:
        messages.warning(request, u"订单不是你发起的,无权删除")
        return redirect("/user/")
    config = Config.get_default()
    if _is_order_shutdown(request, config) is True:
        messages.warning(request, u"订餐系统关闭中,暂时不能删除")
        return redirect("/user/")
    if is_today(datetime.datetime(1, 1, 1).fromtimestamp(order.add_timestamp)) is False:
        logging.debug("order %d is out of time" % order.id)
        messages.warning(request, u"订单已经过期,不允许删除,请联系管理员")
        return redirect("/user/")
    order.delete()
    return redirect("/user/")
Esempio n. 2
0
def ready(request, page = 1):
    """ ready to complete order
    """
    if request.user.is_authenticated() is False:
        return redirect("/login/")
    menus = Menu.objects.filter(id__in = request.session.get("menus", []))
    paginator = Paginator(menus, 20)
    try:
        menus_page = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        menus_page = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        menus_page = paginator.page(paginator.num_pages)
    restaurant = None
    # sum total price
    total_price = 0
    if menus:
        restaurant = menus[0].restaurant
        for menu in menus:
            total_price += menu.price
    # init and check form
    if request.method == "POST":
        form = CommitOrderForm(request.POST)
        if form.is_valid():
            receivers = []
            receiver_names = form.cleaned_data["receivers"].split(" ")
            config = Config.get_default()
            # check fee is validate
            if _is_fee_sufficient(request, config, len(receiver_names), total_price) is False:
                return render_template("order_ready.html", 
                           {"menus_page": menus_page, "restaurant": restaurant, 
                            "total_price": total_price, "form": form}, 
                           request = request)
            if  _is_order_shutdown(request, config) is True:
                return render_template("order_ready.html", 
                           {"menus_page": menus_page, "restaurant": restaurant, 
                            "total_price": total_price, "form": form}, 
                           request = request)
            if _is_restaurant_full(request, restaurant, len(receiver_names)) is True:
                form.set_custom_error(u"餐厅已满")
                return render_template("order_ready.html", 
                           {"menus_page": menus_page, "restaurant": restaurant, 
                            "total_price": total_price, "form": form}, 
                           request = request)
            # begin to order now    
            order = Order(sponsor = request.user, restaurant = restaurant, price = total_price)
            order.save()
            for name in receiver_names:
                user = DjangoUser.objects.filter(username = name)[0]
                user.get_profile().order_meal(order)
                user.get_profile().save()
                receivers.append(user)
            order.owners = receivers
            order.menus = menus
            order.save()
            # update restaurant infomation
            restaurant.add_order_number(len(receiver_names))
            restaurant.save()
            # update menu information
            for menu in menus:
                menu.order_number += len(receiver_names)
                menu.last_order_datetime = datetime.datetime(1, 1, 1).today()
                menu.save()
            # clear session
            request.session["menus"] = set()
            return redirect("/order/%d/" % order.id)       
    else:
        form = CommitOrderForm({"receivers": request.user.username})
    return render_template("order_ready.html", 
                           {"menus_page": menus_page, "restaurant": restaurant, 
                            "total_price": total_price, "form": form}, 
                           request = request)