2020-06-19 01:47:51 +00:00
|
|
|
import traceback
|
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
from flask import current_app
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from .base import db
|
|
|
|
|
|
|
|
|
|
|
|
class History(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2021-11-30 09:02:37 +00:00
|
|
|
# format of msg field must not change. History traversing is done using part of the msg field
|
2019-12-02 03:32:03 +00:00
|
|
|
msg = db.Column(db.String(256))
|
|
|
|
# detail = db.Column(db.Text().with_variant(db.Text(length=2**24-2), 'mysql'))
|
|
|
|
detail = db.Column(db.Text())
|
|
|
|
created_by = db.Column(db.String(128))
|
2020-10-11 10:49:28 +00:00
|
|
|
created_on = db.Column(db.DateTime, index=True, default=datetime.utcnow)
|
2021-03-27 18:33:11 +00:00
|
|
|
domain_id = db.Column(db.Integer,
|
|
|
|
db.ForeignKey('domain.id'),
|
|
|
|
nullable=True)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2021-03-27 18:33:11 +00:00
|
|
|
def __init__(self, id=None, msg=None, detail=None, created_by=None, domain_id=None):
|
2019-12-02 03:32:03 +00:00
|
|
|
self.id = id
|
|
|
|
self.msg = msg
|
|
|
|
self.detail = detail
|
|
|
|
self.created_by = created_by
|
2021-03-27 18:33:11 +00:00
|
|
|
self.domain_id = domain_id
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<History {0}>'.format(self.msg)
|
|
|
|
|
|
|
|
def add(self):
|
|
|
|
"""
|
|
|
|
Add an event to history table
|
|
|
|
"""
|
|
|
|
h = History()
|
|
|
|
h.msg = self.msg
|
|
|
|
h.detail = self.detail
|
|
|
|
h.created_by = self.created_by
|
2021-03-27 18:33:11 +00:00
|
|
|
h.domain_id = self.domain_id
|
2019-12-02 03:32:03 +00:00
|
|
|
db.session.add(h)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def remove_all(self):
|
|
|
|
"""
|
|
|
|
Remove all history from DB
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
db.session.query(History).delete()
|
|
|
|
db.session.commit()
|
|
|
|
current_app.logger.info("Removed all history")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
db.session.rollback()
|
|
|
|
current_app.logger.error("Cannot remove history. DETAIL: {0}".format(e))
|
|
|
|
current_app.logger.debug(traceback.format_exc())
|
|
|
|
return False
|