Example #1
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
Example #2
0
    def _region_map2list(cls, region_struct):
        if not region_struct:
            return [], [], []

        # get ids
        city_ids = region_struct.keys()
        region_group_ids = reduce(lambda l1, l2: l1 + l2,
                                  [x.keys() for x in region_struct.values()])
        region_ids = []
        for r_ids_list in region_struct.values():
            for r_ids in r_ids_list.values():
                region_ids.extend(r_ids)

        # get names
        cities = city_base.mget(city_ids)
        region_groups = rg_base.mget(region_group_ids)
        regions = region_base.mget(region_ids)

        return [{'city_id': city.id, 'city_name': city.name}
                for city in cities],\
               [{'region_group_id': rg.id, 'region_group_name': rg.name}
                for rg in region_groups],\
               [{'region_id': region.id, 'region_name': region.name}
                for region in regions]
Example #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