def add_update_user(): if not LoginController.athenticate_user(): return redirect(url_for("google.login")) resp = google.get("/oauth2/v2/userinfo") email = resp.json()["email"] message = "User created/updated successfully." form = UserForm() if form.validate_on_submit(): username = form.username.data dept_id = form.dept_id.data login_ip = form.login_ip.data user = UserModel.find_by_username(username) if user: user.dept_id = dept_id user.login_ip = login_ip else: user = UserModel(username, dept_id, login_ip) try: user.save() except: message = "Unable to save/update user" flash(message) return render_template('add_user.html', form=form, email=email)
def deleteUser(): form = UserForm() # If GET if request.method == 'GET': return render_template('deleteuser.html', form=form) # If POST else: if form.validate_on_submit(): user_id = request.form['user_id'] deleted_user = User.query.filter_by(user_id=user_id).first() Db.session.delete(deleted_user) Db.session.commit() return redirect(url_for('index')) else: return render_template('deleteuser.html', form=form)
def addUser(): form = UserForm() # If GET if request.method == 'GET': return render_template('adduser.html', form=form) # If POST else: if form.validate_on_submit(): first_name = request.form['first_name'] age = request.form['age'] new_user = User(first_name=first_name, age=age) Db.session.add(new_user) Db.session.commit() return redirect(url_for('index')) else: return render_template('adduser.html', form=form)
def mockUser(): form = UserForm() # If GET if request.method == 'GET': return render_template('mockuser.html', form=form) # If POST else: if form.validate_on_submit(): mock_num = request.form['mock_num'] # Iterate and print for i in range(0, int(mock_num)): mockUser = User(first_name="test", age=i + 1) Db.session.add(mockUser) Db.session.commit() return redirect(url_for('index')) else: return render_template('mockuser.html', form=form)
def updateUser(): form = UserForm() # If GET if request.method == 'GET': return render_template('updateuser.html', form=form) # If POST else: if form.validate_on_submit(): first_name_old = request.form['first_name'] age_old = request.form['age'] first_name_new = request.form['first_name_new'] age_new = request.form['age_new'] old_user = User.query.filter_by(first_name=first_name_old).first() new_user = User(first_name=first_name_new, age=age_new) Db.session.delete(old_user) Db.session.add(new_user) Db.session.commit() return redirect(url_for('index')) else: return render_template('updateuser.html', form=form)