Commit 74cd8246 authored by Willard's avatar Willard

Implement login and registration as FormView

parent 0e07d5c1
......@@ -10,15 +10,15 @@ class OwnerLoginForm(FlaskForm):
def validate(self):
if not FlaskForm.validate(self):
return False, None
return False
owner = Owner.query.filter_by(username=self.username.data).first()
if owner is None:
flash('User does not exist!')
return False, None
return False
if not owner.check_password(self.password.data):
flash('Wrong password!')
return False, None
return False
return True, owner
......
......@@ -8,6 +8,7 @@ from forms import OwnerLoginForm, OwnerRegisterForm
from models import Owner, Stall, Dish
class FormView(View):
methods=['GET', 'POST']
def dispatch_request(self, **kwargs):
form = self.get_form()
if request.method == 'POST':
......@@ -30,44 +31,46 @@ def index():
else:
return render_template('landing.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
def not_logged_in(f):
@wraps(f)
def wrapper(*args, **kwargs):
if current_user.is_authenticated:
return redirect(url_for('stalls'))
return f(*args, **kwargs)
return wrapper
class LoginView(FormView):
decorators=[not_logged_in]
def render_post(self):
login_user(Owner.query.filter_by(username=self.get_form().username.data).first())
return redirect(url_for('stalls'))
def render_get(self):
return render_template('login.html', form=self.get_form())
def get_form(self):
return OwnerLoginForm()
form = OwnerLoginForm()
if request.method == 'POST':
is_valid, owner = form.validate()
if is_valid:
login_user(owner)
return redirect(url_for('stalls'))
else:
flash_form_errors(form)
app.add_url_rule('/login', view_func=LoginView.as_view('login'))
return render_template('login.html', form=form)
class RegisterView(FormView):
decorators=[not_logged_in]
def render_post(self):
form = self.get_form()
owner = Owner(form.name.data, form.username.data, form.email.data, form.password.data)
db.session.add(owner)
db.session.commit()
return redirect(url_for('login'))
def render_get(self):
return render_template('register.html', form=form)
def get_form(self):
return OwnerRegisterForm()
app.add_url_rule('/register', view_func=RegisterView.as_view('register'))
@app.route('/logout')
def logout():
logout_user()
return redirect('/')
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('stalls'))
form = OwnerRegisterForm()
if request.method == 'POST':
if form.validate():
owner = Owner(form.name.data, form.username.data, form.email.data, form.password.data)
db.session.add(owner)
db.session.commit()
return redirect(url_for('login'))
else:
flash_form_errors(form)
return render_template('register.html', form=form)
def stall_validate(f):
@wraps(f)
def wrapper(*args, **kwargs):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment