Пример #1
0
    def get_rules(self, data, session, curs=None):
        r_f = RuleFilter(session, data['filter_params'], data['paging_params'],
                         data.get('ordering_params'))
        rs, total = r_f.filter_counted(curs)
        if total:
            all_to_f = TarifficationObjectFilter(session, {}, {}, None)
            all_tos = all_to_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)
        else:
            all_tos_idx = {}
            all_ts_idx = {}

        def viewer(r):
            t = all_ts_idx[r.tariff_id]
            to = all_tos_idx[r.tariffication_object_id]
            return {
                'id': r.id,
                'tariff_id': t.id,
                'tariff_name': t.name,
                'tariffication_object_id': to.id,
                'tariffication_object_name': to.name,
                'status': r.status,
                'rule': r.rule,
                'draft_rule': r.draft_rule,
                'view_order': r.view_order
            }

        return response_ok(rules=self.objects_info(rs, viewer), total=total)
Пример #2
0
 def delete_tariff(self, data, session, curs=None):
     f = TariffFilter(session, {'id': data['id']}, {}, None)
     try:
         mapping.delete(curs, f.filter_one_obj(curs))
     except ObjectDeletionError:
         raise TariffUsed('Tariff %s used' % data['id'])
     return response_ok()
Пример #3
0
 def delete_tariff(self, data, session, curs=None):
     f = TariffFilter(session, {'id': data['id']}, {}, None)
     try:
         mapping.delete(curs, f.filter_one_obj(curs))
     except ObjectDeletionError:
         raise TariffUsed('Tariff %s used' % data['id'])
     return response_ok()
Пример #4
0
    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)
Пример #5
0
    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
Пример #6
0
    def apply_draft_rules(self, data, session, curs=None):
        t_f = TariffFilter(session, {'id': data['tariff_id']}, {}, None)
        t_f.filter_one_obj(curs)

        f = RuleFilter(session, {'tariff_id': data['tariff_id']}, {}, ['id'])
        rs = f.filter_objs(curs, for_update=True)
        checker = RuleChecker()
        for r in rs:
            if r.draft_rule:
                checker.check(r.draft_rule)
                r.rule = r.draft_rule
                r.draft_rule = None
                mapping.update(curs, r)
        return response_ok()
Пример #7
0
    def apply_draft_rules(self, data, session, curs=None):
        t_f = TariffFilter(session, {'id': data['tariff_id']}, {}, None)
        t_f.filter_one_obj(curs)

        f = RuleFilter(session, {'tariff_id': data['tariff_id']}, {}, ['id'])
        rs = f.filter_objs(curs, for_update=True)
        checker = RuleChecker()
        for r in rs:
            if r.draft_rule:
                checker.check(r.draft_rule)
                r.rule = r.draft_rule
                r.draft_rule = None
                mapping.update(curs, r)
        return response_ok()
Пример #8
0
    def add_tariff_viewing_context(self, data, session, curs=None):
        # checking parent tariff exist
        t_id = data['tariff_id']
        t_f = TariffFilter(session, {'id': t_id}, {}, None)
        t_f.filter_one_obj(curs)

        raw_ctx = data['context']
        ctx = self._collapse_context(raw_ctx)
        t_v_data = {'environment_id': session.environment_id,
            'tariff_id': t_id, 'name': data['name'],
            'view_order': data['view_order'], 'context': ctx}

        t_v_ctx = TariffViewingContext(**t_v_data)
        mapping.insert(curs, t_v_ctx)
        return response_ok(id=t_v_ctx.id)
Пример #9
0
    def add_user_tariff(self, data, session, curs=None):
        # checking tariff exist
        t_id = data['tariff_id']
        u_id = data['user_id']
        if t_id:
            t_f = TariffFilter(session, {'id': t_id}, {}, None)
            t_f.filter_one_obj(curs)

        ut_data = {'environment_id': session.environment_id,
            'tariff_id': t_id, 'user_id': u_id}
        ut = UserTariff(**ut_data)
        try:
            mapping.insert(curs, ut)
        except ObjectCreationError:
            raise UserTariffAlreadyExsits(tariff_id=t_id, user_id=u_id)
        return response_ok(id=ut.id)
Пример #10
0
    def modify_tariff_viewing_context(self, data, session, curs=None):
        f = TariffViewingContextFilter(session, {'id': data.get('id')}, {}, None)
        loader = partial(f.filter_one_obj, curs, for_update=True)

        if 'new_tariff_id' in data:
            t_id = data.get('new_tariff_id')
            t_f = TariffFilter(session, {'id': t_id}, {}, None)
            t_f.filter_one_obj(curs)

        if 'new_context' in data:
            data['new_context'] = self._collapse_context(data['new_context'])
            data = mapping.objects.serialize_field(data, 'new_context',
                'new_serialized_context')

        self.update_obj(curs, data, loader)
        return response_ok()
Пример #11
0
    def modify_tariff_viewing_context(self, data, session, curs=None):
        f = TariffViewingContextFilter(session, {'id': data.get('id')}, {},
                                       None)
        loader = partial(f.filter_one_obj, curs, for_update=True)

        if 'new_tariff_id' in data:
            t_id = data.get('new_tariff_id')
            t_f = TariffFilter(session, {'id': t_id}, {}, None)
            t_f.filter_one_obj(curs)

        if 'new_context' in data:
            data['new_context'] = self._collapse_context(data['new_context'])
            data = mapping.objects.serialize_field(data, 'new_context',
                                                   'new_serialized_context')

        self.update_obj(curs, data, loader)
        return response_ok()
Пример #12
0
    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
