unduplicated alt adding code

* should probably be part of the Alt or User class at some point but this is for a diff day
master
justcool393 2022-10-15 11:52:10 -07:00
parent 60284daa8e
commit 6138c94a10
1 changed files with 16 additions and 38 deletions

View File

@ -21,7 +21,7 @@ def login_get(v):
return render_template("login.html", failed=False, redirect=redir) return render_template("login.html", failed=False, redirect=redir)
def check_for_alts(current): def check_for_alts(current:User):
current_id = current.id current_id = current.id
if current_id in (1691,6790,7069,36152): if current_id in (1691,6790,7069,36152):
session["history"] = [] session["history"] = []
@ -29,8 +29,15 @@ def check_for_alts(current):
ids = [x[0] for x in g.db.query(User.id).all()] ids = [x[0] for x in g.db.query(User.id).all()]
past_accs = set(session.get("history", [])) past_accs = set(session.get("history", []))
for past_id in list(past_accs): 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: if past_id not in ids:
past_accs.remove(past_id) past_accs.remove(past_id)
continue continue
@ -39,46 +46,17 @@ def check_for_alts(current):
if past_id == current_id: continue if past_id == current_id: continue
li = [past_id, current_id] li = [past_id, current_id]
existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).one_or_none() add_alt(past_id, current_id)
other_alts = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).all()
if not existing: for a in other_alts:
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:
if a.user1 != past_id: if a.user1 != past_id:
li = [a.user1, past_id] add_alt(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()
if a.user1 != current_id: if a.user1 != current_id:
li = [a.user1, current_id] add_alt(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()
if a.user2 != past_id: if a.user2 != past_id:
li = [a.user2, past_id] add_alt(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()
if a.user2 != current_id: if a.user2 != current_id:
li = [a.user2, current_id] add_alt(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()
past_accs.add(current_id) past_accs.add(current_id)
session["history"] = list(past_accs) session["history"] = list(past_accs)