예제 #1
0
    def modify_balances(self, data, session, curs=None):
        balances_ids = data['ids']
        currency_f = CurrencyFilter({}, {}, None)
        currencies = currency_f.filter_objs(curs)
        currencies_id_idx = build_index(currencies, 'id')

        balance_f = BalanceFilter(session, {'ids': balances_ids}, {}, None)
        balances = balance_f.filter_objs(curs)

        # Setting users ids for logging
        users_ids = [balance.user_id for balance in balances]
        data['users_ids'] = users_ids

        # Handling different currencies for different balances
        new_overdraft_limit = data.pop('new_overdraft_limit', None)
        def loader():
            if new_overdraft_limit is not None:
                for b in balances:
                    d = {'new_overdraft_limit': new_overdraft_limit}
                    amount_fields = ['new_overdraft_limit']
                    currency = currencies_id_idx[b.currency_id]
                    d = decimal_texts_to_cents(d, currency, amount_fields)
                    b.overdraft_limit = d['new_overdraft_limit']
            return balances

        self.update_objs(curs, data, loader)
        return response_ok()
예제 #2
0
    def modify_balances(self, data, session, curs=None):
        balances_ids = data['ids']
        currency_f = CurrencyFilter({}, {}, None)
        currencies = currency_f.filter_objs(curs)
        currencies_id_idx = build_index(currencies, 'id')

        balance_f = BalanceFilter(session, {'ids': balances_ids}, {}, None)
        balances = balance_f.filter_objs(curs)

        # Setting users ids for logging
        users_ids = [balance.user_id for balance in balances]
        data['users_ids'] = users_ids

        # Handling different currencies for different balances
        new_overdraft_limit = data.pop('new_overdraft_limit', None)

        def loader():
            if new_overdraft_limit is not None:
                for b in balances:
                    d = {'new_overdraft_limit': new_overdraft_limit}
                    amount_fields = ['new_overdraft_limit']
                    currency = currencies_id_idx[b.currency_id]
                    d = decimal_texts_to_cents(d, currency, amount_fields)
                    b.overdraft_limit = d['new_overdraft_limit']
            return balances

        self.update_objs(curs, data, loader)
        return response_ok()
예제 #3
0
 def get_used_currencies(self, data, session, curs=None):
     f = CurrencyFilter({}, {}, data.get('ordering_params'))
     currs = f.filter_objs(curs)
     f = UsedCurrencyFilter(session, {}, {}, None)
     try:
         u_currs = f.filter_one_obj(curs)
         u_currs_ids = u_currs.currencies_ids
     except ObjectNotFound:
         u_currs_ids = []
     filtered_currs = [curr for curr in currs if curr.id in u_currs_ids]
     return response_ok(currencies=self._currencies_info(filtered_currs))
예제 #4
0
 def get_used_currencies(self, data, session, curs=None):
     f = CurrencyFilter({}, {}, data.get('ordering_params'))
     currs = f.filter_objs(curs)
     f = UsedCurrencyFilter(session, {}, {}, None)
     try:
         u_currs = f.filter_one_obj(curs)
         u_currs_ids = u_currs.currencies_ids
     except ObjectNotFound:
         u_currs_ids = []
     filtered_currs = [curr for curr in currs if curr.id in u_currs_ids]
     return response_ok(currencies=self._currencies_info(filtered_currs))
예제 #5
0
    def modify_used_currencies(self, data, session, curs=None):
        f = CurrencyFilter({}, {}, None)
        currs = f.filter_objs(curs)
        currs_code_idx = build_index(currs, idx_field='code')
        new_currs_codes = data.pop('new_currencies_codes', [])

        for curr_code in new_currs_codes:
            if curr_code not in currs_code_idx:
                raise CurrencyNotFound(code=curr_code)

        new_currs_ids = [curr.id for curr in currs if curr.code in new_currs_codes]
        data['new_currencies_ids'] = new_currs_ids
        f = UsedCurrencyFilter(session, {}, {}, None)
        try:
            loader = partial(f.filter_one_obj, curs, for_update=True)
            self.update_obj(curs, data, loader)
        except ObjectNotFound:
            u_currs = UsedCurrency(environment_id=session.environment_id,
                currencies_ids=new_currs_ids)
            mapping.save(curs, u_currs)
        return response_ok()
예제 #6
0
    def modify_used_currencies(self, data, session, curs=None):
        f = CurrencyFilter({}, {}, None)
        currs = f.filter_objs(curs)
        currs_code_idx = build_index(currs, idx_field='code')
        new_currs_codes = data.pop('new_currencies_codes', [])

        for curr_code in new_currs_codes:
            if curr_code not in currs_code_idx:
                raise CurrencyNotFound(code=curr_code)

        new_currs_ids = [
            curr.id for curr in currs if curr.code in new_currs_codes
        ]
        data['new_currencies_ids'] = new_currs_ids
        f = UsedCurrencyFilter(session, {}, {}, None)
        try:
            loader = partial(f.filter_one_obj, curs, for_update=True)
            self.update_obj(curs, data, loader)
        except ObjectNotFound:
            u_currs = UsedCurrency(environment_id=session.environment_id,
                                   currencies_ids=new_currs_ids)
            mapping.save(curs, u_currs)
        return response_ok()
예제 #7
0
 def get_currencies(self, data, session, curs=None):
     f = CurrencyFilter({}, {}, data.get('ordering_params'))
     currencies = f.filter_objs(curs)
     return response_ok(currencies=self._currencies_info(currencies))
예제 #8
0
 def _get_currs_idx(self, curs, idx_field):
     curr_f = CurrencyFilter({}, {}, None)
     currs = curr_f.filter_objs(curs)
     return build_index(currs, idx_field=idx_field)
예제 #9
0
 def _get_currs_idx(self, curs, idx_field):
     curr_f = CurrencyFilter({}, {}, None)
     currs = curr_f.filter_objs(curs)
     return build_index(currs, idx_field=idx_field)
예제 #10
0
 def get_currencies(self, data, session, curs=None):
     f = CurrencyFilter({}, {}, data.get('ordering_params'))
     currencies = f.filter_objs(curs)
     return response_ok(currencies=self._currencies_info(currencies))