示例#1
0
    def get(self):
        user = users.get_current_user()
        restaurant_name = self.request.get('name')

        # generate a cart
        cart_query = CartModel.query(CartModel.restaurant_name==restaurant_name, CartModel.user==user).fetch()
        if (len(cart_query)<1):
            cart = CartModel()
            cart.user = user
            cart.restaurant_name = restaurant_name
            cart.orders = []
            cart.total = 0
            cart.put()

        # self.response.write(restaurant_name)
        restaurant = RestaurantModel.query(RestaurantModel.name==restaurant_name, RestaurantModel.owner==user).fetch()[0]
        key = restaurant.key
        dish_query = DishModel.query(ancestor=key).fetch()
        dish_info = []
        if (len(dish_query)>0):
            for dish in dish_query:
                tmp = "/view_picture/%s" % dish.picture_key
                dish_info.append((dish.name, dish.price, tmp, dish.description))
        part =  urllib.urlencode({'name': restaurant_name})
        add_url = '/add_dish?%s' % part
        # self.response.write(restaurant_name)
        template = JINJA_ENVIRONMENT.get_template('templates/viewdishes.html')
        template_values ={
            'dish_query_len': len(dish_query),
            'dish_info': dish_info,
            'add_url': add_url,
            'restaurant_name': restaurant_name
        }
        self.response.write(template.render(template_values))
示例#2
0
 def get(self):
     user = users.get_current_user()
     restaurant_name = self.request.get('restaurant_name')
     dish_name = self.request.get('dish_name')
     dish_price = float(self.request.get('dish_price'))
     quantity = int(self.request.get('quantity'))
     found = False
     restaurant = RestaurantModel.query(RestaurantModel.name==restaurant_name).fetch()[0]
     dish_query = DishModel.query(DishModel.name==dish_name, ancestor=restaurant.key).fetch()
     # self.response.write(dish_query[0].name)
     if (len(dish_query)>0):
         dish = dish_query[0]
         cart_query = CartModel.query(CartModel.user==user, CartModel.restaurant_name==restaurant_name).fetch()
         if (len(cart_query)>0):
             cart = cart_query[0]
             name = []
             for order_key in cart.orders:
                 # order = OrderModel.get_by_id(order_key.id())
                 order = order_key.get()
                 dish_tmp = order.dish.get()
                 # name.append(order.dish.get().name)
                 if (dish_tmp.name==dish_name):
                     # self.response.write("get!!")
                     order.number = order.number + quantity
                     order.put()
                     cart.total = cart.total + dish_price*quantity
                     cart.put()
                     found = True
                     self.redirect("/order?name=%s" % restaurant.name)
             if (found == False):
                 new_order = OrderModel()
                 new_order.number = quantity
                 new_order.dish = dish.key
                 key = new_order.put()
                 cart.orders.append(key)
                 cart.total = cart.total + dish_price*quantity
                 cart.put()
                 self.redirect("/order?name=%s" % restaurant.name)
         else:
             new_order = OrderModel()
             new_order.number = quantity
             new_order.dish = dish.key
             key = new_order.put()
             cart = CartModel()
             cart.orders = []
             cart.orders.append(key)
             cart.total = quantity*dish.price
             cart.restaurant_name = restaurant_name
             cart.user = user
             cart.put()
             self.redirect("/order?name=%s" % restaurant.name)
             # self.redirect('/main')
     else:
         self.redirect('/main')