-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
920472f
commit 26667c5
Showing
8 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
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,2 @@ | ||
CSRF_ENABLED = True | ||
SECRET_KEY = "It doesn't matter" |
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,15 +1,68 @@ | ||
from flask import Flask,render_template | ||
from flask import Flask,render_template,request,flash,redirect,url_for | ||
from wtforms import StringField, SubmitField,validators, PasswordField | ||
from flask_wtf import FlaskForm | ||
from flask_mysqldb import MySQL | ||
from passlib.hash import sha256_crypt | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
|
||
#Config to mysql | ||
app.config['MYSQL_HOST'] = 'localhost' | ||
app.config['MYSQL_USER'] = 'root' | ||
app.config['MYSQL_PASSWORD'] = 'abc230002' | ||
app.config['MYSQL_DB'] = 'FLASKAPP' | ||
app.config['MYSQL_CURSORCLASS'] = 'DictCursor' | ||
|
||
#Initialize the extension | ||
mysql = MySQL(app) | ||
|
||
@app.route('/') | ||
def index(): | ||
return render_template("index.html") | ||
|
||
@app.route('/<name>') | ||
def hello(name): | ||
return "Hello,%s" % name | ||
|
||
class RegisterForm(FlaskForm): | ||
name = StringField('Name', [validators.Length(min=1, max=50)]) | ||
username = StringField('Username', [validators.Length(min=4, max=25)]) | ||
email = StringField('Email', [validators.Length(min=6, max=50)]) | ||
password = PasswordField('Password', [ | ||
validators.DataRequired(), | ||
validators.EqualTo('confirm', message='Passwords do not match') | ||
]) | ||
confirm = PasswordField('Confirm Password') | ||
|
||
@app.route('/register',methods=["GET","POST"]) | ||
def register(): | ||
form = RegisterForm(request.form) | ||
if request.method == 'POST' and form.validate_on_submit(): | ||
name = form.name.data | ||
email = form.email.data | ||
username = form.username.data | ||
password = sha256_crypt.encrypt(str(form.password.data)) | ||
|
||
##Creat cursor | ||
cur = mysql.connection.cursor() | ||
|
||
#Execute query | ||
cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)",(name, email,username,password)) | ||
|
||
#Commit to DB | ||
mysql.connection.commit() | ||
|
||
#Close connection | ||
cur.close() | ||
|
||
flash("You are now registered.Please log in.",'success') | ||
|
||
return redirect(url_for('index')) | ||
|
||
return render_template('register.html',form=form) | ||
|
||
#CSRF | ||
#app.config.from_object('config') | ||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) | ||
app.secret_key="It doesn't matter" | ||
app.run(debug=True) |
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,12 @@ | ||
{% macro render_field(field) %} | ||
<dt>{{ field.label }} | ||
<dd>{{ field(**kwargs)|safe }} | ||
{% if field.errors %} | ||
<ul class=errors> | ||
{% for error in field.errors %} | ||
<li>{{ error }}</li> | ||
{% endfor %} | ||
</ul> | ||
{% endif %} | ||
</dd> | ||
{% endmacro %} |
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,7 @@ | ||
{% with messages = get_flashed_messages(with_categories=true) %} | ||
{% if messages %} | ||
{% for category, message in messages %} | ||
<div class="alert alert-{{ category }}" >{{ message }}</div> | ||
{% endfor %} | ||
{% endif %} | ||
{% endwith %} |
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
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,25 @@ | ||
{% extends 'layout.html' %} | ||
|
||
{% block body %} | ||
<h1>Register</h1> | ||
{% from "includes/_formhelpers.html" import render_field %} | ||
<form method="POST",action=""> | ||
{{ form.csrf_token }} | ||
<div class="form-group"> | ||
{{render_field(form.name,class_="form-control")}} | ||
</div> | ||
<div class="form-group"> | ||
{{render_field(form.email, class_="form-control")}} | ||
</div> | ||
<div class="form-group"> | ||
{{render_field(form.username, class_="form-control")}} | ||
</div> | ||
<div class="form-group"> | ||
{{render_field(form.password, class_="form-control")}} | ||
</div> | ||
<div class="form-group"> | ||
{{render_field(form.confirm, class_="form-control")}} | ||
</div> | ||
<p><input type="submit" class="btn btn-primary" value="Submit"></p> | ||
</form> | ||
{% endblock %} |