Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ally/registration #54

Open
wants to merge 19 commits into
base: dev-main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager


# Construct core Flask application with embedded Dash app.
Expand All @@ -11,6 +12,10 @@
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view='login'

from app import routes,models


with app.app_context():
Expand Down
20 changes: 19 additions & 1 deletion app/forms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired
from wtforms.validators import DataRequired, ValidationError, Email, EqualTo
from app.models import User


class LoginForm(FlaskForm):
username = StringField("Username", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()])
remember_me = BooleanField("Remember Me")
submit = SubmitField("Log In")

class RegistrationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
email = StringField('Email',validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
password2 = PasswordField('Repeat Password', validators=[DataRequired(), EqualTo('password')])
submit = SubmitField('Register')

def validate_usernames(self, username):
user = User.query.filter_by(username=username.data).first()
if user is not None:
raise ValidationError('This username is already in use.')

def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user is not None:
raise ValidationError('Please use a different email address')
15 changes: 13 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime
from app import db
from app import db, login
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin


user_coins = db.Table(
Expand All @@ -13,7 +15,7 @@
)


class User(db.Model):
class User(db.Model, UserMixin):
__tablename__ = "user"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
Expand All @@ -26,6 +28,15 @@ class User(db.Model):
def __repr__(self):
return "<User {}>".format(self.username)

def set_password(self, password):
self.password_hash = generate_password_hash(password)

def check_password(self, password):
return check_password_hash(self.password_hash, password)

@login.user_loader
def load_user(id):
return User.query.get(int(id))

class Coin(db.Model):
__tablename__ = "coin"
Expand Down
48 changes: 36 additions & 12 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from flask import render_template, flash, redirect, url_for
from app import app
from app.forms import LoginForm
from flask import render_template, flash, redirect, url_for, request
from app import app,db
from app.forms import LoginForm, RegistrationForm
from flask_login import current_user, login_user, logout_user, login_required
from werkzeug.urls import url_parse


@app.route("/")
Expand All @@ -11,22 +13,44 @@ def index():
{"author": {"username": "Sal"}, "body": "Beautiful day in Portland!"},
{"author": {"username": "Michael"}, "body": "Pyrates are so cool!"},
{"author": {"username": "Scott"}, "body": "Ooh, Bootstrap!"},
{"author": {"username": "Allyson"}, "body": "Hello, World!"},
]
return render_template("index.html", title="Home", user=user, posts=posts)


@app.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
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}, "
f"remember_me={form.remember_me.data}"
)
return redirect(url_for("index"))
return render_template("login.html", title="Log In", form=form)
user=User.query.filter_by(username=form.username.data).first()
CloudF1sh marked this conversation as resolved.
Show resolved Hide resolved
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(url_for('index'))
return render_template('login.html', title='Sign In', form=form)

@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))

@app.route("/register")

@app.route("/register", methods=['GET', 'POST'])
def register():
return render_template("wip.html")
if current_user.is_authenticated:
return redirect(url_for('index'))
form = RegistrationForm()
if form.validate_on_submit():
user = User(username=form.username.data, email=form.email.data)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
flash('WELCOME! YOU GOT CRYPTO!')
return redirect(url_for('login'))
return render_template('register.html', title='Register', form=form)
2 changes: 1 addition & 1 deletion app/templates/404.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "base.html" %}
{% block content %}
<div class="container">
<h1>Page Not Found</h1>
<h1>Page Has Been Eaten by a Gru</h1>
CloudF1sh marked this conversation as resolved.
Show resolved Hide resolved
<p><a href="{{ url_for('index') }}">Back to home</a></p>
</div>
{% endblock %}
4 changes: 4 additions & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="{{ url_for('register') }}"><span class="glyphicon glyphicon-user"></span> Register</a></li>
{% if current_user.is_anonymous %}
<li><a href="{{ url_for('login') }}"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
{% else %}
<li><a href="{{ url_for('/dash/') }}"><span class="glyphicon glyphicon-signal"></span> Dash Testing</a></li>
CloudF1sh marked this conversation as resolved.
Show resolved Hide resolved
<li><a href="{{ url_for('logout') }}"><span class="glyphicon glyphicon-log-out"></span> Logout</a></li>
{% endif %}
</ul>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions app/templates/login.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "base.html" %}

{% block content %}
<div class="container">
<div class="container">

<h1>Log In</h1>

Expand All @@ -24,6 +24,6 @@ <h1>Log In</h1>
<p>{{ form.remember_me() }} {{ form.remember_me.label }}</p>
<p>{{ form.submit() }}</p>
</form>

</div>
<p>New User?<a href="{{ url_for('register')}}">Click to Register!</a></p>
SalMireles marked this conversation as resolved.
Show resolved Hide resolved
</div>
{% endblock %}
37 changes: 37 additions & 0 deletions app/templates/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% extends 'base.html' %}

SalMireles marked this conversation as resolved.
Show resolved Hide resolved
{% block content %}
<h1>Register</h1>
<form action ="" method="post">
{{ form.hidden_tag() }}
<p>
{{ form.username.label }}<br>
{{ form.username(size=32) }}<br>
{% for error in form.username.errors %}
<span style="color:red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.email.label }}<br>
{{ form.email(size=32) }}<br>
{% for error in form.email.errors %}
<span style="color:red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.password.label }}<br>
{{ form.password(size=32) }}<br>
{% for error in form.password.errors %}
<span style="color:red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.password2.label }}<br>
{{ form.password2(size=32) }}<br>
{% for error in form.password2.errors %}
<span style="color:red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>{{ form.submit() }}</p>
</form>
{% endblock %}
57 changes: 56 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pycoingecko = "==2.2.0"
Werkzeug = "==2.0.0" # required pin https://github.com/plotly/dash/issues/1992
PyMySQL = "==1.0.2"
cryptography = "==37.0.2"
Flask-Login = "^0.6.1"
CloudF1sh marked this conversation as resolved.
Show resolved Hide resolved
email-validator = "^1.2.1"

[tool.poetry.dev-dependencies]
black = "==22.3.0"
Expand Down