Esempio n. 1
0
def set_discount():
    """
    设置食物折扣
    """
    restaurant_ids, category_ids, food_ids, restaurant_id, category_id, \
    food_id = parse_args1()
    discount = float(request.json['discount'])
    with thrift_client('ers') as ers:
        for _id in restaurant_ids:
            ers.set_restaurant_food_eleme_buy_discount(_id, discount)
        _food_ids = copy.copy(food_ids)
        for _id in category_ids:
            food_query = thirdparty_svc.ers.TFoodQuery()
            food_query.category_id = _id
            food_query.is_valid = True
            food_query.limit = 1000
            foods = ers.query_food(food_query)
            _food_ids.extend([food.id for food in foods])
        foods = ers.mget_food(_food_ids)
        for food in foods:
            fix_food_b2f(food)
            food.attribute['eleme_buy_discount'] = discount
            food.attribute = jsonpickle_dumps(food.attribute)
            for k in food.__dict__.iterkeys():
                if k not in ['id', 'attribute']:
                    setattr(food, k, None)
            result = ers.save_food(food.id, food)
    return ''
Esempio n. 2
0
def set_discount():
    """
    设置食物折扣
    """
    restaurant_ids, category_ids, food_ids, restaurant_id, category_id, \
    food_id = parse_args1()
    discount = float(request.json['discount'])
    with thrift_client('ers') as ers:
        for _id in restaurant_ids:
            ers.set_restaurant_food_eleme_buy_discount(_id, discount)
        _food_ids = copy.copy(food_ids)
        for _id in category_ids:
            food_query = thirdparty_svc.ers.TFoodQuery()
            food_query.category_id = _id
            food_query.is_valid = True
            food_query.limit = 1000
            foods = ers.query_food(food_query)
            _food_ids.extend([food.id for food in foods])
        foods = ers.mget_food(_food_ids)
        for food in foods:
            fix_food_b2f(food)
            food.attribute['eleme_buy_discount'] = discount
            food.attribute = jsonpickle_dumps(food.attribute)
            for k in food.__dict__.iterkeys():
                if k not in ['id', 'attribute']:
                    setattr(food, k, None)
            result = ers.save_food(food.id, food)
    return ''
Esempio n. 3
0
def foods(category_id):
    """
    获取食物分类下的所有食物
    """
    category_id = int(category_id)
    foods = category_base.foods(category_id)
    for food in foods:
        fix_food_b2f(food)
    return foods
Esempio n. 4
0
def batch_add_menu():
    """
    按照一定格式批量编辑菜单.
    """
    menu = request.json['menu']
    restaurant_id = request.json['restaurant_id']
    new_categories_name = set([
        category for name, price, category in menu
        if isinstance(category, basestring)
    ])
    category_id_map = {}
    new_foods = []
    new_categories = []
    with thrift_client('ers') as ers:
        for new_category_name in new_categories_name:
            category = thirdparty_svc.ers.TFoodCategory()
            category.name = new_category_name
            category.restaurant_id = restaurant_id
            category.weight = 0
            new_category_id = ers.save_food_category(None, category)
            new_category = ers.get_food_category(new_category_id)
            new_categories.append(new_category)
            category_id_map[new_category_name] = new_category_id
        for name, price, category in menu:
            if not name and not price:
                continue
            if isinstance(category, basestring):
                category = category_id_map[category]
            food = thirdparty_svc.ers.TFood()
            food.name = name
            food.price = price
            food.restaurant_id = restaurant_id
            food.category_id = category
            new_food_id = ers.save_food(None, food)
            new_food = ers.master_get_food(new_food_id)
            new_foods.append(new_food)
    other_foods = []
    for category in new_categories:
        category._foods = []
    new_category_id_map = {c.id: c for c in new_categories}
    for food in new_foods:
        fix_food_b2f(food)
        if food.category_id in new_category_id_map.keys():
            new_category_id_map[food.category_id]._foods.append(food)
        else:
            other_foods.append(food)
    result = [{
        'food_category': category,
        'foods': copy.copy(category._foods)
    } for category in new_categories]
    for category in new_categories:
        category._activity = [None] * 7
    if other_foods:
        result.append({'foods': other_foods})
    for category in new_categories:
        delattr(category, '_foods')
    return result
