Example #1
0
def _create_currencies():
    from pooldlib.postgresql import Currency

    c = Currency(title='United States Dollar',
                 code='USD',
                 number=840,
                 unit='Dollar',
                 unit_plural='Dollars',
                 sign='$')
    c.title = 'United States Dollar'
    c.code = 'USD'
    c.number = 840
    c.unit = 'Dollar'
    c.unit_plural = 'Dollars'
    c.sign = '$'

    db.session.add(c)
    db.session.commit()
Example #2
0
    def balance_for_currency(self, currency, get_or_create=True, for_update=False):
        """Return a user's :class:`pooldlib.postgresql.models.Balance` for a given
        :class:`pooldlib.postgresql.models.Currency` or currency code (:class:`str`).

        :param currency: Currency of balance to return.
        :type currency: :class:`pooldlib.postgresql.models.Balance` or string.
        :param get_or_create: Should a balance be created for the user in the target currency if none is found?
        :type get_or_create: boolean, default `True`
        :param for_update: If `True` the `FOR UPDATE` directive will be used, locking the row for an `UPDATE` query.
        :type for_update: boolean, default `False`
        """
        from pooldlib.api import balance
        from pooldlib.postgresql import (db,
                                         Currency as CurrencyModel,
                                         Balance as BalanceModel)

        if isinstance(currency, basestring):
            currency = CurrencyModel.get(currency)
        if self.__class__.__name__ == 'User':
            balance = balance.get(for_update=for_update, user_id=self.id, currency_id=currency.id)
        else:
            balance = balance.get(for_update=for_update, campaign_id=self.id, currency_id=currency.id)

        # If we don't find a balance for the user, create if requested to
        if not balance and get_or_create:
            balance = BalanceModel()
            balance.enabled = True
            balance.amount = Decimal('0.0000')
            balance.currency = currency
            balance.type = self.__class__.__name__.lower()
            if self.__class__.__name__ == 'User':
                balance.user = self
            else:
                balance.campaign = self
            db.session.add(balance)
            self.balances.append(balance)
            db.session.flush()

        # Balance.get returns a list if for_update=False. A user should only
        # have a single balance for a given currency, so return the
        # CurrencyModel object instead of a list.
        if isinstance(balance, list):
            balance = balance[0]
        return balance
Example #3
0
    def create_balance(self, user=None, campaign=None, currency_code=None, amount=None):
        amount = amount if amount is not None else Decimal('50.0000')
        if not currency_code:
            currency_code = 'USD'

        currency = Currency.get(currency_code)
        b = Balance()
        b.enabled = True
        b.amount = amount
        b.currency = currency

        if user is not None:
            b.user_id = user.id
            b.type = 'user'
        elif campaign is not None:
            b.campaign_id = campaign.id
            b.type = 'campaign'

        self.commit_model(b)
        return b
Example #4
0
    def create_balance(self,
                       user=None,
                       campaign=None,
                       currency_code=None,
                       amount=None):
        amount = amount if amount is not None else Decimal('50.0000')
        if not currency_code:
            currency_code = 'USD'

        currency = Currency.get(currency_code)
        b = Balance()
        b.enabled = True
        b.amount = amount
        b.currency = currency

        if user is not None:
            b.user_id = user.id
            b.type = 'user'
        elif campaign is not None:
            b.campaign_id = campaign.id
            b.type = 'campaign'

        self.commit_model(b)
        return b