-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
195 lines (143 loc) · 6.31 KB
/
app.py
File metadata and controls
195 lines (143 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
from flask import Flask, render_template, json, request, Response, url_for
from flaskext.mysql import MySQL
from werkzeug.security import generate_password_hash, check_password_hash
from contextlib import closing
import pymysql
mysql = MySQL()
app = Flask(__name__, static_url_path='/static')
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'motorola_motox4'
app.config['MYSQL_DATABASE_DB'] = 'BucketList'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
def connection():
# Edited out actual values
conn = mysql.connect(host="localhost",
user="root",
passwd="motorola_motox4",
db = "BucketList")
cursor = conn.cursor()
return cursor, conn
@app.route('/')
def main():
return render_template('index.html')
@app.route('/showSignUp')
def showSignUp():
return render_template('signup.html')
@app.route('/signUp',methods=['POST','GET'])
def signUp():
try:
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']
# validate the received values
if _name and _email and _password:
# All Good, let's call MySQL
conn = mysql.connect()
cursor = conn.cursor()
#cursor = mysql.connection.cursor()
# added - JMM - 2019/02/07
#with closing(mysql.connect()) as conn:
# with closing(conn.cursor()) as cursor:
query = "select * from BucketList.tbl_user where user_email = %s"
cursor.execute(query, (_email))
data = cursor.fetchall()
try:
conn1 = mysql.connect()
cursor1 = conn1.cursor()
# cursor1 = mysql.connection.cursor()
# with closing(mysql.connect()) as conn1:
# with closing(conn.cursor()) as cursor1:
_hashed_password = generate_password_hash(_password)
query2 = "insert into BucketList.tbl_user (user_name,user_email,user_password) values (%s,%s,%s)"
cursor1.execute(query2, (_name, _email, _hashed_password))
if len(data) is 0:
conn1.commit()
return json.dumps({'message':'User created successfully !'})
return json.dumps({'html': '<h4>User Created: </h4></br></br>'})
return json.dumps({'html': '<p>'+str(_name)+'</p>'})
else:
return json.dumps({'data error':str(data[0]),
'message': 'An account associated with this email address already exists.'})
return json.dumps({'html': '<span>An account associated with this email address already exists.</span>'})
#origin - NO SIRVE
#_hashed_password = generate_password_hash(_password)
#cursor.callproc('sp_createUser',(_name,_email,_hashed_password))
#data = cursor.fetchall()
except Exception as e:
return json.dumps({'error exception': str(e)})
finally:
cursor1.close()
conn1.close()
#return render_template("consulta.html", data=data)
else:
return json.dumps({'html':'<span>Enter the required fields</span>'})
return json.dumps({'message': 'Enter the required fields: '})
return json.dumps({'message': str(_name)})
return json.dumps({'message': str(_email)})
return json.dumps({'message': str(_password)})
print('Enter the required fields:', str(_name), ", ", str(_email), ', ', str(_password))
except Exception as e:
return json.dumps({'error exception':str(e)})
finally:
cursor.close()
conn.close()
def conexion():
host = "127.0.0.1"
user = "root"
password = "motorola_motox4"
db = "BucketList"
con = pymysql.connect(host=host, user=user, password=password, db=db, cursorclass=pymysql.cursors.DictCursor)
cur = con.cursor()
return cur
def list_users():
cursor = conexion()
cursor.execute("select user_id, user_name, user_email from BucketList.tbl_user")
result = cursor.fetchall()
return result
@app.route('/consulta', methods=['POST', 'GET'])
def consulta():
def db_query():
# db = __init__(self=self)
users = list_users()
return users
res = db_query()
# return json.dumps({'data query': str(res[0])})
return render_template('consulta.html', result=res, content_type='application/json')
if __name__ == "__main__":
app.run(debug=True)
'''
@app.route('/consulta', methods=['POST', 'GET'])
def result():
try:
if request.method == "POST":
#cursor, conn = connection()
#conn = mysql.connect()
#cursor = conn.cursor()
# cursor = mysql.connection.cursor()
# added - JMM - 2019/02/07
with closing(mysql.connect()) as conn:
with closing(conn.cursor()) as cursor:
query = "select * from BucketList.tbl_user"
cursor.execute(query)
rows = cursor.fetchall()
row_headers = [x[0] for x in cursor.description] # this will extract row
# headers
json_data = []
for result in rows:
json_data.append(dict(zip(row_headers, result)))
consultajs = json.dumps(json_data)
resp = Response(consultajs, status=200, mimetype='application/json')
#resp.headers['Link'] = 'consulta.html'
return json.dumps({'sql data: ': str(rows[0])})
#return render_template("consulta.html", str(rows[0])=str(rows[0]))
#return render_template("consulta.html", records=cursor.fetchall())
#for data in cursor.fetchall():
#return data, {'Content-Type': 'text/html'}
# return redirect(url_for('consulta'))
#return render_template("consulta.html")
except Exception as e:
return json.dumps({'error exception': str(e)})
'''