def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: g.user = get_db().execute('SELECT * FROM user WHERE id = ?', (user_id, )).fetchone()
def index(): db = get_db() orders = db.execute( "SELECT 'order'.id, lead.first_name as lead_name, lead.phone," "product.name as product_name, product.price, 'order'.product_qty," "order_status.name as status " "FROM 'order' " "JOIN lead ON 'order'.lead_id = lead.id " "JOIN product ON 'order'.product_id = product.id " "JOIN order_status ON 'order'.status_id = order_status.id " "ORDER BY 'order'.id DESC ").fetchall() return render_template('orders/index.html', orders=orders)
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None user = db.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() if user is None or not check_password_hash(user['password'], password): error = 'Username or password is incorrect.' if error is None: session.clear() session['user_id'] = user['id'] return redirect(url_for('index')) flash(error) return render_template('auth/login.html')
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' elif db.execute('SELECT id FROM user WHERE username = ?', (username, )).fetchone() is not None: error = 'User {} is already registered.'.format(username) if error is None: db.execute('INSERT INTO user (username, password) VALUES (?, ?)', (username, generate_password_hash(password))) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def new_orders_count_tag(): db = get_db() new_orders_count = db.execute("SELECT COUNT(*) FROM 'order' " "WHERE 'order'.status_id = 1 ").fetchall() return dict(new_orders_count=new_orders_count[0][0])