async def select_by_tracks(cls, track_ids: list = None, point_ids: list or set = None, fields: list = None) -> list: # join j = join(TrackPoints, Track, TrackPoints.track_id == Track.id, isouter=True) # set selected fields if not fields: fields = [TrackPoints, Track] # query to db query = select(fields) # conditions # by track_ids if track_ids: query = query.where(TrackPoints.track_id == any_(track_ids)) # by point_ids if point_ids: query = query.where(TrackPoints.point_id == any_(point_ids)) # add join query = query.select_from(j) # return all records return await DBManager().query_fetch(query)
async def get_entities(self, ids: list, filter_name: str = None, creater_ids: set = None) -> tuple: # result vars result = [] errors = [] # conditions by allowed creaters conditions = await self._get_base_condition() # condition by selector ids if ids: conditions.append(self.entity_cls.id == any_(ids)) # condition by selector name if filter_name: conditions.append(self.entity_cls.name.contains(filter_name)) # condition by selector name if creater_ids: conditions.append(self.entity_cls.id == any_(creater_ids)) # select by conditions records = await self.entity_cls.select_where( str_fields=self.select_fields, conditions=conditions ) self.calc_result(records, ids, result, errors) return result, errors
async def get_entities(self, ids: list, schedule_ids: set = None, customer_ids: set = None, filter_name: str = None, status: str = None) -> tuple: # result vars result = [] errors = [] # conditions by select users conditions = await self._get_base_condition() allowed_schedule_ids = self.allowed_schedule_ids if schedule_ids: # condition by allowed Schedules allowed_schedule_ids = self.allowed_schedule_ids.intersection(schedule_ids) # conditions for select details, by allowed schedule conditions = [self.entity_cls.schedule_id == any_(allowed_schedule_ids)] # condition by selector ids if ids: conditions.append(self.entity_cls.id == any_(ids)) # condition by selector name if customer_ids: conditions.append(self.entity_cls.customer_id == any_(customer_ids)) # condition by selector name if filter_name: conditions.append(self.entity_cls.name.contains(filter_name)) # condition by order status if status: conditions.append(self.entity_cls.status.contains(status)) # select by conditions records = await self.entity_cls.select_where( str_fields=self.select_fields, conditions=conditions ) # ---- result data format ---- # schedule_id: list() # ---------------------------- format_result = dict() # format details for record in records: format_result_data = format_result.setdefault(record['schedule_id'], list()) format_result_data.append(self.get_result_item(record, self.select_fields)) if format_result: result.append(format_result) return result, errors
async def get_entities(self, ids: list, filter_name: str = None, **kwargs) -> tuple: # result success result = [] # result errors errors = [] # conditions by select entities conditions = await self.get_base_condition() # condition by selector ids/all if ids: #TODO check permissions for user # add allowed_ids in conditions conditions.append(self.entity_cls.id == any_(ids)) # condition by selector name if filter_name: conditions.append(self.entity_cls.label.contains(filter_name)) # select by conditions records = await self.entity_cls.select_where( str_fields=self.select_fields, conditions=conditions) # generate result list [ result.append(self.get_result_item(record, self.select_fields)) for record in records ] return result, errors
async def get_entities(self, ids: list, schedule_ids: set = None, filter_name: str = None) -> tuple: result = [] errors = [] # default filter customers only by ids in request allowed_customer_ids = ids # if need specific schedule data if schedule_ids: # condition by allowed Schedules allowed_schedule_ids = self.allowed_schedule_ids.intersection( schedule_ids) # get orders for some schedules order_items = await Order.select_where( cls_fields=[Order.id, Order.schedule_id], conditions=[Order.schedule_id == any_(allowed_schedule_ids)]) # get customer ids form orders allowed_customer_ids = [ order_item['customer_id'] for order_item in order_items ] if ids: allowed_customer_ids = allowed_customer_ids.intersection(ids) # base conditions conditions = await self._get_base_condition() # condition by selector ids if allowed_customer_ids: conditions.append(self.entity_cls.id == any_(allowed_customer_ids)) # condition by selector name if filter_name: conditions.append(self.entity_cls.name.contains(filter_name)) # select by conditions records = await self.entity_cls.select_where( str_fields=self.select_fields, conditions=conditions) self.calc_result(records, ids, result, errors) return result, errors
async def get_entities(self, ids: list, schedule_ids: set = None) -> tuple: # result vars result = [] errors = [] # conditions for select details, by allowed schedule conditions = await self._get_base_condition() # condition by selector ids if ids: conditions.append(self.entity_cls.id == any_(ids)) # condition by schedule_ids # get intersection schedules if schedule_ids: conditions.append(self.entity_cls.schedule_id == any_(schedule_ids)) # select by conditions records = await self.entity_cls.select_where( str_fields=self.select_fields, conditions=conditions ) # ---- result data format ---- # schedule_id: list() # ---------------------------- format_result = dict() # format details for detail_item in records: format_result_data = format_result.setdefault(detail_item['schedule_id'], list()) format_result_data.append(self.get_result_item(detail_item, self.select_fields)) if format_result: result.append(format_result) return result, errors
def __init__(self, allowed_schedule_ids: set, select_fields: set=set(), creater_id: int = -1): """ :param select_fields: set, list fields for result """ super().__init__( entity_cls=SCHDetail, all_fields=( 'id', 'time', 'description', 'members', 'price', 'schedule_id', ), select_fields=select_fields, conditions=[SCHDetail.schedule_id == any_(allowed_schedule_ids)] ) # get creter id for current session self.creater_id = creater_id # allowed schedules self.allowed_schedule_ids = allowed_schedule_ids
async def get_entities(self, ids: list, **kwargs) -> tuple: # result success result = [] # result errors errors = [] # conditions for query conditions = await self._calc_conditions(kwargs.get('conditions')) # select by conditions sel_fields = self.select_fields # check select by ids if ids: sel_fields.add('id') conditions.append(self.entity_cls.id == any_(ids)) # query to db records = await self.entity_cls.select_where(str_fields=sel_fields, conditions=conditions) self.calc_result(records, ids, result, errors) return result, errors
async def select_by_ids(cls, model_ids: list or set): return await DBManager().query_fetch(select([cls]).where(cls.id == any_(model_ids)))
async def get_entities(self, ids: list, schedule_ids: set = None, filter_name: str = None) -> tuple: # result vars result = [] errors = [] # check permissions for schedule_ids by account if schedule_ids: # select schedules by ids schedule_items = await Schedule.select_by_ids( model_ids=schedule_ids) # get allowed schedule_ids ids and errors by schedule_ids allowed_schedule_ids, errs = self.get_allowed_ids_by_list( all_ids=schedule_ids, items=schedule_items) # add ids-errors in all errors errors.extend(errs) # get all schedule_ids by account else: schedule_items = await Schedule.select_all() # allowed schedule_ids allowed_schedule_ids = [ schedule_item['id'] for schedule_item in schedule_items ] # conditions by select users conditions = [] # condition by allowed Fleets conditions.append( self.entity_cls.schedule_id == any_(allowed_schedule_ids)) # condition by selector ids if ids: conditions.append(self.entity_cls.id == any_(ids)) # condition by selector name if filter_name: conditions.append(self.entity_cls.name.contains(filter_name)) # select by conditions records = await self.entity_cls.select_where( str_fields=self.select_fields, conditions=conditions) # ids by selected items select_ids = set() # format data format_result = dict() # generate result list for record in records: select_ids.add(record['id']) format_result.setdefault(record['schedule_id'], []) format_result[record['schedule_id']].append( self.get_result_item(record, self.select_fields)) # add not selected items in errors if ids: # get ids not selected ids = set(ids) ids_diff = ids.difference(select_ids) # add errors by not found ids for id_diff in ids_diff: errors.append( self.get_error_item( selector='id', reason='Schedule or schedule-detail is not found', value=id_diff)) result.append(format_result) return result, errors
async def select_by_ids(cls, item_ids: list): query = select([cls]) if item_ids: query = query.where(cls.id == any_(item_ids)) return await DBManager().query_fetch(query)