Example #1
0
def register():
	form = RegisterForm()
	if form.validate():
		#if user registers with email, create a username, from their email
		if form.newusername.data.find('@') != -1:
			username = form.newusername.data.split('@')[0]
			email = form.newusername.data
		else:
			username = form.newusername.data
			email = None
		#checks if username already exists
		checkuser = User.query.filter_by(username = username).first()
		if checkuser != None:
			flash('user name already taken')
			return redirect(url_for('index'))
		else:
			#add new user to database
			newuser = models.User(username=username, email = email, password=generate_password_hash(form.password.data), fullname = form.fullname.data)
			dbsess.add(newuser)
			dbsess.commit()
			login_user(newuser)
		#ins = User.insert().values(username=form.username.data, password=generate_password_hash(form.password.data), fullname=form.fullname.data)
		#db.session.execute(ins)
		#db.session.commit()
		return redirect(url_for('index'))
	return render_template('index.html',
		registerform = form,
		loginform = LoginForm())
Example #2
0
def createpost():
	form = CreatePostForm(request.form)
	if form.validate_on_submit():
		newpost = Post(posttext = form.posttext.data, timestamp = datetime.datetime.now(), author = g.user)
		dbsess.add(newpost)
		dbsess.commit()
    	return redirect(url_for('index'))
	return render_template('index.html',form = form)