Forums

Flask Help

So I am running into issues with building a flask app. This is the first web app that Ive done so Im not very experienced and it may just be simple fix. I have built an api into our database website using python requests module. What I am trying to do with this webapp is have a user login to the web app with the same credentials they would use to login to our database website. On my api side its just creating a requests session using the credentials to log in with requests. I can perform the initialization of the class and login to the website successfully. But any requests made with my request session object after the inital login will not work. Nothing is showing up in the error log. I just keep getting 502-backend error. Heres my code. Any help would be so awesome! Thanks!

from flask import Flask, render_template, request, redirect, url_for, abort, session, g, flash
from functools import wraps
import sys, csv, traceback
sys.path.append('/home/tfndevelopment/API')
from Database_Site import *

app = Flask(__name__)
app.secret_key = ''

# login required decorator
def login_required(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        if 'logged_in' in session:
            return f(*args,**kwargs)
        else:
            #flash('You need to login first.')
            return redirect(url_for('login'))
    return wrap



@app.route('/login', methods=['GET','POST'])
def login():
    error = None
    if request.method == 'POST':
        trax = Database_Site()
        session['user_id'] = request.form['q99']
        session['password'] = request.form['q100']
        error = trax.login((request.form['q99'],request.form['q100']))
        trax.log_out()
        if error == None:
            session['logged_in'] = True
            return redirect(url_for('home'))
    return render_template('login.html', error=error)

@app.route('/logout')
@login_required
def logout():
    session.clear()
    return redirect(url_for('login'))

@app.route('/', methods=['GET','POST'])
@login_required
def home():
    if request.method == 'POST':
        search_type = request.form['q263_howDo']
        if search_type == 'Phone #':
            phone = request.form['q265_phoneNumber[full]']
            phone = ''.join(c for c in phone if c.isdigit())
            search_dict = {
                'lead_company_id': '1253',
                'search_by': [('Phone Number',None)],
                'search_data': {'Phone Number':phone}
            }
            trax = Database_Site()
            trax.login((session['user_id'],session['password']))
            cust_id = trax.search_customer(search_dict)
            return cust_id
        else:
            return 'error'
    return render_template('inbound.html')

if __name__ == '__main__':
    app.run(threaded=True)

what happens if you log the session['user_id'] and session['password'] in the def home(): view?

ie. check to see if your cookie has what you think it does?

I dont have the username, password form on the home view though. The login works just fine. Its when I try and call any other function in my API thats when the error occurs.

I think Conrad is suggesting that you log the session data in the home page view in order to check that the login really worked, and that the cookies are set correctly?