-
Notifications
You must be signed in to change notification settings - Fork 17
/
dbModel.py
68 lines (54 loc) · 1.85 KB
/
dbModel.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
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
# app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:xxxxx@localhost/db'
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
class UserAccounts(db.Model):
__tablename__ = 'UserAccounts'
Id = db.Column(db.Integer, primary_key=True)
UserName = db.Column(db.String(64), unique=True)
Password = db.Column(db.String(64))
MugShot = db.Column(db.String(64))
CreateDate = db.Column(db.DateTime)
ModifiedDate = db.Column(db.DateTime)
def __init__(self,
user_name,
password,
mugshot,
create_date=datetime.now(),
modified_date=datetime.now()):
self.UserName = user_name
self.Password = self.psw_to_md5(password)
self.MugShot = mugshot
self.CreateDate = create_date
self.ModifiedDate = modified_date
@staticmethod
def psw_to_md5(str_psw):
import hashlib
if not str_psw:
return None
else:
m = hashlib.md5(str_psw.encode(encoding='utf-8'))
return m.hexdigest()
class Message(db.Model):
__tablename__ = 'Message'
Id = db.Column(db.Integer, primary_key=True)
UserName = db.Column(db.String(64))
Messages = db.Column(db.Text)
CreateDate = db.Column(db.DateTime)
def __init__(self,
user_name,
messages,
create_date):
self.UserName = user_name
self.Messages = messages
self.CreateDate = create_date
if __name__ == '__main__':
manager.run()