diff --git a/files/__main__.py b/files/__main__.py index 79d2e7b21..9b3832f92 100644 --- a/files/__main__.py +++ b/files/__main__.py @@ -72,7 +72,7 @@ Base = declarative_base() engine = create_engine(app.config['SQLALCHEMY_DATABASE_URL']) -db_session = scoped_session(sessionmaker(bind=engine)) +db_session = scoped_session(sessionmaker(bind=engine, autoflush=False)) cache = Cache(app) Compress(app) diff --git a/files/helpers/alerts.py b/files/helpers/alerts.py index 1199a7436..dfdf5d925 100644 --- a/files/helpers/alerts.py +++ b/files/helpers/alerts.py @@ -48,7 +48,17 @@ def notif_comment(text): text_html = sanitize(text) - existing = g.db.query(Comment.id).filter_by(author_id=AUTOJANNY_ID, parent_submission=None, body_html=text_html, is_bot=True).one_or_none() + try: existing = g.db.query(Comment.id).filter_by(author_id=AUTOJANNY_ID, parent_submission=None, body_html=text_html, is_bot=True).one_or_none() + except: + existing = g.db.query(Comment).filter_by(author_id=AUTOJANNY_ID, parent_submission=None, body_html=text_html, is_bot=True).all() + + notifs = g.db.query(Notification).filter(Notification.comment_id.in_([x.id for x in existing])).all() + for c in notifs: g.db.delete(c) + g.db.flush() + + for c in existing: g.db.delete(c) + g.db.flush() + existing = g.db.query(Comment.id).filter_by(author_id=AUTOJANNY_ID, parent_submission=None, body_html=text_html, is_bot=True).one_or_none() if existing: return existing[0] else: return create_comment(text_html) diff --git a/files/helpers/cron.py b/files/helpers/cron.py index f52aadbce..29d81b325 100644 --- a/files/helpers/cron.py +++ b/files/helpers/cron.py @@ -88,6 +88,7 @@ def sub_inactive_purge_task(): for x in to_delete: g.db.delete(x) + g.db.flush() for x in dead_holes: g.db.delete(x) diff --git a/files/routes/admin.py b/files/routes/admin.py index 3531e152c..a5e866f0c 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -77,14 +77,17 @@ def merge(v, id1, id2): if not user1.has_badge(badge.badge_id): badge.user_id = user1.id g.db.add(badge) + g.db.flush() for mod in mods: if not user1.mods(mod.sub): mod.user_id = user1.id g.db.add(mod) + g.db.flush() for exile in exiles: if not user1.exiled_from(exile.sub): exile.user_id = user1.id g.db.add(exile) + g.db.flush() for kind in ('comment_count', 'post_count', 'winnings', 'received_award_count', 'coins_spent', 'lootboxes_bought', 'coins', 'truecoins', 'procoins'): amount = getattr(user1, kind) + getattr(user2, kind) @@ -129,6 +132,7 @@ def merge_all(v, id): if not user.has_badge(badge.badge_id): badge.user_id = user.id g.db.add(badge) + g.db.flush() for alt in user.alts_unique: for kind in ('comment_count', 'post_count', 'winnings', 'received_award_count', 'coins_spent', 'lootboxes_bought', 'coins', 'truecoins', 'procoins'): diff --git a/files/routes/awards.py b/files/routes/awards.py index 5e4e3d209..e97c1a443 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -82,6 +82,7 @@ def buy(v, award): lootbox_items.append(AWARDS[award]['title']) award = AwardRelationship(user_id=v.id, kind=award) g.db.add(award) + g.db.flush() v.lootboxes_bought += 1 lootbox_msg = "You open your lootbox and receive: " + ', '.join(lootbox_items) diff --git a/files/routes/comments.py b/files/routes/comments.py index c79bb4e69..5e6b083e8 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -258,9 +258,10 @@ def comment(v): all_by_author = g.db.query(Marsey).filter_by(author_id=user.id).count() - if all_by_author >= 100: + # off-by-one: newly added marsey isn't counted + if all_by_author >= 99: badge_grant(badge_id=143, user=user) - elif all_by_author >= 10: + elif all_by_author >= 9: badge_grant(badge_id=16, user=user) else: badge_grant(badge_id=17, user=user) diff --git a/files/routes/users.py b/files/routes/users.py index db87f6a10..a6a791172 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -1314,6 +1314,7 @@ def fp(v, fp): if existing: continue new_alt = Alt(user1=v.id, user2=u.id) g.db.add(new_alt) + g.db.flush() print(v.username + ' + ' + u.username, flush=True) g.db.add(v) return '', 204 diff --git a/files/routes/votes.py b/files/routes/votes.py index 65b2fd7cd..77bc3bf7e 100644 --- a/files/routes/votes.py +++ b/files/routes/votes.py @@ -79,6 +79,7 @@ def vote_post(post_id, new, v): coin_mult = 1 + g.db.flush() existing = g.db.query(Vote).filter_by(user_id=v.id, submission_id=post.id).one_or_none() if DOUBLE_XP_ENABLED > 0: @@ -120,6 +121,7 @@ def vote_post(post_id, new, v): ) g.db.add(vote) + g.db.flush() post.upvotes = g.db.query(Vote).filter_by(submission_id=post.id, vote_type=1).count() post.downvotes = g.db.query(Vote).filter_by(submission_id=post.id, vote_type=-1).count() post.realupvotes = g.db.query(Vote).filter_by(submission_id=post.id, real=True).count() @@ -157,6 +159,7 @@ def vote_comment(comment_id, new, v): coin_mult = 1 + g.db.flush() existing = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).one_or_none() if DOUBLE_XP_ENABLED > 0: @@ -199,6 +202,7 @@ def vote_comment(comment_id, new, v): g.db.add(vote) + g.db.flush() comment.upvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, vote_type=1).count() comment.downvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, vote_type=-1).count() comment.realupvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, real=True).count()