stop usage of .update

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

View File

@ -120,7 +120,9 @@ def execute_snappy(post:Post, v:User):
if group.name == 'biofoids': mul = 10
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
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
if cost > v.coins:
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)
if log_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:
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)

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
if every_5m:
_award_timers_task()
if FEATURES['GAMBLING']:
check_if_end_lottery_task()
g.db.commit()
spin_roulette_wheel()
g.db.commit()
#offsitementions.offsite_mentions_task(cache)
if every_1h:
_award_timers_task()
g.db.commit()
if every_1d:
stats.generate_charts_task(SITE)
g.db.commit()
_sub_inactive_purge_task()
g.db.commit()
cache.set('stats', stats.stats())
g.db.commit()
_generate_emojis_zip()
g.db.commit()
_leaderboard_task()
g.db.commit()
g.db.commit()
g.db.close()
del g.db
stdout.flush()
@ -182,7 +174,13 @@ def _process_timer(attr, badge_ids, text, extra_attrs={}):
#set user attributes
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
if badge_ids:
@ -191,7 +189,6 @@ def _process_timer(attr, badge_ids, text, extra_attrs={}):
#notify users
for uid in uids:
send_repeatable_notification(uid, text)
g.db.commit()
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.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!", {
User.house: User.old_house,
User.house: ("old_house"),
User.old_house: '',
})
_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.rainbow, [171], "The rainbow 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,
})
_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!", {
User.username: User.prelock_username,
User.username: ("prelock_username"),
User.prelock_username: None,
})

View File

@ -124,10 +124,15 @@ def notifications_messages(v:User):
).all()
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.comment_id.in_(notifs_unread),
).update({Notification.read: True})
)
for n in notif_list:
n.read = True
g.db.add(n)
g.db.flush()
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:
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
g.db.add(target.author)
existing.vote_type = new
existing.coins = coin_value
g.db.add(existing)
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
g.db.add(target.author)
g.db.delete(existing)
@ -112,7 +112,7 @@ def vote_post_comment(target_id, new, v, cls, vote_cls):
existing.vote_type = new
g.db.add(existing)
elif new != 0:
target.author.coins += coin_value
target.author.pay_account('coins', coin_value)
target.author.truescore += coin_delta
g.db.add(target.author)