contentmatcher/matches.py

59 lines
1.8 KiB
Python
Raw Normal View History

2022-06-14 02:17:58 +02:00
from flask import (
Blueprint, redirect, render_template, request, session, url_for
)
from requests import get
from config import CONFIG
2022-06-14 14:47:12 +02:00
from create_db import Card, User, get_session
2022-06-14 02:17:58 +02:00
bp = Blueprint('matches', __name__, url_prefix='/matches')
def get_matches(user_id):
2022-06-23 21:15:38 +02:00
#@TODO this is buggy
2022-06-14 14:47:12 +02:00
dbsession = get_session()
2022-06-14 02:17:58 +02:00
list_of_matches = []
#in all shared cards for the ones you voted yes
2022-06-14 02:17:58 +02:00
all_cards = dbsession.query(Card)
2022-06-23 22:02:49 +02:00
all_shared_of_user_of_ir_1 = all_cards.filter(Card.share_id != "0", Card.owner_id == user_id, Card.interest_rate == "1").all()
2022-06-14 02:17:58 +02:00
if all_shared_of_user_of_ir_1 == []:
return list_of_matches
# see who else voted yes
for c in all_shared_of_user_of_ir_1:
2022-06-23 22:02:49 +02:00
others_yes = all_cards.filter(Card.share_id == c.share_id, Card.interest_rate == "1", Card.owner_id != user_id).all()
2022-06-14 02:17:58 +02:00
if others_yes == []:
pass
else:
list_of_matches.append(others_yes)
return list_of_matches
@bp.route("/", methods=("GET", "POST"))
def index():
2022-06-15 11:09:07 +02:00
#@TODO its buggs, not symetrical for users, shouldnt show if only you said yes
2022-06-14 02:17:58 +02:00
if not 'user_id' in session:
redirect(url_for('index'))
2022-06-14 17:01:45 +02:00
dbsession = get_session()
2022-06-14 02:17:58 +02:00
user_id = session["user_id"]
username = session["username"]
list_of_matches = get_matches(user_id)
2022-06-14 13:24:40 +02:00
#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)
users = dbsession.query(User)
names_by_ids = {}
emails_by_ids = {}
2022-06-14 13:24:40 +02:00
for id in user_ids:
names_by_ids[id] = users.get(id).username
emails_by_ids[id] = users.get(id).email
2022-06-14 13:24:40 +02:00
2022-06-23 21:15:38 +02:00
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)