-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
58 lines (49 loc) · 1.77 KB
/
user.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
import sqlite3
from flask_restful import Resource, reqparse
class User:
def __init__(self,id,username,password):
self.id= id
self.username=username
self.password=password
@classmethod
def find_by_username(cls, username):
connection = sqlite3.connect('data.db')
cursor = connection.cursor()
query = "SELECT * from users WHERE username=?"
result = cursor.execute(query,(username,))
row = cursor.fetchone()
if row:
user = cls(*row)
else:
user = None
connection.close()
return user
@classmethod
def find_by_id(cls,id):
connection = sqlite3.connect('data.db')
cursor = connection.cursor()
query = "SELECT * from users WHERE id=?"
result = cursor.execute(query,(id,))
row = cursor.fetchone()
if row:
user = cls(*row)
else:
user =None
connection.close()
return user
class UserRegister(Resource):
parser = reqparse.RequestParser()
parser.add_argument('username', type=str, required=True, help="this filed can not pe empty")
parser.add_argument('password', type=str, required=True, help="this filed can not pe empty")
def post(self):
data = UserRegister.parser.parse_args()
if(User.find_by_username(data['username'])):
return {"meassage": "user exist"},201
else:
connection = sqlite3.connect('data.db')
cursor = connection.cursor()
insert_table = "INSERT INTO users VALUES (NULL,?,?)"
cursor.execute(insert_table,(data['username'], data['password']))
connection.commit()
connection.close()
return {"message":"User Created successfully."}, 201