/
register_process.py
57 lines (42 loc) · 1.39 KB
/
register_process.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
# !/usr/bin/python3
# -*- coding: utf-8 -*-
# __author__ = "Jeako_Wu"
import utility
import sql
import pymysql
from passlib.hash import sha256_crypt
def register_process(user, identity):
# user password 加密
user.password = sha256_crypt.hash(user.password)
try:
# 打开数据库连接
db = pymysql.connect("localhost", "root", "wujiahao.", "flaskTest")
# 使用cursor()方法获取操作游标
cursor = db.cursor()
key = ["id"]
condition = {"username": user.username}
query = sql.select(identity,key,condition,0)
if cursor.execute(query):
return "该用户名已经存在", False
key = ["id"]
condition = {"number": user.number}
query = sql.select(identity, key, condition, 0)
if cursor.execute(query):
return "该手机号已经存在", False
# 生成SQL语句
query = sql.insert(identity, utility.class_2_dict(user))
try:
# 使用execute方法执行SQL语句
cursor.execute(query)
# 提交操作到db
db.commit()
except:
# 操作失败回滚
db.rollback()
db.close()
return "注册失败", False
# 关闭数据库连接
db.close()
return "注册成功", True
except:
return "Unable to connect to DB", False