def add_tariff(self, data, session, curs=None): # checking parent tariff exist pt_id = data['parent_tariff_id'] currency = data.get('currency', None) t_data = { 'environment_id': session.environment_id, 'parent_tariff_id': pt_id, 'name': data['name'], 'type': data['type'], 'status': data['status'] } if pt_id is not None: if currency is not None: raise NonParentTariffWithCurrency else: t_f = TariffFilter(session, {'id': pt_id}, {}, None) t_f.filter_one_obj(curs) elif currency is None: raise ParentTariffWithoutCurrency else: c_f = CurrencyFilter({'code': currency}, {}, None) c = c_f.filter_one_obj(curs) t_data['currency_id'] = c.id t = Tariff(**t_data) mapping.insert(curs, t) return response_ok(id=t.id)
def modify_tariff(self, data, session, curs=None): f = TariffFilter(session, {'id': data.get('id')}, {}, None) loader = partial(f.filter_one_obj, curs, for_update=True) if 'new_currency' in data: curr = data.pop('new_currency') if curr is not None: c_f = CurrencyFilter({'code': curr}, {}, None) c = c_f.filter_one_obj(curs) curr_id = c.id else: curr_id = None data['new_currency_id'] = curr_id try: updated_objs = self.update_obj(curs, data, loader) t = updated_objs[0] # checking only parent tariff with currency if t.parent_tariff_id is None and t.currency_id is None: raise ParentTariffWithoutCurrency if t.currency_id is not None and t.parent_tariff_id is not None: raise NonParentTariffWithCurrency # checking tariff cycle all_ts_f = TariffFilter(session, {}, {}, None) all_ts = all_ts_f.filter_objs(curs) all_ts_idx = build_index(all_ts) all_curs_idx = self._all_curs_idx(curs) self._tariffs_chain_data(all_ts_idx, t, all_curs_idx) except DataIntegrityError: raise HelixtariffObjectAlreadyExists('Tariff %s already exists' % data.get('new_name')) return response_ok()
def add_tariff(self, data, session, curs=None): # checking parent tariff exist pt_id = data['parent_tariff_id'] currency = data.get('currency', None) t_data = {'environment_id': session.environment_id, 'parent_tariff_id': pt_id, 'name': data['name'], 'type': data['type'], 'status': data['status']} if pt_id is not None: if currency is not None: raise NonParentTariffWithCurrency else: t_f = TariffFilter(session, {'id': pt_id}, {}, None) t_f.filter_one_obj(curs) elif currency is None: raise ParentTariffWithoutCurrency else: c_f = CurrencyFilter({'code': currency}, {}, None) c = c_f.filter_one_obj(curs) t_data['currency_id'] = c.id t = Tariff(**t_data) mapping.insert(curs, t) return response_ok(id=t.id)
def _all_curs_idx(self, curs): all_curs_f = CurrencyFilter({}, {}, None) all_curs = all_curs_f.filter_objs(curs) return build_index(all_curs)