Skip to content

Commit

Permalink
added registration form
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKayAce committed Jul 12, 2019
1 parent 9576e8e commit 8bc9bff
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 7 deletions.
Binary file modified __pycache__/config.cpython-37.pyc
Binary file not shown.
Binary file modified __pycache__/dvdcollection.cpython-37.pyc
Binary file not shown.
Binary file modified app.db
Binary file not shown.
1 change: 1 addition & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
db = SQLAlchemy(app)
migrate = Migrate(app = app, db = db)
login = LoginManager(app)
login.login_view = "login" # Requires login at first visit

from app import routes, models
Binary file modified app/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file modified app/__pycache__/routes.cpython-37.pyc
Binary file not shown.
33 changes: 32 additions & 1 deletion app/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired
from wtforms.validators import Email
from wtforms.validators import ValidationError
from wtforms.validators import EqualTo

from app import User

class LoginForm(FlaskForm):
"""
Class for handling login for the page
"""
username = StringField("Username", validators=[DataRequired()])
password = PasswordField ("Password", validators=[DataRequired()])
remember_me = BooleanField("Remember Me?")
submit = SubmitField("Sign In")
submit = SubmitField("Sign In")

class RegistrationForm(FlaskForm):
"""
Handles User registration
Creates a new user in the database
"""
username = StringField("Username", validators=[DataRequired()])
email = StringField("Email",validators=[DataRequired(),Email()])
password = PasswordField ("Password", validators=[DataRequired()])
password_repeat = PasswordField(
"Repeat Password",
validators = [DataRequired(),EqualTo("password")])
submit = SubmitField("Register")

def validate_username(self, username):
user = User.query.filter_by(username = username.data).first()
if user is not None:
raise ValidationError("Invalid Username, already taken")

def validate_email(self, email):
user = User.query.filter_by(email= email.data).first()
if user is not None:
raise ValidationError("Email already registered, please reset your password")
36 changes: 31 additions & 5 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
from flask import render_template, flash, redirect, url_for
from flask import render_template
from flask import flash
from flask import redirect
from flask import url_for
from flask import request
from flask_login import current_user
from flask_login import login_user
from flask_login import logout_user
from flask_login import login_required
from werkzeug.urls import url_parse

from app import app
from app.forms import LoginForm
from app.models import User


@app.route("/")
@app.route("/index")
@login_required
def index():
user = {"username": "Jesper"}
posts = [
Expand All @@ -17,12 +29,26 @@ def index():
"body":"Keep on trucking"
}
]
return render_template('index.html', title = "Home", user=user, posts=posts)
return render_template('index.html', title = "Home Page", posts=posts)

@app.route("/login", methods=["GET","POST"])
def login():
if current_user.is_authenticated:
return redirect(url_for("index"))
form = LoginForm()
if form.validate_on_submit():
flash(f"Login Requested for user {form.username.data}, Remember Me?={form.remember_me.data}")
return redirect(url_for("index"))
return render_template("login.html", title="Sign In", form=form)
user = User.query.filter_by(username = form.username.data).first()
if user is None or not user.check_password(form.password.data):
flash("Invalid username or password")
return redirect(url_for("login"))
login_user(user, remember = form.remember_me.data)
next_page = request.args.get("next")
if not next_page or url_parse(next_page).netloc != "":
next_page = url_for("index")
return redirect(next_page)
return render_template("login.html", title="Sign In", form=form)

@app.route("/logout")
def logout():
logout_user()
return redirect(url_for("index"))
4 changes: 4 additions & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
<body>
<div>Microblog:
<a href="{{ url_for('index') }}">Home</a>
{% if current_user.is_anonymous %}
<a href="{{ url_for('login') }}">Login</a>
{% else %}
<a href="{{ url_for('logout') }}">Logout</a>
{% endif %}
</div>
<hr>
{% with messages = get_flashed_messages() %}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "base.html" %}

{% block content %}
<h1>Hello {{ user.username }}</h1>
<h1>Hello {{ current_user.username }}</h1>
{% for post in posts %}
<div>
<p>
Expand Down

0 comments on commit 8bc9bff

Please sign in to comment.