def delete_user_tariffs(self, data, session, curs=None): f = UserTariffFilter(session, { 'user_id': data['user_id'], 'tariff_ids': data['tariff_ids'] }, {}, ['id']) mapping.delete_objects(curs, f.filter_objs(curs, for_update=True)) return response_ok()
def _price_info(self, session, curs, data, rule_field_name): # Getting required data for price calculation to_id = data['tariffication_object_id'] to_f = TarifficationObjectFilter(session, {'id': to_id}, {}, None) to = to_f.filter_one_obj(curs) # TODO: handle user tariffs all_ts_f = TariffFilter(session, {}, {}, None) all_ts = all_ts_f.filter_objs(curs) all_ts_idx = build_index(all_ts) t_id = data['tariff_id'] if t_id not in all_ts_idx: raise TariffNotFound(tariff_id=t_id) if 'user_id' in data: ut_f = UserTariffFilter(session, { 'user_id': data['user_id'], 'tariff_id': t_id }, {}, None) ut_f.filter_one_obj(curs) all_curs_idx = self._all_curs_idx(curs) t = all_ts_idx[t_id] ts_chain_data = self._tariffs_chain_data(all_ts_idx, t, all_curs_idx) ts_chain_ids = [t_data['id'] for t_data in ts_chain_data] # Fetching active rules r_f = RuleFilter( session, { 'tariff_ids': ts_chain_ids, 'tariffication_object_id': to_id, 'status': Rule.STATUS_ACTIVE }, {}, None) rs = r_f.filter_objs(curs) rs_to_t_idx = build_complex_index( rs, ('tariffication_object_id', 'tariff_id')) # Generation price info calculation_ctx = data.get('calculation_context', {}) r, t = self._find_rule_tariff(to, ts_chain_ids, all_ts_idx, rs_to_t_idx, rule_field_name, calculation_ctx) if r is None: raise PriceNotFound price_info = self._calculate_rule_info(r, rule_field_name, t, calculation_ctx) price_info['tariffication_object_id'] = to.id price_info['tariffication_object_name'] = to.name if 'calculation_context' in data: price_info['calculation_context'] = calculation_ctx return price_info
def get_user_tariffs(self, data, session, curs=None): ut_f = UserTariffFilter(session, data['filter_params'], data['paging_params'], data.get('ordering_params')) uts = ut_f.filter_objs(curs) uts_data = {} for ut in uts: if ut.user_id not in uts_data: uts_data[ut.user_id] = [] uts_data[ut.user_id].append(ut.tariff_id) uts_info = [{'user_id': k, 'tariff_ids': v} for k, v in uts_data.items()] uts_info.sort(key=lambda x: x['user_id']) return response_ok(user_tariffs=uts_info, total=len(uts_info))
def get_user_tariffs(self, data, session, curs=None): ut_f = UserTariffFilter(session, data['filter_params'], data['paging_params'], data.get('ordering_params')) uts = ut_f.filter_objs(curs) uts_data = {} for ut in uts: if ut.user_id not in uts_data: uts_data[ut.user_id] = [] uts_data[ut.user_id].append(ut.tariff_id) uts_info = [{ 'user_id': k, 'tariff_ids': v } for k, v in uts_data.items()] uts_info.sort(key=lambda x: x['user_id']) return response_ok(user_tariffs=uts_info, total=len(uts_info))
def _price_info(self, session, curs, data, rule_field_name): # Getting required data for price calculation to_id = data['tariffication_object_id'] to_f = TarifficationObjectFilter(session, {'id': to_id}, {}, None) to = to_f.filter_one_obj(curs) # TODO: handle user tariffs all_ts_f = TariffFilter(session, {}, {}, None) all_ts = all_ts_f.filter_objs(curs) all_ts_idx = build_index(all_ts) t_id = data['tariff_id'] if t_id not in all_ts_idx: raise TariffNotFound(tariff_id=t_id) if 'user_id' in data: ut_f = UserTariffFilter(session, {'user_id': data['user_id'], 'tariff_id': t_id}, {}, None) ut_f.filter_one_obj(curs) all_curs_idx = self._all_curs_idx(curs) t = all_ts_idx[t_id] ts_chain_data = self._tariffs_chain_data(all_ts_idx, t, all_curs_idx) ts_chain_ids = [t_data['id'] for t_data in ts_chain_data] # Fetching active rules r_f = RuleFilter(session, {'tariff_ids': ts_chain_ids, 'tariffication_object_id': to_id, 'status': Rule.STATUS_ACTIVE}, {}, None) rs = r_f.filter_objs(curs) rs_to_t_idx = build_complex_index(rs, ('tariffication_object_id', 'tariff_id')) # Generation price info calculation_ctx = data.get('calculation_context', {}) r, t = self._find_rule_tariff(to, ts_chain_ids, all_ts_idx, rs_to_t_idx, rule_field_name, calculation_ctx) if r is None: raise PriceNotFound price_info = self._calculate_rule_info(r, rule_field_name, t, calculation_ctx) price_info['tariffication_object_id'] = to.id price_info['tariffication_object_name'] = to.name if 'calculation_context' in data: price_info['calculation_context'] = calculation_ctx return price_info
def get_tariffs_prices(self, data, session, curs=None): f_params = data['filter_params'] if 'user_id' in f_params: user_id = f_params['user_id'] ts_ids = f_params['ids'] ut_f = UserTariffFilter(session, { 'user_id': user_id, 'tariff_ids': ts_ids }, {}, None) uts = ut_f.filter_objs(curs) uts_tariff_ids = [ut.tariff_id for ut in uts] ts_ids = filter(lambda x: x in uts_tariff_ids, ts_ids) f_params['ids'] = ts_ids t_f = TariffFilter(session, data['filter_params'], data['paging_params'], data.get('ordering_params')) ts, total = t_f.filter_counted(curs) ts_idx = build_index(ts) ts_ids = filter(lambda x: x in ts_idx, data['filter_params']['ids']) all_tos_f = TarifficationObjectFilter(session, {}, {}, None) all_tos = all_tos_f.filter_objs(curs) all_tos_idx = build_index(all_tos) all_ts_f = TariffFilter(session, {}, {}, None) all_ts = all_ts_f.filter_objs(curs) all_ts_idx = build_index(all_ts) active_r_f = RuleFilter(session, {}, {}, None) active_rs = active_r_f.filter_objs(curs) active_rs_to_t_idx = build_complex_index( active_rs, ('tariffication_object_id', 'tariff_id')) tariffs_prices = [] calculation_ctxs = data['calculation_contexts'] for t_id in ts_ids: tariff_price_info = self._tariff_price_info( session, curs, ts_idx[t_id], all_tos_idx, all_ts_idx, active_rs_to_t_idx, calculation_ctxs) tariffs_prices.append(tariff_price_info) return response_ok(tariffs=tariffs_prices, total=total)
def get_tariffs_prices(self, data, session, curs=None): f_params = data['filter_params'] if 'user_id' in f_params: user_id = f_params['user_id'] ts_ids = f_params['ids'] ut_f = UserTariffFilter(session, {'user_id': user_id, 'tariff_ids': ts_ids}, {}, None) uts = ut_f.filter_objs(curs) uts_tariff_ids = [ut.tariff_id for ut in uts] ts_ids = filter(lambda x: x in uts_tariff_ids, ts_ids) f_params['ids'] = ts_ids t_f = TariffFilter(session, data['filter_params'], data['paging_params'], data.get('ordering_params')) ts, total = t_f.filter_counted(curs) ts_idx = build_index(ts) ts_ids = filter(lambda x: x in ts_idx, data['filter_params']['ids']) all_tos_f = TarifficationObjectFilter(session, {}, {}, None) all_tos = all_tos_f.filter_objs(curs) all_tos_idx = build_index(all_tos) all_ts_f = TariffFilter(session, {}, {}, None) all_ts = all_ts_f.filter_objs(curs) all_ts_idx = build_index(all_ts) active_r_f = RuleFilter(session, {}, {}, None) active_rs = active_r_f.filter_objs(curs) active_rs_to_t_idx = build_complex_index(active_rs, ('tariffication_object_id', 'tariff_id')) tariffs_prices = [] calculation_ctxs = data['calculation_contexts'] for t_id in ts_ids: tariff_price_info = self._tariff_price_info(session, curs, ts_idx[t_id], all_tos_idx, all_ts_idx, active_rs_to_t_idx, calculation_ctxs) tariffs_prices.append(tariff_price_info) return response_ok(tariffs=tariffs_prices, total=total)
def delete_user_tariffs(self, data, session, curs=None): f = UserTariffFilter(session, {'user_id': data['user_id'], 'tariff_ids': data['tariff_ids']}, {}, ['id']) mapping.delete_objects(curs, f.filter_objs(curs, for_update=True)) return response_ok()