-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.py
210 lines (161 loc) · 4.96 KB
/
users.py
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import sqlite3
import time
import datetime
import random
import json
import re
import hashlib
import base64
import binascii
from flask import Flask,jsonify,request,make_response,Response,abort,Response
app = Flask(__name__)
"""
from flask_cors import CORS
cors=CORS(app)
"""
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error':'Not found'}),404)
@app.route('/api/v1/users',methods=['POST'])
def add_user():
conn10 = sqlite3.connect("users.db")
c10 = conn10.cursor()
c10.execute("select *from _count")
count=0
for row in c10.fetchall():
#print(row[0])
count=row[0]
count=count+1
print(count)
c10.execute('UPDATE _count set count=(?) ',(count,))
conn10.commit()
c10.close()
conn10.close()
if not request.json:
abort(400)
conn = sqlite3.connect("users.db")
c = conn.cursor()
data = request.json
try:
if(data["username"]):
print("")
except:
return make_response(jsonify("username"),400)
try:
if(data["password"]):
print("")
except:
return make_response(jsonify("password"),400)
row = (data['username'],data['password'])
c.execute("SELECT * FROM users")
for row1 in c.fetchall():
if(row1[0]==row[0]):
return make_response(jsonify("username"),400)
hexval=data["password"]
#print(type(hexval))
pattern = re.compile(r'\b[0-9a-f]{40}\b')
match = re.match(pattern,hexval)
#print(type(match))
time.sleep(4)
try:
if(match.group(0)==hexval):
c.execute('INSERT INTO users(username,password) VALUES (?,?)',row)
conn.commit()
c.close()
conn.close()
return jsonify({}),201
except:
return make_response(jsonify("password"),400)
@app.route('/api/v1/users/<string:username>',methods=['DELETE'])
def delete_user(username):
conn10 = sqlite3.connect("users.db")
c10 = conn10.cursor()
c10.execute("select *from _count")
count=0
for row in c10.fetchall():
#print(row[0])
count=row[0]
count=count+1
print(count)
c10.execute('UPDATE _count set count=(?) ',(count,))
conn10.commit()
c10.close()
conn10.close()
conn = sqlite3.connect("users.db")
c = conn.cursor()
c.execute("SELECT * FROM users ")
flag=0
try:
if(username):
print("akshay")
except:
return make_response(jsonify("username"),400)
for row in c.fetchall():
if(row[0]==username):
flag=1
if(flag==1):
c.execute('DELETE FROM users WHERE username=(?)',(username,))
conn.commit()
c.close()
conn.close()
return make_response(jsonify(),200)
if(flag==0):
return make_response(jsonify(),400)
@app.route('/api/v1/users',methods=['GET'])
def list_all_users():
conn10 = sqlite3.connect("users.db")
c10 = conn10.cursor()
c10.execute("select *from _count")
count=0
for row in c10.fetchall():
#print(row[0])
count=row[0]
count=count+1
print(count)
c10.execute('UPDATE _count set count=(?) ',(count,))
conn10.commit()
c10.close()
conn10.close()
conn = sqlite3.connect("users.db")
c = conn.cursor()
c.execute("SELECT * FROM users")
#data = c.fetchall()
#print(data)
#row = c.fetchall()
userslist=list()
for row in c.fetchall():
userslist.append(row[0])
#catlist={}
if(userslist==[]):
return make_response(jsonify("users"),204)
conn.commit()
c.close()
conn.close()
return Response(json.dumps(userslist), mimetype='application/json')
@app.route('/api/v1/_count',methods=['GET'])
def count_requests():
conn10 = sqlite3.connect("users.db")
c10 = conn10.cursor()
c10.execute("select *from _count")
count=0
for row in c10.fetchall():
count=row[0]
conn10.commit()
c10.close()
conn10.close()
l=list()
l.append(count)
return Response(json.dumps(l), mimetype='application/json')
@app.route('/api/v1/_count',methods=['DELETE'])
def resest_count_requests():
conn10 = sqlite3.connect("users.db")
c10 = conn10.cursor()
count=0
c10.execute('UPDATE _count set count=(?) ',(count,))
conn10.commit()
c10.close()
conn10.close()
l1=dict()
return Response(json.dumps(l1), mimetype='application/json')
if __name__ == '__main__':
app.run(debug=True,host="0.0.0.0",port=80)