forked from rDrama/rDrama
voting: allow voting to cause a user's DC balance to go negative
parent
1e9f9564dc
commit
d5812096af
|
@ -172,20 +172,22 @@ class User(Base):
|
||||||
g.db.flush()
|
g.db.flush()
|
||||||
|
|
||||||
|
|
||||||
def charge_account(self, currency, amount):
|
def charge_account(self, currency, amount, **kwargs):
|
||||||
in_db = g.db.query(User).filter(User.id == self.id).with_for_update().one()
|
in_db = g.db.query(User).filter(User.id == self.id).with_for_update().one()
|
||||||
succeeded = False
|
succeeded = False
|
||||||
|
|
||||||
|
should_check_balance = kwargs.get('should_check_balance', True)
|
||||||
|
|
||||||
if currency == 'coins':
|
if currency == 'coins':
|
||||||
account_balance = in_db.coins
|
account_balance = in_db.coins
|
||||||
|
|
||||||
if account_balance >= amount:
|
if not should_check_balance or account_balance >= amount:
|
||||||
g.db.query(User).filter(User.id == self.id).update({ User.coins: User.coins - amount })
|
g.db.query(User).filter(User.id == self.id).update({ User.coins: User.coins - amount })
|
||||||
succeeded = True
|
succeeded = True
|
||||||
elif currency == 'procoins':
|
elif currency == 'procoins':
|
||||||
account_balance = in_db.procoins
|
account_balance = in_db.procoins
|
||||||
|
|
||||||
if account_balance >= amount:
|
if not should_check_balance or account_balance >= amount:
|
||||||
g.db.query(User).filter(User.id == self.id).update({ User.procoins: User.procoins - amount })
|
g.db.query(User).filter(User.id == self.id).update({ User.procoins: User.procoins - amount })
|
||||||
succeeded = True
|
succeeded = True
|
||||||
|
|
||||||
|
|
|
@ -86,7 +86,7 @@ def vote_post_comment(target_id, new, v, cls, vote_cls):
|
||||||
existing.coins = coin_value
|
existing.coins = coin_value
|
||||||
g.db.add(existing)
|
g.db.add(existing)
|
||||||
elif existing.vote_type != 0 and new == 0:
|
elif existing.vote_type != 0 and new == 0:
|
||||||
target.author.charge_account('coins', existing.coins)
|
target.author.charge_account('coins', existing.coins, should_check_balance=False)
|
||||||
target.author.truecoins -= coin_delta
|
target.author.truecoins -= coin_delta
|
||||||
g.db.add(target.author)
|
g.db.add(target.author)
|
||||||
g.db.delete(existing)
|
g.db.delete(existing)
|
||||||
|
|
Loading…
Reference in New Issue