Esempio n. 5
0
def batch_add_menu():
    """
    按照一定格式批量编辑菜单.
    """
    menu = request.json['menu']
    restaurant_id = request.json['restaurant_id']
    new_categories_name = set([category for name, price, category in menu
                               if isinstance(category, basestring)])
    category_id_map = {}
    new_foods = []
    new_categories = []
    with thrift_client('ers') as ers:
        for new_category_name in new_categories_name:
            category = thirdparty_svc.ers.TFoodCategory()
            category.name = new_category_name
            category.restaurant_id = restaurant_id
            category.weight = 0
            new_category_id = ers.save_food_category(None, category)
            new_category = ers.get_food_category(new_category_id)
            new_categories.append(new_category)
            category_id_map[new_category_name] = new_category_id
        for name, price, category in menu:
            if not name and not price:
                continue
            if isinstance(category, basestring):
                category = category_id_map[category]
            food = thirdparty_svc.ers.TFood()
            food.name = name
            food.price = price
            food.restaurant_id = restaurant_id
            food.category_id = category
            new_food_id = ers.save_food(None, food)
            new_food = ers.master_get_food(new_food_id)
            new_foods.append(new_food)
    other_foods = []
    for category in new_categories:
        category._foods = []
    new_category_id_map = {c.id: c for c in new_categories}
    for food in new_foods:
        fix_food_b2f(food)
        if food.category_id in new_category_id_map.keys():
            new_category_id_map[food.category_id]._foods.append(food)
        else:
            other_foods.append(food)
    result = [
        {'food_category': category, 'foods': copy.copy(category._foods)}
        for category in new_categories]
    for category in new_categories:
        category._activity = [None]*7
    if other_foods:
        result.append({'foods': other_foods})
    for category in new_categories:
        delattr(category, '_foods')
    return result
Esempio n. 6
0
def menu(restaurant_id):
    """
    获取餐厅菜单
    """
    with thrift_client('ers') as ers:
        categories = ers.query_food_category_with_foods(restaurant_id)
        for category in categories:
            _activity_ids = get_activity_ids(category.foods)
            # _activities = _week_activity_ids_to_full(_activity_ids)
            category.food_category._activity = _activity_ids
            category.food_category._activity_objs = [food_act_base.get(_id) if _id else None for _id in _activity_ids]
            for food in category.foods:
                fix_food_b2f(food)
        categories = [category for category in categories
                      if category.food_category.id != GARNISH_CATEGORY_ID]
        return categories
Esempio n. 7
0
def menu(restaurant_id):
    """
    获取餐厅菜单
    """
    with thrift_client('ers') as ers:
        categories = ers.query_food_category_with_foods(restaurant_id)
        for category in categories:
            _activity_ids = get_activity_ids(category.foods)
            # _activities = _week_activity_ids_to_full(_activity_ids)
            category.food_category._activity = _activity_ids
            category.food_category._activity_objs = [
                food_act_base.get(_id) if _id else None
                for _id in _activity_ids
            ]
            for food in category.foods:
                fix_food_b2f(food)
        categories = [
            category for category in categories
            if category.food_category.id != GARNISH_CATEGORY_ID
        ]
        return categories
Esempio n. 8
0
def get(food_id):
    food_id = int(food_id)
    with thrift_client('ers') as ers:
        food = ers.master_get_food(food_id)
        fix_food_b2f(food)
        return food
Esempio n. 9
0
def get(food_id):
    food_id = int(food_id)
    with thrift_client('ers') as ers:
        food = ers.master_get_food(food_id)
        fix_food_b2f(food)
        return food