def register(): from app.forms import RegistrationForm form = RegistrationForm() if request.method == "POST": if len(request.form["username"]) > 0 and len( request.form["password"]) > 0: from werkzeug.security import generate_password_hash from app.models import UserTable username = form.username.data password = form.password.data u = UserTable() res = u.select_user(username) if len(res) > 0: flash("Username busy") return redirect(url_for('register')) hash = generate_password_hash(password) u.insert_user({"username": username, "password_hash": hash}) flash("Registered") return redirect(url_for('index')) return render_template("register.html", form=form, title="Registration")
def drop_tables(): ut = UserTable() bt = BookTable() st = SubmissionTable() ut.drop_table() bt.drop_table() st.drop_table()
def create_tables(): ut = UserTable() bt = BookTable() st = SubmissionTable() ut.create_table() bt.create_table() st.create_table()
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegisterForm() if form.validate_on_submit(): user = UserTable(username=form.username.data, email=form.email.data) user.set_password(form.password.data) db.session.add(user) db.session.commit() add_to_index('user_table', user) flash('Registration successful. Please login.') return redirect(url_for('auth.login')) return render_template('auth/register.html', title='Register', form=form)
def post(self): """ Add new user to the table :return: table and user id's :rtype: dict/json """ # Parsing request JSON fields args = table_request.parse_args() # Login of authorized user stores in Flask g object user = User.query.filter_by(username=g.user.username).first() if args['table_key'] is None: abort(400, message="Input payload validation failed") table = Table.query.filter_by(table_key=args['table_key']).first() if table is None: abort(404, message="Table keyword '{}' does not exist".format( args['table_key'])) # Create user-table dependency user_table = UserTable(user_id=user.id, table_id=table.id) try: db.session.add(user_table) db.session.commit() # Return JSON using template return user_table, 201 except IntegrityError: db.session.rollback() abort(406, message="Username '{}' already connected with table".format( user.username))
def login(): from app.forms import LoginForm if current_user.is_authenticated: return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): from werkzeug.security import check_password_hash from app.models import UserTable, User username = form.username.data password = form.password.data u = UserTable() user_data = u.select_user(username) if len(user_data) <= 0: flash("No such user") return redirect(url_for('login')) user = User(user_data[0]) if check_password_hash(user.password_hash, password): login_user(user) return redirect(url_for('index')) flash("wrong wrong wrong") return redirect(url_for('login')) return render_template("login.html", title="Login", form=form)
def post(self): """ Create new table :return: New table key phrase :rtype: dict/json """ # Login of authorized user stores in Flask g object user = User.query.filter_by(username=g.user.username).first() # Create table and add to database key = self.newPhraseIfAlreadyExists() table = Table(table_key=key, table_date=datetime.utcnow()) # TODO: Delete try-except or stop recursive key generation after X tries try: db.session.add(table) db.session.flush() except IntegrityError: db.session.rollback() abort(409, message="Table '{}' already exist".format(key)) # Create user-table dependency user_table = UserTable(user_id=user.id, table_id=table.id) try: db.session.add(user_table) db.session.commit() # Return JSON using template return table, 201 except IntegrityError: db.session.rollback() abort(406, message="Username '{}' already connected with table".format( user.username))