Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fd47de

Browse files
authored
Add files via upload
1 parent 3450151 commit 6fd47de

File tree

8 files changed

+190
-0
lines changed

8 files changed

+190
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
#app.secret_key = "secret key"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pymysql
2+
from db_config import mysql
3+
from werkzeug import generate_password_hash, check_password_hash
4+
5+
def user_exist(email):
6+
conn = None;
7+
cursor = None;
8+
9+
try:
10+
conn = mysql.connect()
11+
cursor = conn.cursor()
12+
13+
sql = "SELECT email FROM user WHERE email=%s"
14+
sql_where = (email,)
15+
16+
cursor.execute(sql, sql_where)
17+
row = cursor.fetchone()
18+
19+
if row:
20+
return True
21+
return False
22+
23+
except Exception as e:
24+
print(e)
25+
26+
finally:
27+
if cursor and conn:
28+
cursor.close()
29+
conn.close()
30+
31+
def register(email, name, pwd):
32+
conn = None;
33+
cursor = None;
34+
35+
try:
36+
conn = mysql.connect()
37+
cursor = conn.cursor()
38+
39+
sql = "INSERT INTO user(name, email, pwd) VALUES(%s, %s, %s)"
40+
data = (name, email, generate_password_hash(pwd),)
41+
42+
cursor.execute(sql, data)
43+
44+
conn.commit()
45+
46+
except Exception as e:
47+
print(e)
48+
49+
finally:
50+
if cursor and conn:
51+
cursor.close()
52+
conn.close()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from app import app
2+
from flaskext.mysql import MySQL
3+
4+
mysql = MySQL()
5+
6+
# MySQL configurations
7+
app.config['MYSQL_DATABASE_USER'] = 'root'
8+
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
9+
app.config['MYSQL_DATABASE_DB'] = 'roytuts'
10+
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
11+
mysql.init_app(app)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import rest
2+
from app import app
3+
from flask import render_template
4+
5+
@app.route('/')
6+
def home():
7+
return render_template('signup.html')
8+
9+
if __name__ == "__main__":
10+
app.run()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can read tutorial https://www.roytuts.com/jquery-ajax-based-registration-system-using-python-flask-mysql/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import dao
2+
import json
3+
from app import app
4+
from flask import jsonify, request
5+
6+
@app.route('/signup', methods=['POST'])
7+
def signup():
8+
_json = request.json
9+
_name = _json['name']
10+
_email = _json['email']
11+
_pwd = _json['password']
12+
13+
if _email and _name and _pwd:
14+
15+
user_exist = dao.user_exist(_email)
16+
17+
if user_exist == True:
18+
resp = jsonify({'message' : 'User already registered'})
19+
resp.status_code = 409
20+
return resp
21+
else:
22+
dao.register(_email, _name, _pwd)
23+
24+
resp = jsonify({'message' : 'User registered successfully'})
25+
resp.status_code = 201
26+
return resp
27+
else:
28+
resp = jsonify({'message' : 'Bad Request - invalid parameters'})
29+
resp.status_code = 400
30+
return resp
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
$(document).ready(function() {
2+
$('#signupSubmit').on('click', function(e) {
3+
e.preventDefault();
4+
5+
var name = $('#fullname').val();
6+
var email = $('#email').val();
7+
var pwd = $('#password').val();
8+
var cnfpwd = $('#cnfpassword').val();
9+
10+
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/i;
11+
12+
if(email != "" && pwd != "" && cnfpwd != "") {
13+
if(pwd != cnfpwd) {
14+
$('#msg').html('<span style="color: red;">Password and confirm password must match</span>');
15+
} else if(!regex.test(email)) {
16+
$('#msg').html('<span style="color: red;">Invalid email address</span>');
17+
} else {
18+
$.ajax({
19+
method: "POST",
20+
url: '/signup',
21+
contentType: 'application/json;charset=UTF-8',
22+
data: JSON.stringify({'name': name, 'email': email, 'password': pwd}),
23+
dataType: "json",
24+
success: function(data) {
25+
$('#signupform').hide();
26+
$('#msg').html('<span style="color: green;">You are registered successfully</span>');
27+
},statusCode: {
28+
400: function() {
29+
$('#msg').html('<span style="color: red;">Bad request parameters</span>');
30+
},
31+
409 : function() {
32+
$('#msg').html('<span style="color: red;">You are already registered user</span>');
33+
}
34+
},
35+
error: function(err) {
36+
console.log(err);
37+
}
38+
});
39+
}
40+
} else {
41+
$('#msg').html('<span style="color: red;">All fields are required</span>');
42+
}
43+
});
44+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5+
<title>jQuery AJAX based Registration System using Python Flask MySQL</title>
6+
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
7+
<script type="text/javascript" src="{{ url_for('static', filename='js/register.js') }}"/>
8+
<script type="text/javascript">
9+
$SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
10+
</script>
11+
</head>
12+
<body>
13+
<div style="width: 500px; margin: auto;">
14+
<div id="msg"></div>
15+
<div style="clear: both;"></div>
16+
<div id="signupform" style="width: 500px; margin: auto;">
17+
<h2>Sign Up</h2>
18+
<dl>
19+
<p>
20+
<input id="fullname" value="" type="text" placeholder="Full Name" required>
21+
</p>
22+
<p>
23+
<input id="email" value="" type="text" placeholder="Email" required>
24+
</p>
25+
<p>
26+
<input id="password" value="" type="password" placeholder="Password" autocomplete="off" required>
27+
</p>
28+
<p>
29+
<input id="cnfpassword" value="" type="password" placeholder="Confirm Password" autocomplete="off" required>
30+
</p>
31+
</dl>
32+
<p>
33+
<input type="button" id="signupSubmit" value="Submit">
34+
</p>
35+
</div>
36+
</div>
37+
</body>
38+
</html>

0 commit comments

Comments
 (0)