-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
61 lines (43 loc) · 1.3 KB
/
models.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
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class BaseDbModel:
def insert(self):
try:
db.session.add(self)
db.session.commit()
except Exception as e:
db.session.rollback()
raise e
def update(self):
try:
db.session.commit()
except Exception as e:
db.session.rollback()
raise e
def delete(self):
try:
db.session.delete(self)
db.session.commit()
except Exception as e:
db.session.rollback()
raise e
class Document(BaseDbModel, db.Model):
__tablename__ = 'Document'
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.String)
added_at = db.Column(db.DateTime, default=datetime.utcnow)
def __init__(self, body):
self.body = body
def format(self):
return {
'id': self.id,
'body': self.body,
'added_at': self.added_at.strftime('%Y-%m-%d %H:%M:%S')
}
class Index(BaseDbModel, db.Model):
__tablename__ = 'Index'
id = db.Column(db.Integer, primary_key=True)
index = db.Column(db.String)
def __init__(self, index):
self.index = index