def register(): if request.method=='POST': userData=User(name=request.form['name'],email=request.form['email'],password=request.form['password']) session.add(userData) session.commit() return redirect(url_for('index')) else: return render_template('register.html')
def createUser(login_session): newUser = User(name=login_session['username'], email=login_session['email'], picture=login_session['picture']) session.add(newUser) session.commit() user = session.query(User).filter_by(email=login_session['email']).one() return user.id
def addData(): if request.method == 'POST': newData = User(name=request.form['name'], email=request.form['email'], password=request.form['password']) session.add(newData) session.commit() flash("New Data added....") return redirect(url_for('home')) else: return render_template("new.html")
def createUser(login_session): """ Create a new user in the app database Args: login_session (data type: dict): dictionary that contain information from the user provided with the google authentication Returns: the id for the user that become the identification number in all his/her subsequent sessions. """ newUser = User(name=login_session['username'], email=login_session['email'], picture=login_session['picture']) session.add(newUser) session.commit() email = login_session['email'] user = session.query(User).filter_by(email=email).limit(1).one() return user.id
def reg(): if request.method=='POST': userData=User(name=request.form['name'], email=request.form['email'], password=request.form['password'], session.add(userData) session.commit() return redirect(url_for('navData')) else: return render_template('register.html') @app.route("/new",methods=['POST','GET']) def addData(): if request.method=='POST': newData=Register(name=request.form['name'], surname=request.form['surname'], mobile=request.form['mobile'], email=request.form['email'], branch=request.form['branch'], roll=request.form['roll']) session.add(newData) session.commit() flash("New Data added...") return redirect(url_for('showData')) else: return render_template('new.html') @app.route("/navigation") def navData(): return render_template('navigation.html') @app.route("/edit/<int:register_id>",methods=['POST','GET']) def editData(register_id): editedData=session.query(Register).filter_by(id=register_id).one() if request.method=='POST': editedData.name=request.form['name'] editedData.surname=request.form['surname'] editedData.mobile=request.form['mobile'] editedData.email=request.form['email'] editedData.branch=request.form['branch'] editedData.roll=request.form['roll'] session.add(editedData) session.commit() flash("Data edited...{}".format(editedData.name)) return redirect(url_for('showData')) else: return render_template('edit.html',register=editedData) @app.route("/delete/<int:register_id>",methods=['POST','GET']) def deleteData(register_id): deletedData=session.query(Register).filter_by(id=register_id).one() if request.method=='POST': session.delete(deletedData) session.commit() flash("Data Deleted...{}".format(deletedData.name)) return redirect(url_for('showData')) else: return render_template('delete.html',register=deletedData) @app.route("/login",methods=['POST','GET']) def login(): if current_user.is_authenticated: return redirect(url_for('showData')) try: if request.method=='POST': user=session.query(User).filter_by(email=request.form['email'],password=request.form['password']).first() if user: login_user(user) return redirect(url_for('showData')) else: flash("Invalid login..") else: return render_template('login.html',title="login") except Exception as e: flash("Login Failed...") else: return render_template('login.html',title='login') @app.route("/logout") def logout(): logout_user() return redirect(url_for('navData')) @login_manager.user_loader def load_user(user_id): return session.query(User).get(int(user_id)) if __name__=='__main__': app.run(debug=True)
# declaratives can be accessed through a DBSession instance Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Precious stones user = User(name='nicolas', email='*****@*****.**') session.add(user) session.commit() mineral1 = Mineral(name='native elements', user_id=1) session.add(mineral1) session.commit() item1 = Item(name='gold', origin='California, USA', colour='rich yellow', price='2.49', stones=mineral1, hardness='2.5-3', description='Gold'