forked from rDrama/rDrama
1
0
Fork 0

voting: allow voting to cause a user's DC balance to go negative

master
justcool393 2022-11-01 12:50:01 -05:00
parent 1e9f9564dc
commit d5812096af
2 changed files with 6 additions and 4 deletions

View File

@ -172,20 +172,22 @@ class User(Base):
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()
succeeded = False
should_check_balance = kwargs.get('should_check_balance', True)
if currency == '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 })
succeeded = True
elif currency == '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 })
succeeded = True

View File

@ -86,7 +86,7 @@ def vote_post_comment(target_id, new, v, cls, vote_cls):
existing.coins = coin_value
g.db.add(existing)
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
g.db.add(target.author)
g.db.delete(existing)