-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
215 additions
and
33 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from . import app, db | ||
from .models import User | ||
from flask import render_template, redirect, url_for, request, Blueprint, flash, session | ||
from werkzeug.security import generate_password_hash, check_password_hash | ||
|
||
bp = Blueprint('auth', __name__, url_prefix='/auth') | ||
|
||
|
||
@bp.route('/register', methods=['POST', 'GET']) | ||
def register(): | ||
if request.method == 'POST': | ||
username = request.form['username'] | ||
password = request.form['password'] | ||
identical_user = User.query.filter(User.username == username).first() | ||
error = None | ||
|
||
if not username: | ||
error = 'Username field is qequired' | ||
elif identical_user is not None: | ||
error = 'This username is already registered' | ||
elif not password: | ||
error = 'password field is required' | ||
|
||
if error is not None: | ||
return render_template('auth/register.html', error=error) | ||
else: | ||
new_user = User(username=username, password=generate_password_hash(password)) | ||
db.session.add(new_user) | ||
db.session.commit() | ||
|
||
return redirect(url_for('auth.login')) | ||
# flash(error) | ||
else: | ||
return render_template('auth/register.html') | ||
|
||
|
||
@bp.route('/login', methods=['GET', 'POST']) | ||
def login(): | ||
if request.method == 'POST': | ||
username = request.form['username'] | ||
password = request.form['password'] | ||
user = User.query.filter_by(username=username).first() | ||
all_identical_users = User.query.filter(User.username == username).first() | ||
error = None | ||
|
||
if not username: | ||
error = 'USERNAME is required Ya Dog :) ' | ||
# To check if the password not correct ??? | ||
# elif username not in | ||
elif all_identical_users is None: | ||
error = 'User Not Registered' | ||
elif not check_password_hash(user.password, password): | ||
error = 'Incorrect password Ya Animal :) ' | ||
|
||
if error is not None: | ||
flash(error) | ||
return render_template('auth/login.html', error=error, | ||
all_identical_users=all_identical_users) | ||
else: | ||
# create new session | ||
session.clear() | ||
session['user-id'] = user.id | ||
return redirect(url_for('index')) | ||
|
||
return render_template('auth/login.html') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
from datetime import datetime | ||
from app import db | ||
|
||
|
||
class Post(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
title = db.Column(db.String(100), nullable=False) | ||
body = db.Column(db.Text, nullable=False) | ||
pub_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) | ||
id = db.Column(db.Integer, primary_key=True) | ||
title = db.Column(db.String(100), nullable=False) | ||
body = db.Column(db.Text, nullable=False) | ||
pub_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) | ||
|
||
def date(self): | ||
return self.pub_date.strftime('%d %B - %H:%M:%S (UTC)') | ||
|
||
def __repr__(self): | ||
return '<Post %r>' % self.title | ||
|
||
|
||
def date(self): | ||
return self.pub_date.strftime('%d %B - %H:%M:%S (UTC)') | ||
class User(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
username = db.Column(db.String(20), nullable=False, unique=True) | ||
password = db.Column(db.String(50), nullable=False) | ||
time_registered = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) | ||
|
||
def __repr__(self): | ||
return '<Post %r>' % self.title | ||
def __repr__(self): | ||
return '{}'.format(self.username) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{%with messages = get_flashed_messages()%} | ||
{%if messages%} | ||
{%for message in messages%} | ||
<ul>{{message}}</ul> | ||
{%endfor%} | ||
{%endif%} | ||
{%endwith%} | ||
{{all_identical_users}} | ||
<form method="POST"> | ||
<label for="username">Username</label> | ||
<input name="username" id="username" required> | ||
<label for="password">Password</label> | ||
<input type="password" name="password" id="password" required> | ||
<input type="submit" name="Login"> | ||
|
||
</form> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{%if error%} | ||
{{error}} | ||
{%endif%} | ||
|
||
{% block content %} | ||
<form method="POST"> | ||
<label for="username">Username</label> | ||
<input name="username" id="username" required> | ||
<label for="password">Password</label> | ||
<input type="password" name="password" id="password"> | ||
<input type="submit" name="Login"> | ||
</form> | ||
{% endblock %} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""modify method | ||
Revision ID: 12b611e156b0 | ||
Revises: 37b3916ccd8b | ||
Create Date: 2018-06-09 01:23:29.668766 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '12b611e156b0' | ||
down_revision = '37b3916ccd8b' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_unique_constraint(None, 'user', ['username']) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint(None, 'user', type_='unique') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""users table | ||
Revision ID: 37b3916ccd8b | ||
Revises: e58b08b6e626 | ||
Create Date: 2018-06-08 18:16:07.112604 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '37b3916ccd8b' | ||
down_revision = 'e58b08b6e626' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('user', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('username', sa.String(length=20), nullable=False), | ||
sa.Column('password', sa.String(length=50), nullable=False), | ||
sa.Column('time_registered', sa.DateTime(), nullable=False), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('user') | ||
# ### end Alembic commands ### |
Binary file added
BIN
+756 Bytes
migrations/versions/__pycache__/12b611e156b0_modify_method.cpython-36.pyc
Binary file not shown.
Binary file added
BIN
+930 Bytes
migrations/versions/__pycache__/37b3916ccd8b_users_table.cpython-36.pyc
Binary file not shown.
Binary file modified
BIN
-3 Bytes
(100%)
migrations/versions/__pycache__/e58b08b6e626_.cpython-36.pyc
Binary file not shown.