Repository navigation
Expand file tree
/
Copy pathModel.py
More file actions
121 lines (103 loc) · 3.42 KB
/
Copy pathModel.py
File metadata and controls
121 lines (103 loc) · 3.42 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pony.orm import *
import Config
import hashlib
import uuid
import time
db = Database()
if Config.get('develop') == True:
db.bind('sqlite', 'test.sqlite', create_db=True)
else:
db.bind('mysql', host=Config.get('host'), user=Config.get('user'), passwd=Config.get('passwd'), db=Config.get('db'))
if Config.get('debug') == True:
sql_debug(True)
class User(db.Entity):
password = Optional(str)
student_id = PrimaryKey(str)
email = Optional(str, unique=True)
phone = Optional(str)
nickname = Optional(str)
ykt = Optional(str)
reg_time = Required(str)
avatar = Required(str, default='default.jpg')
tokens = Set(lambda: Token)
def before_insert(self):
self.password = self._salt(self.student_id, self.password)
def before_update(self):
self.password = self._salt(self.student_id, self.password)
@classmethod
def getBySid(cls, student_id):
return User.get(student_id=student_id)
@classmethod
def login(cls, username, password):
if not username:
return False
u = User.get(student_id=username)
if not u or u.password=='':
s = StudentInfo.get(StudentID=username)
if not s:
return False
if s.IDCard[-6:]==password:
u = User(student_id=username, reg_time = str(int(time.time())))
u.password = password
u._setToken()
return u
else:
return False
elif u.password==User._salt(username, password):
u._setToken()
return u
else:
return False
@classmethod
def register(cls, username, password):
return User(
student_id=username,
password=cls._salt(username, password),
reg_time=str(int(time.time()))
)
@classmethod
def is_exist(cls, username):
return User.get(student_id=username)
@classmethod
def _salt(cls, username, password):
return hashlib.sha256(str(username) + \
str(hashlib.sha256(str(password) + \
str(Config.get('salt'))).hexdigest())).hexdigest()
@classmethod
def _makeToken(self, username):
return str(uuid.uuid3(uuid.uuid1(),'uctoken').hex)
def _setToken(self):
self.token = self.tokens.create(token=User._makeToken(self.student_id), last_use_time=str(int(time.time())))
class Token(db.Entity):
user = Required(User)
token = Required(str)
last_use_time = Required(str)
def is_expired(self):
if int(time.time()) - int(self.last_use_time) < Config.get('token_expire'):
self.last_use_time = str(int(time.time()))
return False
else:
return True
class StudentInfo(db.Entity):
StudentID = PrimaryKey(str)
ClassCode = Optional(str)
Name = Optional(str)
Sex = Optional(str)
Birth = Optional(str)
Nationality = Optional(str)
Political = Optional(str)
EduType = Optional(str)
Classify = Optional(str)
Source = Optional(str)
Native = Optional(str)
Class = Optional(str)
EducationLen = Optional(str)
DiplomaNo = Optional(str)
DgreeNo = Optional(str)
StudyFlag = Optional(str)
PunishFlag = Optional(str)
IDCard = Optional(str)
KSH = Optional(str)
db.generate_mapping(check_tables=True, create_tables=True)