contentmatcher/matches.py

71 lines
2.2 KiB
Python

from flask import (
Blueprint, redirect, render_template, request, session, url_for
)
from requests import get
from config import CONFIG
from create_db import Card, User, get_session, get_engine
bp = Blueprint('matches', __name__, url_prefix='/matches')
def get_matches(user_id):
#@TODO this is buggy
list_of_matches = []
#in all shared cards for the ones you voted yes
Session = get_session()
with Session() as dbsession:
all_cards = dbsession.query(Card)
all_shared_of_user_of_ir_1 = all_cards.filter(Card.share_id != "0", Card.owner_id == user_id, Card.interest_rate == "1").all()
dbsession.close()
get_engine().dispose()
if all_shared_of_user_of_ir_1 == []:
return list_of_matches
# see who else voted yes
Session = get_session()
with Session() as dbsession:
for c in all_shared_of_user_of_ir_1:
others_yes = all_cards.filter(Card.share_id == c.share_id, Card.interest_rate == "1", Card.owner_id != user_id).all()
if others_yes == []:
pass
else:
list_of_matches.append(others_yes)
dbsession.close()
get_engine().dispose()
return list_of_matches
@bp.route("/", methods=("GET", "POST"))
def index():
#@TODO its buggs, not symetrical for users, shouldnt show if only you said yes
if not 'user_id' in session:
redirect(url_for('index'))
user_id = session["user_id"]
username = session["username"]
list_of_matches = get_matches(user_id)
#relevant userids, but might be uselessly duplicated
user_ids = []
for match in list_of_matches:
for card in match:
user_ids.append(card.owner_id)
Session = get_session()
with Session() as dbsession:
users = dbsession.query(User).all()
dbsession.close()
get_engine().dispose()
names_by_ids = {}
emails_by_ids = {}
for id in user_ids:
names_by_ids[id] = users.get(id).username
emails_by_ids[id] = users.get(id).email
return render_template("matches.html", username=username, list_of_matches=list_of_matches, names_by_ids=names_by_ids, emails_by_ids=emails_by_ids, users=users)