Esempio n. 1
0
def create_for_user(user, currency):
    b = BalanceModel()
    b.enabled = True
    b.amount = Decimal('0.0000')
    b.currency = currency
    b.user = user
    b.type = 'user'

    with transaction_session() as session:
        session.add(b)
        session.commit()
    return b
Esempio n. 2
0
def create_for_campaign(campaign, currency):
    b = BalanceModel()
    b.enabled = True
    b.amount = Decimal('0.0000')
    b.currency = currency
    b.campaign = campaign
    b.type = 'campaign'

    with transaction_session() as session:
        session.add(b)
        session.commit()
    return b
Esempio n. 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
Esempio n. 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
Esempio n. 5
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
Esempio n. 6
0
    def factory(name, description, percentage, flat):
        u = User()
        u.username = name
        u.password = name
        db.session.add(u)
        db.session.flush()

        b = Balance()
        b.type = 'user'
        b.user_id = u.id
        b.amount = 0
        b.currency_id = 1
        db.session.add(b)

        f = Fee()
        f.name = name
        f.description = description
        f.fractional_pct = percentage
        f.flat = flat
        f.user_id = u.id
        db.session.add(f)