def set_password(self, password): self.password_hash = generate_password_hash(password) cur = get_db().cursor() cur.execute(""" UPDATE zerosum_user SET password_hash = %s, is_active = true WHERE user_id = %s """, [self.password_hash, self.user_id]) assert cur.rowcount == 1
def login(): if request.method == 'POST': login = request.form['login'] password = request.form['password'] # get user cur = get_db().cursor() cur.execute("SELECT * FROM zerosum_user WHERE email = %s", [login]) user = User(cur.fetchall()[0]) if user.check_password(password): login_user(user) return redirect(url_for('home')) return render_template('login.html')
def get_or_create_user(email): conn = get_db() cur = conn.cursor() cur.execute("SELECT * FROM zerosum_user WHERE email = %s FOR UPDATE", [email]) rows = cur.fetchall() if not rows: cur.execute(""" INSERT INTO zerosum_user(email, name) VALUES (%(email)s, %(email)s) RETURNING *""", dict(email=email)) rows = cur.fetchall() return rows[0]
def home(): cur = get_db().cursor() cur.execute("SELECT * FROM recent_owes(%s)", [current_user.get_id()]) owes = cur.fetchall() cur.execute("SELECT * FROM balances(%s)", [current_user.get_id()]) balances = cur.fetchall() cur.execute(""" SELECT user_id, array_to_json(array_agg(recent_owes)) FROM recent_owes(%s) GROUP BY 1 """, [current_user.get_id()]) details = dict(cur.fetchall()) requests = get_all(""" SELECT owe_request.*, name FROM owe_request JOIN zerosum_user ON (creditor_id = user_id) WHERE debitor_id = %s AND status = 'open' ORDER BY created_at """, [current_user.get_id()]) my_open_requests = get_all(""" SELECT owe_request.*, name FROM owe_request JOIN zerosum_user ON (debitor_id = user_id) WHERE creditor_id = %s AND status = 'open' ORDER BY created_at """, [current_user.get_id()]) total = ( sum(b.amount for b in balances if b.amount > 0), sum(b.amount for b in balances if b.amount < 0), ) return render_template('home.html', owes=owes, balances=balances, details=details, total=total, requests=requests, my_open_requests=my_open_requests)
def get(cls, user_id): cur = get_db().cursor() cur.execute("SELECT * FROM zerosum_user WHERE user_id = %s", [user_id]) return cls(cur.fetchall()[0])