stop usage of .update

master
Aevann 2023-07-03 00:42:34 +03:00
parent a11c12ef77
commit 25b9a0f535
6 changed files with 40 additions and 32 deletions

View File

@ -206,9 +206,9 @@ class User(Base):
return return
if currency == 'coins': if currency == 'coins':
g.db.query(User).filter(User.id == self.id).update({ User.coins: User.coins + amount }) self.coins += amount
else: else:
g.db.query(User).filter(User.id == self.id).update({ User.marseybux: User.marseybux + amount }) self.marseybux += amount
g.db.flush() g.db.flush()
@ -224,14 +224,14 @@ class User(Base):
account_balance = in_db.coins account_balance = in_db.coins
if not should_check_balance or 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 }) self.coins -= amount
succeeded = True succeeded = True
charged_coins = amount charged_coins = amount
elif currency == 'marseybux': elif currency == 'marseybux':
account_balance = in_db.marseybux account_balance = in_db.marseybux
if not should_check_balance or account_balance >= amount: if not should_check_balance or account_balance >= amount:
g.db.query(User).filter(User.id == self.id).update({ User.marseybux: User.marseybux - amount }) self.marseybux -= amount
succeeded = True succeeded = True
elif currency == 'combined': elif currency == 'combined':
if in_db.marseybux >= amount: if in_db.marseybux >= amount:
@ -243,10 +243,8 @@ class User(Base):
if subtracted_coins > in_db.coins: if subtracted_coins > in_db.coins:
return (False, 0) return (False, 0)
g.db.query(User).filter(User.id == self.id).update({ self.coins -= subtracted_coins
User.marseybux: User.marseybux - subtracted_mbux, self.marseybux -= subtracted_mbux
User.coins: User.coins - subtracted_coins,
})
succeeded = True succeeded = True
charged_coins = subtracted_coins charged_coins = subtracted_coins

View File

@ -120,7 +120,9 @@ def execute_snappy(post:Post, v:User):
if group.name == 'biofoids': mul = 10 if group.name == 'biofoids': mul = 10
else: mul = 5 else: mul = 5
g.db.query(User).filter(User.id.in_(group_members)).update({ User.coins: User.coins + mul }) for user in g.db.query(User).filter(User.id.in_(group_members)).all():
user.pay_account('coins', mul)
g.db.add(user)
cost = len(group_members) * mul cost = len(group_members) * mul
snappy.charge_account('coins', cost) snappy.charge_account('coins', cost)

View File

@ -157,7 +157,11 @@ def NOTIFY_USERS(text, v, oldtext=None, ghost=False, log_cost=None):
cost = g.db.query(User).count() * 5 cost = g.db.query(User).count() * 5
if cost > v.coins: if cost > v.coins:
abort(403, f"You need {cost} coins to mention these ping groups!") abort(403, f"You need {cost} coins to mention these ping groups!")
g.db.query(User).update({ User.coins: User.coins + 5 })
for user in g.db.query(User).all():
user.pay_account('coins', 5)
g.db.add(user)
v.charge_account('coins', cost) v.charge_account('coins', cost)
if log_cost: if log_cost:
log_cost.ping_cost = cost log_cost.ping_cost = cost
@ -181,7 +185,9 @@ def NOTIFY_USERS(text, v, oldtext=None, ghost=False, log_cost=None):
if log_cost: if log_cost:
log_cost.ping_cost = cost log_cost.ping_cost = cost
g.db.query(User).filter(User.id.in_(members)).update({ User.coins: User.coins + mul }) for user in g.db.query(User).filter(User.id.in_(members)).all():
user.pay_account('coins', mul)
g.db.add(user)
v.charge_account('coins', cost) v.charge_account('coins', cost)

View File

