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

Refactor #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@

@login_manager.user_loader
def load_user(user_id):
return User.get(email=user_id)
try:
return User.get(id=user_id)
except User.DoesNotExist:
pass

@login_manager.unauthorized_handler
def unauthorized_callback():
return redirect('/login')

from app.models.core import db
User.create_table(fail_silently=True)
db.register_fields({'primary_key': 'BIGINT AUTOINCREMENT'})

from app.views.accounts import *
1 change: 1 addition & 0 deletions app/forms/user_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

class LoginForm(Form):
email = StringField('email', validators=[DataRequired()])
username = StringField('username', validators=[DataRequired()])
password = StringField('password', validators=[DataRequired()])
24 changes: 17 additions & 7 deletions app/models/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,42 @@
from werkzeug import security
from app.models.core import Base

class UserAlreadyExists(BaseException):
class EmailAlreadyUsed(BaseException):
pass

class UsernameAlreadyUsed(BaseException):
pass

class User(Base, UserMixin):
'''
User model that represnts all user types in the app
used for authentcation and authorization
Note: should only contain methods and fields
related to this functionalties only
'''
email = TextField(primary_key=True)
email = TextField(unique=True)
username = TextField(unique = True)
password = TextField()
id = PrimaryKeyField()

@staticmethod
def register(email, password):
def register(email, username, password):
password = security.generate_password_hash(password, method='pbkdf2:sha1', salt_length=8)
try:
user = User.get(email=email)
raise UserAlreadyExists
if User.get(email=email) :
raise EmailAlreadyUsed
except User.DoesNotExist:
user = User.create(email=email, password=password)
try :
if User.get(username=username) :
raise UsernameAlreadyUsed
except User.DoesNotExist :
pass
user = User.create(email=email, username=username, password=password)
return user

def authenticat_password(self, password):
return security.check_password_hash(self.password, password)


def get_id(self):
return self.email
return self.id
13 changes: 13 additions & 0 deletions app/templates/edit_profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{%extends 'base.html'%}
{% block title %}
<title>Edit Profile</title>
{% endblock %}
{%block content%}
<form method='post' name='Edit'>
<h4>Edit</h4>
<p>{{ form.username.label}} {{form.username}}</p>
<p>{{ form.email.label }} {{ form.email }}</p>
<p>{{ form.password.label }} {{ form.password }}</p>
<input type='submit' value='Ok'>
</form>
{%endblock%}
2 changes: 1 addition & 1 deletion app/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{%block content%}
<form method='post' name='login'>
<h4>Login</h4>
<p>{{ form.email.label }} {{ form.email }}</p>
<p>{{ form.email.label }} or username {{ form.email }}</p>
<p>{{ form.password.label }} {{ form.password }}</p>
<input type='submit' value='Login'>
</form>
Expand Down
8 changes: 8 additions & 0 deletions app/templates/profile_user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends "base.html"%}
{%block content%}
{%if current_user.username == user.username %}
<a href={{url_for('edit_profile')}}>Edit</a>
{%endif%}
<p>username :{{ user.username }}</p>
<p>email : {{user.email}}</p>
{%endblock%}
1 change: 1 addition & 0 deletions app/templates/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{%block content%}
<form method='post' name='login'>
<h4>Register</h4>
<p>{{ form.username.label}} {{form.username}}</p>
<p>{{ form.email.label }} {{ form.email }}</p>
<p>{{ form.password.label }} {{ form.password }}</p>
<input type='submit' value='SignUp'>
Expand Down
65 changes: 53 additions & 12 deletions app/views/accounts.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,60 @@
from flask import render_template, request, redirect, url_for, flash
from flask_login import login_user, logout_user, current_user

from flask_login import login_user, logout_user, current_user, login_required
import peewee
from app import App
from app.forms.user_forms import LoginForm
from app.models.accounts import User, UserAlreadyExists

from app.models.accounts import User, EmailAlreadyUsed, UsernameAlreadyUsed
from werkzeug import security
from app.models.core import db

@App.route('/index')
@login_required
def index():
return "Hello " + current_user.email
return "Hello " + current_user.username

@App.route('/<username>')
@login_required
def profile(username):
try :
user = User.get(username=username)
except User.DoesNotExist :
flash("User doesn't exist")
return redirect(url_for('register'))
return render_template('profile_user.html', user=user)

@App.route("/edit_profile", methods=('GET', 'POST'))
@login_required
def edit_profile():
global current_user
user = User.get(email=current_user.email)
form = LoginForm(username=user.username, email=user.email)
if request.method == "POST" :
if form.username.data != user.username :
try:
db.execute_sql("UPDATE User SET username=? WHERE email=?;", (form.username.data, user.email))
except peewee.IntegrityError:
flash("Username already registered")
if form.email.data != user.email:
try :
db.execute_sql("UPDATE User SET email=? WHERE username=?;", ( form.email.data,user.username))
except peewee.IntegrityError :
flash("Email already registered")
if form.password.data != '':
User.update(password=security.generate_password_hash(form.password.data, method='pbkdf2:sha1', salt_length=8)).where(username==username).execute()
return render_template("edit_profile.html", form=form)

@App.route('/register', methods=('GET', 'POST'))
def register():
form = LoginForm()
if request.method == 'POST':
try:
User.register(form.email.data, form.password.data)
except UserAlreadyExists :
flash('User already registered')
return redirect(url_for('login'))
User.register(form.email.data, form.username.data, form.password.data)
except EmailAlreadyUsed :
flash('Email already registered')
return render_template('register.html', form=form)
except UsernameAlreadyUsed :
flash('Username already registered')
return render_template('register.html', form=form)
flash('Created user succesfully')
return redirect(url_for('login'))
return render_template('register.html', form=form)
Expand All @@ -29,16 +65,21 @@ def login():
if request.method == 'POST':
try:
user = User.get(email=form.email.data)
except User.DoesNotExist:
try :
user = User.get(username=form.email.data)
except User.DoesNotExist:
user = None
if user:
check = user.authenticat_password(form.password.data)
if check:
login_user(user)
return redirect(url_for('index'))
except User.DoesNotExist:
pass
flash("wrong password or email")
flash("wrong password or email/username")
return render_template('login.html', form=form)

@App.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
Binary file removed example.db
Binary file not shown.
Binary file modified kawn.db
Binary file not shown.