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,43 +31,45 @@ def index(): ...@@ -30,43 +31,45 @@ 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)
def wrapper(*args, **kwargs):
if current_user.is_authenticated: if current_user.is_authenticated:
return redirect(url_for('stalls')) return redirect(url_for('stalls'))
return f(*args, **kwargs)
return wrapper
form = OwnerLoginForm() class LoginView(FormView):
if request.method == 'POST': decorators=[not_logged_in]
is_valid, owner = form.validate() def render_post(self):
if is_valid: login_user(Owner.query.filter_by(username=self.get_form().username.data).first())
login_user(owner)
return redirect(url_for('stalls')) return redirect(url_for('stalls'))
else: def render_get(self):
flash_form_errors(form) return render_template('login.html', form=self.get_form())
def get_form(self):
return render_template('login.html', form=form) return OwnerLoginForm()
@app.route('/logout') app.add_url_rule('/login', view_func=LoginView.as_view('login'))
def logout():
logout_user()
return redirect('/')
@app.route('/register', methods=['GET', 'POST']) class RegisterView(FormView):
def register(): decorators=[not_logged_in]
if current_user.is_authenticated: def render_post(self):
return redirect(url_for('stalls')) form = self.get_form()
form = OwnerRegisterForm()
if request.method == 'POST':
if form.validate():
owner = Owner(form.name.data, form.username.data, form.email.data, form.password.data) owner = Owner(form.name.data, form.username.data, form.email.data, form.password.data)
db.session.add(owner) db.session.add(owner)
db.session.commit() db.session.commit()
return redirect(url_for('login')) return redirect(url_for('login'))
else: def render_get(self):
flash_form_errors(form)
return render_template('register.html', form=form) 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('/')
def stall_validate(f): def stall_validate(f):
@wraps(f) @wraps(f)
......
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