Exemple #1
0
def change_director_region(user_id, old_region, new_region):
    city_diff = _diff(old_region.city_ids, new_region.city_ids)
    region_group_diff = _diff(old_region.region_group_ids, new_region.region_group_ids)
    region_diff = _diff(old_region.region_ids, new_region.region_ids)

    query_dict = {}
    if city_diff:
        query_dict["city_ids"] = city_diff
    if region_group_diff:
        query_dict["region_group_ids"] = region_group_diff
    if region_diff:
        query_dict["region_ids"] = region_diff

    if not query_dict:
        return

    dir_records = rst_dir_base.query_restaurant_director(director_ids=[user_id], offset=0, limit=1000)
    dir_rst_ids = [d.restaurant_id for d in dir_records]
    query_dict["restaurant_ids"] = dir_rst_ids
    query_dict["offset"] = 0
    query_dict["limit"] = 1000

    restaurants = rst_dir_base.search_restaurants(**query_dict)
    rst_ids = [r["id"] for r in restaurants[0]]
    rst_dir_base.rm_restaurant_director(user_id, rst_ids)
Exemple #2
0
def change_director_region(user_id, old_region, new_region):
    city_diff = _diff(old_region.city_ids, new_region.city_ids)
    region_group_diff = _diff(old_region.region_group_ids,
                              new_region.region_group_ids)
    region_diff = _diff(old_region.region_ids, new_region.region_ids)

    query_dict = {}
    if city_diff:
        query_dict['city_ids'] = city_diff
    if region_group_diff:
        query_dict['region_group_ids'] = region_group_diff
    if region_diff:
        query_dict['region_ids'] = region_diff

    if not query_dict:
        return

    dir_records = rst_dir_base.query_restaurant_director(
        director_ids=[user_id], offset=0, limit=1000)
    dir_rst_ids = [d.restaurant_id for d in dir_records]
    query_dict['restaurant_ids'] = dir_rst_ids
    query_dict['offset'] = 0
    query_dict['limit'] = 1000

    restaurants = rst_dir_base.search_restaurants(**query_dict)
    rst_ids = [r['id'] for r in restaurants[0]]
    rst_dir_base.rm_restaurant_director(user_id, rst_ids)
Exemple #3
0
def get_notifications(args):
    city_ids, region_group_ids, region_ids = get_direct_struct()

    if city_ids == [] and region_ids == [] and region_ids == []:
        return [], 0

    rst_id = args.pop('restaurant_id', None)
    if rst_id is not None:
        args['restaurant_ids'] = [rst_id]

    page_no, page_size = get_paging_params()
    restaurants, total_num = director_base.search_restaurants(
        city_ids=city_ids, region_group_ids=region_group_ids,
        region_ids=region_ids, offset=(page_no - 1) * page_size,
        limit=page_size, **args)

    if not restaurants:
        return [], 0

    restaurant_ids = [r['id'] for r in restaurants]
    city_ids = list(set([r['city_id'] for r in restaurants]))
    notifications = director_base.query_restaurant_director(
        restaurant_ids, [current_user.id, ])
    notification_map = {n.restaurant_id: [n.notice_enabled, n.in_charge]
                        for n in notifications}

    result = []
    city_map = city_base.mget(city_ids, return_map=True)
    for restaurant in restaurants:
        if city_map.get(restaurant['city_id'], None) is None:
            continue

        with thrift_client('ers') as ers:
            try:
                region = ers.get_region_by_restaurant(restaurant['id'])
                region_name = region.name
            except Exception as e:
                region_name = ''
                region_group_name = ''
            else:
                try:
                    region_group_name = ers.get_region_group_by_region(
                        region.id).name
                except Exception as e:
                    region_group_name = ''

        result.append({
            'restaurant_id': restaurant['id'],
            'restaurant_name': restaurant['name'],
            'city_id': restaurant['city_id'],
            'city_name': city_map[restaurant['city_id']].name,
            'region_group_name': region_group_name,
            'region_name': region_name,
            'notice_enabled': notification_map.get(restaurant['id'], [0])[0],
            'in_charge': notification_map.get(restaurant['id'], [None, 0])[1]
        })

    return result, total_num
Exemple #4
0
def get_notifications(args):
    city_ids, region_group_ids, region_ids = get_direct_struct()

    if city_ids == [] and region_ids == [] and region_ids == []:
        return [], 0

    rst_id = args.pop('restaurant_id', None)
    if rst_id is not None:
        args['restaurant_ids'] = [rst_id]

    page_no, page_size = get_paging_params()
    restaurants, total_num = director_base.search_restaurants(
        city_ids=city_ids,
        region_group_ids=region_group_ids,
        region_ids=region_ids,
        offset=(page_no - 1) * page_size,
        limit=page_size,
        **args)

    if not restaurants:
        return [], 0

    restaurant_ids = [r['id'] for r in restaurants]
    city_ids = list(set([r['city_id'] for r in restaurants]))
    notifications = director_base.query_restaurant_director(
        restaurant_ids, [
            current_user.id,
        ])
    notification_map = {
        n.restaurant_id: [n.notice_enabled, n.in_charge]
        for n in notifications
    }

    result = []
    city_map = city_base.mget(city_ids, return_map=True)
    for restaurant in restaurants:
        if city_map.get(restaurant['city_id'], None) is None:
            continue

        with thrift_client('ers') as ers:
            try:
                region = ers.get_region_by_restaurant(restaurant['id'])
                region_name = region.name
            except Exception as e:
                region_name = ''
                region_group_name = ''
            else:
                try:
                    region_group_name = ers.get_region_group_by_region(
                        region.id).name
                except Exception as e:
                    region_group_name = ''

        result.append({
            'restaurant_id':
            restaurant['id'],
            'restaurant_name':
            restaurant['name'],
            'city_id':
            restaurant['city_id'],
            'city_name':
            city_map[restaurant['city_id']].name,
            'region_group_name':
            region_group_name,
            'region_name':
            region_name,
            'notice_enabled':
            notification_map.get(restaurant['id'], [0])[0],
            'in_charge':
            notification_map.get(restaurant['id'], [None, 0])[1]
        })

    return result, total_num