Skip to content

Commit

Permalink
practice
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Vemuri authored and Anand Vemuri committed Apr 23, 2017
0 parents commit dedb71c
Show file tree
Hide file tree
Showing 27 changed files with 291 additions and 0 deletions.
3 changes: 3 additions & 0 deletions auth-todo-1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
venv/**
instance/**
migrations/**
39 changes: 39 additions & 0 deletions auth-todo-1/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from flask import Flask
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_wtf.csrf import CSRFProtect

from config import app_config

CONFIG_PYFILE = 'config.py'

login_manager = LoginManager()
db = SQLAlchemy()
LOGIN_MESSAGE = "You Are Unauthorized to View This Page"
LOGIN_VIEW = "auth.login"
csrf = CSRFProtect()

def create_app(config_name):
app = Flask(__name__,instance_relative_config=True)
app.config.from_object(app_config[config_name])
app.config.from_pyfile(CONFIG_PYFILE)


from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)

from .home import home as home_blueprint
app.register_blueprint(home_blueprint)

login_manager.init_app(app)
login_manager.login_message = LOGIN_MESSAGE
login_manager.login_view = LOGIN_VIEW

db.init_app(app)
csrf.init_app(app)
migrate = Migrate(app,db)

from app import models

return app
Binary file added auth-todo-1/app/__init__.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions auth-todo-1/app/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from flask import Blueprint

auth = Blueprint('auth',__name__)

from . import views
Binary file added auth-todo-1/app/auth/__init__.pyc
Binary file not shown.
15 changes: 15 additions & 0 deletions auth-todo-1/app/auth/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField, SubmitField, ValidationError
from wtforms.validators import DataRequired, EqualTo

from ..models import User


class RegistrationForm(FlaskForm):
username = StringField('username',validators=[DataRequired()])
password = PasswordField('password', validators=[DataRequired()])
confirm_password = PasswordField('confirmPassword')

def validate_username(self,field):
if User.query.filter_by(username=field.data).first():
raise ValidationError("Username Already in Use")
Binary file added auth-todo-1/app/auth/forms.pyc
Binary file not shown.
26 changes: 26 additions & 0 deletions auth-todo-1/app/auth/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from flask import render_template


from . import auth
from forms import RegistrationForm
from .. import db
from ..models import User

@auth.route('/login')
def login():
return render_template('/auth/login.html')

@auth.route('/register',methods=['GET','POST'])
def register():
form = RegistrationForm()

if form.validate_on_submit():

user = User(username=form.username.data,
password=form.password.data)
db.session.add(user)
db.session.commit()
#flash('You have successfully registered! You may now login.')
return redirect(url_for('auth.login'))
print(form.errors)
return render_template('/auth/register.html')
Binary file added auth-todo-1/app/auth/views.pyc
Binary file not shown.
7 changes: 7 additions & 0 deletions auth-todo-1/app/home/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from flask import Blueprint

home = Blueprint('home',__name__)

from . import views


Binary file added auth-todo-1/app/home/__init__.pyc
Binary file not shown.
Empty file added auth-todo-1/app/home/forms.py
Empty file.
7 changes: 7 additions & 0 deletions auth-todo-1/app/home/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from flask import render_template

from . import home

@home.route('/')
def hello():
return render_template('home/index.html')
Binary file added auth-todo-1/app/home/views.pyc
Binary file not shown.
27 changes: 27 additions & 0 deletions auth-todo-1/app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash

from app import db,login_manager


class User(UserMixin,db.Model):
__tablename__ = 'users'

id = db.Column(db.Integer,primary_key=True)
username = db.Column(db.String(60),unique=True,index=True)
password_hash = db.Column(db.String(128))

@property
def password(self):
raise AttributeError("Password Attribute not Readable")

@password.setter
def password(self,password):
self.password_hash = generate_password_hash(password)

def verify_password(self,password):
return check_password_hash(self.password,password)

@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
Binary file added auth-todo-1/app/models.pyc
Binary file not shown.
10 changes: 10 additions & 0 deletions auth-todo-1/app/static/components/registration/RegistrationApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var RegistrationApp = React.createClass({
render : function(){
return (
<div>
<RegistrationBanner/>
<RegistrationForm/>
</div>
)
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var RegistrationBanner = React.createClass({
render:function(){
return(
<h3> Register Here!</h3>
)
}
});
60 changes: 60 additions & 0 deletions auth-todo-1/app/static/components/registration/RegistrationForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var RegistrationForm = React.createClass({
getInitialState:function(){
return ({
username: "",
password:"",
confirmPassword:""
});
},
toggleUsername:function(e){
this.setState({
username:e.target.value
});
},
togglePassword:function(e){
this.setState({
password:e.target.value
});
},
toggleConfirmPassword:function(e){
this.setState({
confirmPassword:e.target.value
});
},
register:function(e){
console.log(e);
e.preventDefault();

if(this.state.password === this.state.confirmPassword){

axios.post('/register',{
username:this.state.username,
password:this.state.password,
confirmPassword:this.state.confirmPassword

})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

}
else{
alert("Passwords are not in alignment");
}
},

render:function(){
return(
<form onSubmit={this.register}>
<input type="text" value={this.state.username} onChange={this.toggleUsername}/>
<input type="password" value={this.state.password} onChange={this.togglePassword}/>
<input type="password" value={this.state.confirmPassword} onChange={this.toggleConfirmPassword}/>
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<input type="submit" value="Register"/>
</form>
)
}
});
7 changes: 7 additions & 0 deletions auth-todo-1/app/templates/auth/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
<h1> Login Page Under Construction </h1>
</body>
</html>
27 changes: 27 additions & 0 deletions auth-todo-1/app/templates/auth/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html lang="en">
<head>
<script src="https://fb.me/react-0.12.2.js"></script>
<script src="https://fb.me/JSXTransformer-0.12.2.js"></script>
<script src="{{url_for('static',filename='components/registration/RegistrationApp.js')}}" type="text/jsx"></script>
<script src="{{url_for('static',filename='components/registration/RegistrationBanner.js')}}" type="text/jsx"></script>
<script src="{{url_for('static',filename='components/registration/RegistrationForm.js')}}" type="text/jsx"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id='registration'></div>
<script type="text/jsx">
React.renderComponent(<RegistrationApp/>,document.getElementById('registration'));
</script>
<script type="text/javascript">
var csrf_token = "{{ csrf_token() }}";

$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrf_token);
}
}
});
</script>
</body>
</html>
8 changes: 8 additions & 0 deletions auth-todo-1/app/templates/home/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE>
<html>
<head>
</head>
<body>
<h1>Under Construction</h1>
</body>
</html>
13 changes: 13 additions & 0 deletions auth-todo-1/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Config(object):
Debug = True

class ProductionConfig(Config):
Debug = False

class DevelopmentConfig(Config):
Debug = True

app_config = {
'development' : DevelopmentConfig,
'production' : ProductionConfig
}
Binary file added auth-todo-1/config.pyc
Binary file not shown.
21 changes: 21 additions & 0 deletions auth-todo-1/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
alembic==0.9.1
appdirs==1.4.3
click==6.7
Flask==0.12.1
Flask-Login==0.4.0
Flask-Migrate==2.0.3
Flask-Script==2.0.5
Flask-SQLAlchemy==2.2
Flask-WTF==0.14.2
itsdangerous==0.24
Jinja2==2.9.6
Mako==1.0.6
MarkupSafe==1.0
MySQL-python==1.2.5
packaging==16.8
pyparsing==2.2.0
python-editor==1.0.3
six==1.10.0
SQLAlchemy==1.1.9
Werkzeug==0.12.1
WTForms==2.1
9 changes: 9 additions & 0 deletions auth-todo-1/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os

from app import create_app

config_name = os.getenv("FLASK_CONFIG")
app = create_app(config_name)

if __name__ == '__main__':
app.run()
Binary file added auth-todo-1/run.pyc
Binary file not shown.

0 comments on commit dedb71c

Please sign in to comment.