Commit 74cd8246 authored by Willard's avatar Willard

Implement login and registration as FormView

parent 0e07d5c1
...@@ -10,15 +10,15 @@ class OwnerLoginForm(FlaskForm): ...@@ -10,15 +10,15 @@ class OwnerLoginForm(FlaskForm):
def validate(self): def validate(self):
if not FlaskForm.validate(self): if not FlaskForm.validate(self):
return False, None return False
owner = Owner.query.filter_by(username=self.username.data).first() owner = Owner.query.filter_by(username=self.username.data).first()
if owner is None: if owner is None:
flash('User does not exist!') flash('User does not exist!')
return False, None return False
if not owner.check_password(self.password.data): if not owner.check_password(self.password.data):
flash('Wrong password!') flash('Wrong password!')
return False, None return False
return True, owner return True, owner
......
...@@ -8,6 +8,7 @@ from forms import OwnerLoginForm, OwnerRegisterForm ...@@ -8,6 +8,7 @@ from forms import OwnerLoginForm, OwnerRegisterForm
from models import Owner, Stall, Dish from models import Owner, Stall, Dish
class FormView(View): class FormView(View):
methods=['GET', 'POST']
def dispatch_request(self, **kwargs): def dispatch_request(self, **kwargs):
form = self.get_form() form = self.get_form()
if request.method == 'POST': if request.method == 'POST':
...@@ -30,44 +31,46 @@ def index(): ...@@ -30,44 +31,46 @@ def index():
else: else:
return render_template('landing.html') return render_template('landing.html')
@app.route('/login', methods=['GET', 'POST']) def not_logged_in(f):
def login(): @wraps(f)
if current_user.is_authenticated: 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')) 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() app.add_url_rule('/login', view_func=LoginView.as_view('login'))
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)
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') @app.route('/logout')
def logout(): def logout():
logout_user() logout_user()
return redirect('/') 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): def stall_validate(f):
@wraps(f) @wraps(f)
def wrapper(*args, **kwargs): 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