Пример #13
0
    def add_user_tariff(self, data, session, curs=None):
        # checking tariff exist
        t_id = data['tariff_id']
        u_id = data['user_id']
        if t_id:
            t_f = TariffFilter(session, {'id': t_id}, {}, None)
            t_f.filter_one_obj(curs)

        ut_data = {
            'environment_id': session.environment_id,
            'tariff_id': t_id,
            'user_id': u_id
        }
        ut = UserTariff(**ut_data)
        try:
            mapping.insert(curs, ut)
        except ObjectCreationError:
            raise UserTariffAlreadyExsits(tariff_id=t_id, user_id=u_id)
        return response_ok(id=ut.id)
Пример #14
0
    def add_tariff_viewing_context(self, data, session, curs=None):
        # checking parent tariff exist
        t_id = data['tariff_id']
        t_f = TariffFilter(session, {'id': t_id}, {}, None)
        t_f.filter_one_obj(curs)

        raw_ctx = data['context']
        ctx = self._collapse_context(raw_ctx)
        t_v_data = {
            'environment_id': session.environment_id,
            'tariff_id': t_id,
            'name': data['name'],
            'view_order': data['view_order'],
            'context': ctx
        }

        t_v_ctx = TariffViewingContext(**t_v_data)
        mapping.insert(curs, t_v_ctx)
        return response_ok(id=t_v_ctx.id)
Пример #15
0
    def save_rule(self, data, session, curs=None):
        all_t_f = TariffFilter(session, {}, {}, None)
        all_ts = all_t_f.filter_objs(curs)
        all_ts_idx = build_index(all_ts)

        all_to_f = TarifficationObjectFilter(session, {}, {}, None)
        all_tos = all_to_f.filter_objs(curs)
        all_tos_idx = build_index(all_tos)

        all_r_f = RuleFilter(session, {}, {}, None)
        all_rs = all_r_f.filter_objs(curs)
        all_rs_idx = build_index(all_rs)

        r_data = {
            'environment_id': session.environment_id,
            'tariff_id': data['tariff_id'],
            'status': data['status'],
            'tariffication_object_id': data['tariffication_object_id'],
            'draft_rule': data['draft_rule']
        }
        r_data['view_order'] = data.get('view_order', 0)
        rule_id = data.get('id')
        if rule_id:
            r_data['id'] = rule_id
        r = Rule(**r_data)

        if rule_id and rule_id not in all_rs_idx:
            raise RuleNotFound(id=rule_id)
        if r.tariff_id not in all_ts_idx:
            raise TariffNotFound(rule_id=rule_id, tariff_id=r.tariff_id)
        if r.tariffication_object_id not in all_tos_idx:
            raise TarifficationObjectNotFound(
                rule_id=rule_id,
                tariffication_object_id=r.tariffication_object_id)
        checker = RuleChecker()
        checker.check(r.draft_rule)

        try:
            mapping.save(curs, r)
        except ObjectCreationError:
            raise RuleAlreadyExsits(r)
        return response_ok(id=r.id)
Пример #16
0
    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)
Пример #17
0
    def save_rule(self, data, session, curs=None):
        all_t_f = TariffFilter(session, {}, {}, None)
        all_ts = all_t_f.filter_objs(curs)
        all_ts_idx = build_index(all_ts)

        all_to_f = TarifficationObjectFilter(session, {}, {}, None)
        all_tos = all_to_f.filter_objs(curs)
        all_tos_idx = build_index(all_tos)

        all_r_f = RuleFilter(session, {}, {}, None)
        all_rs = all_r_f.filter_objs(curs)
        all_rs_idx = build_index(all_rs)

        r_data = {'environment_id': session.environment_id,
            'tariff_id': data['tariff_id'], 'status': data['status'],
            'tariffication_object_id': data['tariffication_object_id'],
            'draft_rule': data['draft_rule']}
        r_data['view_order'] = data.get('view_order', 0)
        rule_id = data.get('id')
        if rule_id:
            r_data['id'] = rule_id
        r = Rule(**r_data)

        if rule_id and rule_id not in all_rs_idx:
            raise RuleNotFound(id=rule_id)
        if r.tariff_id not in all_ts_idx:
            raise TariffNotFound(rule_id=rule_id, tariff_id=r.tariff_id)
        if r.tariffication_object_id not in all_tos_idx:
            raise TarifficationObjectNotFound(rule_id=rule_id,
                tariffication_object_id=r.tariffication_object_id)
        checker = RuleChecker()
        checker.check(r.draft_rule)

        try:
            mapping.save(curs, r)
        except ObjectCreationError:
            raise RuleAlreadyExsits(r)
        return response_ok(id=r.id)
Пример #18
0
    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()
Пример #19
0
    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()
Пример #20
0
    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)
Пример #21
0
    def get_rules(self, data, session, curs=None):
        r_f = RuleFilter(session, data['filter_params'],
            data['paging_params'], data.get('ordering_params'))
        rs, total = r_f.filter_counted(curs)
        if total:
            all_to_f = TarifficationObjectFilter(session, {}, {}, None)
            all_tos = all_to_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)
        else:
            all_tos_idx = {}
            all_ts_idx = {}

        def viewer(r):
            t = all_ts_idx[r.tariff_id]
            to = all_tos_idx[r.tariffication_object_id]
            return {'id': r.id, 'tariff_id': t.id, 'tariff_name': t.name,
                'tariffication_object_id': to.id, 'tariffication_object_name': to.name,
                'status': r.status, 'rule': r.rule, 'draft_rule': r.draft_rule,
                'view_order': r.view_order}
        return response_ok(rules=self.objects_info(rs, viewer), total=total)
Пример #22
0
    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)