def build_db(): ''' delete all, then rebuild db ''' db.drop_all() db.create_all() return 'build database success.'
class User(db.Model, BaseMethod): '''用户表''' id = db.Column(db.String(64), primary_key=True) uid = db.Column(db.String(32)) name = db.Column(db.String(32)) source = db.Column(db.String(8)) # github / qq last_login = db.Column(db.DateTime)
def run(self, argv): dir = os.path.join(os.path.expanduser('~'), '.redis-monitor') if not os.path.exists(dir): os.makedirs(dir) if os.path.isdir(dir): path = os.path.join(dir, 'redis_monitor_config.py') if os.path.exists(path): print('Fail: the configuration file exist in `%s`.' % path) else: sqlite_file = os.path.join(dir, 'redis_monitor.db') \ .replace('\\', '/') with open(path, 'w') as f: f.write(CONFIG_TEMP % sqlite_file) print('OK: init configuration file into `%s`.' % path) db.create_all() print('OK: database is initialed into `%s`.' % sqlite_file) else: print('Fail: %s should be directory.' % dir)
class Todo(db.Model, BaseMethod): '''用户表''' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(1024)) add_time = db.Column(db.DateTime) completed = db.Column(db.Boolean) user_id = db.Column(db.String(64), db.ForeignKey('user.id')) def dict(self): rst = {} rst['id'] = self.id rst['title'] = self.title rst['add_time'] = self.add_time rst['completed'] = self.completed rst['user_id'] = self.user_id return rst
class RedisInfo(db.Model, BaseMethod): '''RedisInfo''' md5 = db.Column(db.String(32), primary_key=True) host = db.Column(db.String(32)) port = db.Column(db.Integer, default=6379) password = db.Column(db.String(32)) add_time = db.Column(db.DateTime, default=datetime.datetime.now) def dict(self): rst = {} rst['md5'] = self.md5 rst['host'] = self.host rst['port'] = self.port rst['password'] = self.password rst['add_time'] = self.add_time return rst
class Collaborator(db.Model, BaseMethod): '''Collaborator''' id = db.Column(db.Integer, primary_key=True) # webhook webhook_id = db.Column(db.Integer, db.ForeignKey(WebHook.id)) webhook = db.relationship(WebHook) # user user_id = db.Column(db.String(32), db.ForeignKey(User.id)) user = db.relationship(User) add_time = db.Column(db.DateTime, default=datetime.datetime.now) def dict(self): rst = {} rst['id'] = self.id rst['webhook_id'] = self.webhook_id rst['user_id'] = self.user_id rst['user'] = {} if self.user: rst['user'] = self.user.dict() rst['add_time'] = self.add_time return rst
def drop_db(): db.drop_all()
def rebuild_db(): db.create_all()
class WebHook(db.Model, BaseMethod): '''webhook''' id = db.Column(db.Integer, primary_key=True) repo = db.Column(db.String(32)) # repo name branch = db.Column(db.String(32)) # repo branch shell = db.Column(db.Text) # do what user_id = db.Column(db.String(32), db.ForeignKey(User.id)) user = db.relationship(User) server_id = db.Column(db.Integer, db.ForeignKey(Server.id)) server = db.relationship(Server) add_time = db.Column(db.DateTime, default=datetime.datetime.now) key = db.Column(db.String(32), unique=True) # 用于webhook,保证私密,直接用 md5 salt status = db.Column( db.String(1)) # 1:waiting, 2:ing, 3:error, 4:success, 5:except, other lastUpdate = db.Column(db.DateTime, default=datetime.datetime.now) # 最新执行时间 def dict(self, with_key=False): rst = {} rst['id'] = self.id rst['repo'] = self.repo rst['branch'] = self.branch rst['shell'] = self.shell rst['user_id'] = self.user_id rst['server_id'] = self.server_id rst['server'] = self.server and self.server.dict() or {} rst['add_time'] = self.add_time rst['key'] = with_key and self.key or '' rst['status'] = self.status rst['lastUpdate'] = self.lastUpdate return rst def updateStatus(self, status): self.status = status self.lastUpdate = datetime.datetime.now() self.save()
class Server(db.Model, BaseMethod): '''server list''' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32)) ip = db.Column(db.String(16)) port = db.Column(db.Integer) account = db.Column(db.String(32)) pkey = db.Column(db.Text) user_id = db.Column(db.String(32), db.ForeignKey(User.id)) user = db.relationship(User) add_time = db.Column(db.DateTime, default=datetime.datetime.now) deleted = db.Column(db.Boolean, default=False) def dict(self, with_pkey=False): rst = {} rst['id'] = self.id rst['name'] = self.name rst['ip'] = self.ip rst['port'] = self.port rst['account'] = self.account rst['pkey'] = with_pkey and self.pkey or '' rst['user_id'] = self.user_id rst['add_time'] = self.add_time return rst
class User(db.Model, BaseMethod): '''user''' id = db.Column(db.String(32), primary_key=True) name = db.Column(db.String(32)) location = db.Column(db.String(32)) avatar = db.Column(db.String(128)) src = db.Column(db.String(4), default="gh") last_login = db.Column(db.DateTime, default=datetime.datetime.now) def dict(self): rst = {} rst['id'] = self.id rst['name'] = self.name rst['location'] = self.location rst['avatar'] = self.avatar rst['src'] = self.src rst['last_login'] = self.last_login return rst
class User(db.Model, BaseMethod): '''user''' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(32), nullable=True) location = db.Column(db.String(32), nullable=True) avatar = db.Column(db.String(128), nullable=True) username = db.Column(db.String(32), index=True) password_hash = db.Column(db.String(128)) email = db.Column(db.String(128), nullable=True) src = db.Column(db.String(4), default="gh", nullable=True) # useless last_login = db.Column(db.DateTime, default=datetime.datetime.now) def dict(self): rst = {} rst['id'] = self.id rst['name'] = self.name rst['location'] = self.location rst['avatar'] = self.avatar rst['src'] = self.src rst['last_login'] = self.last_login rst['email'] = self.email rst['username'] = self.username return rst def hash_password(self, password): self.password_hash = pwd_context.encrypt(password) def verify_password(self, password): return pwd_context.verify(password, self.password_hash)
def rebuild_db(): db.create_all() return 'rebuild database success.'
class FlightInfo(db.Model): id = db.Column(db.Integer, primary_key=True) uid = db.Column(db.String(80)) start_location = db.Column(db.String(120)) end_location = db.Column(db.String(120)) start_date = db.Column(db.String(120)) return_date = db.Column(db.String(120)) flight_type = db.Column(db.INTEGER) status = db.Column(db.INTEGER) complete = db.Column(db.Boolean) update_time = db.Column(db.DATETIME, default=datetime.datetime.utcnow()) nlp = const.NLP def __init__(self,uid,start_location=None,end_location=None,flight_type=None,status=0): # 状态:0 :初始状态内容为空 1:缺少起始城市 2:缺少到达城市 3 缺少类型(往返或者单程) # 4:缺少出发时间 5缺少返回时间 6:完成 self.uid = uid self.start_location = start_location self.end_location = end_location self.start_date = None self.return_date = None #航班类型1:单程 2:往返 self.flight_type = flight_type self.status = status self.complete = False def save(self): db.session.add(self) db.session.commit() def get_qstr(self): q = QuestionStr() if self.status == 4: return ( u"您从" + self.start_location + u"到" + self.end_location +u"的行程" + q.get_question_str(self.status)) if self.status == 5: return (u"您从" + self.end_location + u"返回" + self.start_location + u"的行程"+ q.get_question_str(self.status)) if self.status == 6: return q.get_question_str(self.status) + self.start_location + u'到' + self.end_location + u'的机票' else: return q.get_question_str(self.status) def analyse(self,text): text = DateTransfer.date_transfer(text) self.__get_flight_info(text) self.__change_status() self.save() def get_dict(self): rst={} rst['origin'] = CitySearch.get_city_code(self.start_location) rst['destination'] = CitySearch.get_city_code(self.end_location) rst['departure_date'] = self.start_date if self.return_date is None: return rst else: rst['return_date']=self.return_date def __get_flight_type(self,text): result = self.nlp.tag(text)[0]['word'] for d in result: if d == u"单程": self.flight_type = 1 break if d == u"往返": self.flight_type = 2 break def __get_flight_info(self, text): times = [] locations = [] result = self.nlp.ner(text)[0] words = result['word'] entities = result['entity'] for entity in entities: if entity[2] == "time": times.append(words[entity[0]:entity[1]]) if entity[2] == "location": locations.append(words[entity[0]:entity[1]]) self.__get_flight_type(text) if len(times) == 2: self.start_date = self.__date_trans(times[0]) self.return_date = self.__date_trans(times[1]) if self.return_date is None and self.start_date is not None and len(times) == 1: self.return_date = self.__date_trans(times[0]) if self.start_date is None and len(times) == 1: self.start_date = self.__date_trans(times[0]) if len(locations) == 2: self.start_location = "%s" % locations[0][0] self.end_location = "%s" % locations[1][0] return if len(locations) == 1 and self.start_location is None: self.start_location = "%s" % locations[0][0] return if len(locations) == 1 and self.start_location is not None: self.end_location = "%s" % locations[0][0] return def __date_trans(self,date): ret = u"" for d in date: ret += d return ret def __change_status(self): if self.start_location is None: self.status = 1 return if self.end_location is None: self.status = 2 return if self.flight_type is None: self.status = 3 return if self.start_date is None: self.status = 4 return if self.flight_type == 2 and self.return_date is None: self.status = 5 return self.status = 6 return def change_complete(self): self.complete = True self.save()
def build_db(): # database init db.drop_all() db.create_all() return 'build database success.'
def app(): SQLAlchemyDB.create_all() yield _app SQLAlchemyDB.session.close() SQLAlchemyDB.drop_all()
class History(db.Model, BaseMethod): '''push history''' # md5, notice, output, push_name, push_email, success, add_time id = db.Column(db.Integer, primary_key=True) status = db.Column( db.String(1)) # 1:waiting, 2:ing, 3:error, 4:success, 5:except, other shell_log = db.Column(db.Text) # hook shell log data = db.Column(db.Text) # git push data push_user = db.Column(db.String(64)) # git push user(name<email>) add_time = db.Column(db.DateTime, default=datetime.datetime.now) # git push time webhook_id = db.Column(db.Integer, db.ForeignKey(WebHook.id)) webhook = db.relationship(WebHook) def dict(self): rst = {} rst['id'] = self.id rst['status'] = self.status rst['shell_log'] = self.shell_log rst['data'] = self.data # json rst['push_user'] = self.push_user rst['add_time'] = self.add_time rst['webhook_id'] = self.webhook_id return rst def updateStatus(self, status): self.status = status self.save()
def createdb(drop_first=False): """Creates the database.""" if drop_first: db.drop_all() db.create_all() print('OK: database is initialed.')
class User(db.Model, BaseMethod): id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(250), nullable=True, unique=True) password_hash = db.Column(db.String(128), nullable=False) name = db.Column(db.String(250), nullable=True) username = db.Column(db.String(32), index=True, nullable=False) active = db.Column(db.Integer, nullable=True, default=0) avatar = db.Column(db.String(128), nullable=True) handle_id = db.Column(db.Integer, nullable=True) admin = db.Column(db.Boolean, nullable=True) creation_time = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp(), nullable=False) last_login = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp(), nullable=False) def get_hash(self, pwd): return pwd_context.encrypt(pwd) def to_dict(self): rst = {} rst['id'] = self.id rst['name'] = self.name rst['avatar'] = self.avatar rst['active'] = self.active rst['last_login'] = self.last_login rst['email'] = self.email rst['username'] = self.username rst['handle_id'] = self.handle_id rst['admin'] = self.admin return rst