From 6138c94a1078493e40551d57021a5c6765c8ca3d Mon Sep 17 00:00:00 2001 From: justcool393 Date: Sat, 15 Oct 2022 11:52:10 -0700 Subject: [PATCH] unduplicated alt adding code * should probably be part of the Alt or User class at some point but this is for a diff day --- files/routes/login.py | 54 +++++++++++++------------------------------ 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/files/routes/login.py b/files/routes/login.py index 04ff71d2d..73ddc2766 100644 --- a/files/routes/login.py +++ b/files/routes/login.py @@ -21,7 +21,7 @@ def login_get(v): return render_template("login.html", failed=False, redirect=redir) -def check_for_alts(current): +def check_for_alts(current:User): current_id = current.id if current_id in (1691,6790,7069,36152): session["history"] = [] @@ -29,8 +29,15 @@ def check_for_alts(current): ids = [x[0] for x in g.db.query(User.id).all()] past_accs = set(session.get("history", [])) + def add_alt(user1:int, user2:int): + li = [user1, user2] + existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).one_or_none() + if not existing: + new_alt = Alt(user1=user1, user2=user2) + g.db.add(new_alt) + g.db.flush() + for past_id in list(past_accs): - if past_id not in ids: past_accs.remove(past_id) continue @@ -39,46 +46,17 @@ def check_for_alts(current): if past_id == current_id: continue li = [past_id, current_id] - existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).one_or_none() - - if not existing: - new_alt = Alt(user1=past_id, user2=current_id) - g.db.add(new_alt) - g.db.flush() - - otheralts = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).all() - for a in otheralts: + add_alt(past_id, current_id) + other_alts = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).all() + for a in other_alts: if a.user1 != past_id: - li = [a.user1, past_id] - existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).one_or_none() - if not existing: - new_alt = Alt(user1=a.user1, user2=past_id) - g.db.add(new_alt) - g.db.flush() - + add_alt(a.user1, past_id) if a.user1 != current_id: - li = [a.user1, current_id] - existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).one_or_none() - if not existing: - new_alt = Alt(user1=a.user1, user2=current_id) - g.db.add(new_alt) - g.db.flush() - + add_alt(a.user1, current_id) if a.user2 != past_id: - li = [a.user2, past_id] - existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).one_or_none() - if not existing: - new_alt = Alt(user1=a.user2, user2=past_id) - g.db.add(new_alt) - g.db.flush() - + add_alt(a.user2, past_id) if a.user2 != current_id: - li = [a.user2, current_id] - existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).one_or_none() - if not existing: - new_alt = Alt(user1=a.user2, user2=current_id) - g.db.add(new_alt) - g.db.flush() + add_alt(a.user2, current_id) past_accs.add(current_id) session["history"] = list(past_accs)