@ -34,34 +34,26 @@ def cron(every_5m, every_1h, every_1d, every_1mo):
#I put commit under each task to release database locks and prevent main flask app crashing #I put commit under each task to release database locks and prevent main flask app crashing
if every_5m: if every_5m:
_award_timers_task()
if FEATURES['GAMBLING']: if FEATURES['GAMBLING']:
check_if_end_lottery_task() check_if_end_lottery_task()
g.db.commit()
spin_roulette_wheel() spin_roulette_wheel()
g.db.commit()
#offsitementions.offsite_mentions_task(cache) #offsitementions.offsite_mentions_task(cache)
if every_1h:
_award_timers_task()
g.db.commit()
if every_1d: if every_1d:
stats.generate_charts_task(SITE) stats.generate_charts_task(SITE)
g.db.commit()
_sub_inactive_purge_task() _sub_inactive_purge_task()
g.db.commit()
cache.set('stats', stats.stats()) cache.set('stats', stats.stats())
g.db.commit()
_generate_emojis_zip() _generate_emojis_zip()
g.db.commit()
_leaderboard_task() _leaderboard_task()
g.db.commit()
g.db.commit()
g.db.close() g.db.close()
del g.db del g.db
stdout.flush() stdout.flush()
@ -182,7 +174,13 @@ def _process_timer(attr, badge_ids, text, extra_attrs={}):
#set user attributes #set user attributes
attr_dict = {attr: 0} | extra_attrs attr_dict = {attr: 0} | extra_attrs
users.update(attr_dict)
for user in users:
for k, val in attr_dict.items():
k = str(k).split('.')[1]
if isinstance(val, tuple):
val = getattr(user, val[0])
setattr(user, k, val)
#remove corresponding badges #remove corresponding badges
if badge_ids: if badge_ids:
@ -191,7 +189,6 @@ def _process_timer(attr, badge_ids, text, extra_attrs={}):
#notify users #notify users
for uid in uids: for uid in uids:
send_repeatable_notification(uid, text) send_repeatable_notification(uid, text)
g.db.commit()
def _award_timers_task(): def _award_timers_task():
@ -205,19 +202,19 @@ def _award_timers_task():
_process_timer(User.owoify, [167], "The OwOify award you received has expired!") _process_timer(User.owoify, [167], "The OwOify award you received has expired!")
_process_timer(User.sharpen, [289], "The Sharpen award you received has expired!") _process_timer(User.sharpen, [289], "The Sharpen award you received has expired!")
_process_timer(User.bite, [168], "The bite award you received has expired! You're now back in your original house!", { _process_timer(User.bite, [168], "The bite award you received has expired! You're now back in your original house!", {
User.house: User.old_house, User.house: ("old_house"),
User.old_house: '', User.old_house: '',
}) })
_process_timer(User.earlylife, [169], "The earlylife award you received has expired!") _process_timer(User.earlylife, [169], "The earlylife award you received has expired!")
_process_timer(User.marsify, [170], "The marsify award you received has expired!") _process_timer(User.marsify, [170], "The marsify award you received has expired!")
_process_timer(User.rainbow, [171], "The rainbow award you received has expired!") _process_timer(User.rainbow, [171], "The rainbow award you received has expired!")
_process_timer(User.queen, [285], "The queen award you received has expired!", { _process_timer(User.queen, [285], "The queen award you received has expired!", {
User.username: User.prelock_username, User.username: ("prelock_username"),
User.prelock_username: None, User.prelock_username: None,
}) })
_process_timer(User.spider, [179], "The spider award you received has expired!") _process_timer(User.spider, [179], "The spider award you received has expired!")
_process_timer(User.namechanged, [281], "The namelock award you received has expired. You're now back to your old username!", { _process_timer(User.namechanged, [281], "The namelock award you received has expired. You're now back to your old username!", {
User.username: User.prelock_username, User.username: ("prelock_username"),
User.prelock_username: None, User.prelock_username: None,
}) })

View File

@ -124,10 +124,15 @@ def notifications_messages(v:User):
).all() ).all()
notifs_unread = [n.comment_id for n in notifs_unread_row] notifs_unread = [n.comment_id for n in notifs_unread_row]
g.db.query(Notification).filter( notif_list = g.db.query(Notification).filter(
Notification.user_id == v.id, Notification.user_id == v.id,
Notification.comment_id.in_(notifs_unread), Notification.comment_id.in_(notifs_unread),
).update({Notification.read: True}) )
for n in notif_list:
n.read = True
g.db.add(n)
g.db.flush() g.db.flush()
list_to_perserve_unread_attribute = [] list_to_perserve_unread_attribute = []

View File

@ -97,14 +97,14 @@ def vote_post_comment(target_id, new, v, cls, vote_cls):
if existing and existing.vote_type == new: return "", 204 if existing and existing.vote_type == new: return "", 204
if existing: if existing:
if existing.vote_type == 0 and new != 0: if existing.vote_type == 0 and new != 0:
target.author.coins += coin_value target.author.pay_account('coins', coin_value)
target.author.truescore += coin_delta target.author.truescore += coin_delta
g.db.add(target.author) g.db.add(target.author)
existing.vote_type = new existing.vote_type = new
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.coins -= existing.coins target.author.charge_account('coins', existing.coins, should_check_balance=False)
target.author.truescore -= coin_delta target.author.truescore -= coin_delta
g.db.add(target.author) g.db.add(target.author)
g.db.delete(existing) g.db.delete(existing)
@ -112,7 +112,7 @@ def vote_post_comment(target_id, new, v, cls, vote_cls):
existing.vote_type = new existing.vote_type = new
g.db.add(existing) g.db.add(existing)
elif new != 0: elif new != 0:
target.author.coins += coin_value target.author.pay_account('coins', coin_value)
target.author.truescore += coin_delta target.author.truescore += coin_delta
g.db.add(target.author) g.db.add(target.author)