예제 #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 get_tariffication_objects(self, data, session, curs=None):
     to_f = TarifficationObjectFilter(session, data['filter_params'],
         data['paging_params'], data.get('ordering_params'))
     tos, total = to_f.filter_counted(curs)
     def viewer(to):
         return {
             'id': to.id,
             'name': to.name,
         }
     return response_ok(tariffication_objects=self.objects_info(tos, viewer), total=total)
예제 #3
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
예제 #4
0
    def get_tariffication_objects(self, data, session, curs=None):
        to_f = TarifficationObjectFilter(session, data['filter_params'],
                                         data['paging_params'],
                                         data.get('ordering_params'))
        tos, total = to_f.filter_counted(curs)

        def viewer(to):
            return {
                'id': to.id,
                'name': to.name,
            }

        return response_ok(tariffication_objects=self.objects_info(
            tos, viewer),
                           total=total)
예제 #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 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)
예제 #7
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)
예제 #8
0
 def modify_tariffication_object(self, data, session, curs=None):
     f = TarifficationObjectFilter(session, {'id': data.get('id')}, {},
                                   None)
     loader = partial(f.filter_one_obj, curs, for_update=True)
     try:
         self.update_obj(curs, data, loader)
     except DataIntegrityError:
         raise HelixtariffObjectAlreadyExists(
             'Tariffication object %s already exists' %
             data.get('new_name'))
     return response_ok()
예제 #9
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)
예제 #10
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)
예제 #11
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)
예제 #12
0
 def delete_tariffication_object(self, data, session, curs=None):
     f = TarifficationObjectFilter(session, {'id': data.get('id')}, {},
                                   None)
     mapping.delete(curs, f.filter_one_obj(curs))
     return response_ok()
예제 #13
0
 def delete_tariffication_object(self, data, session, curs=None):
     f = TarifficationObjectFilter(session, {'id': data.get('id')}, {}, None)
     mapping.delete(curs, f.filter_one_obj(curs))
     return response_ok()