-
Notifications
You must be signed in to change notification settings - Fork 81
/
users_mgt.py
52 lines (33 loc) · 1.15 KB
/
users_mgt.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
from sqlalchemy import Table
from sqlalchemy.sql import select
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash
from config import engine
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(15), unique=True)
email = db.Column(db.String(50), unique=True)
password = db.Column(db.String(80))
User_tbl = Table('user', User.metadata)
def create_user_table():
User.metadata.create_all(engine)
def add_user(username, password, email):
hashed_password = generate_password_hash(password, method='sha256')
ins = User_tbl.insert().values(
username=username, email=email, password=hashed_password)
conn = engine.connect()
conn.execute(ins)
conn.close()
def del_user(username):
delete = User_tbl.delete().where(User_tbl.c.username == username)
conn = engine.connect()
conn.execute(delete)
conn.close()
def show_users():
select_st = select([User_tbl.c.username, User_tbl.c.email])
conn = engine.connect()
rs = conn.execute(select_st)
for row in rs:
print(row)
conn.close()