def add_statistics(self, s_type, name, value=None, user_id=0, project_id=0, status=1): if value is None: value = dict() sql = """INSERT INTO t_statistics (`userId`, `projectId`, `type`, `name`, `value`, `status`) VALUE (%(userId)s, %(projectId)s, %(type)s, %(name)s, %(value)s, %(status)s) """ with (yield pool.Connection()) as conn: with conn.cursor() as cursor: try: yield cursor.execute( sql, dict(userId=user_id, projectId=project_id, name=name, status=status, type=s_type, value=json.dumps(value, ensure_ascii=False))) except pymysql.Error as e: yield conn.rollback() log.error('新增统计信息失败#{}'.format(e)) flag, msg = False, '新增统计信息失败#{}'.format(e) else: yield conn.commit() log.info('新增统计信息成功') flag, msg = cursor.lastrowid, '新增统计信息成功' return flag, msg
def add_setting(self, pid, s_type, name, value, status=1, sort=1): sql = """INSERT INTO t_settings (`project`, `type`, `name`, `value`, `status`, `sort`) VALUE (%(pid)s, %(s_type)s, %(name)s, %(value)s, %(status)s, %(sort)s)""" with (yield pool.Connection()) as conn: with conn.cursor() as cursor: try: yield cursor.execute( sql, dict(pid=pid, s_type=s_type, name=name, value=value, status=status, sort=sort)) except pymysql.Error as e: yield conn.rollback() log.error('新增 {} 设置项 {} 失败#{}'.format(s_type, name, e)) flag, msg = False, '新增 {} 设置项 {} 失败#{}'.format( s_type, name, e) else: yield conn.commit() log.info('新增 {} 设置项 {} 成功'.format(s_type, name)) flag, msg = cursor.lastrowid, '新增 {} 设置项 {} 成功'.format( s_type, name) return flag, msg
def add_project(self, name, p_type, team_id=None, config=None, status=None, create_time=None): pj = yield self.get_project(name=name, p_type=p_type) if not pj: sql = "INSERT INTO t_projects (`name`, `type`, `teamId`, `config`, `status`, `createTime`) VALUE (%(name)s, %(type)s, %(teamId)s, %(config)s, %(status)s, %(createTime)s)" with (yield pool.Connection()) as conn: with conn.cursor() as cursor: try: yield cursor.execute( sql, dict(name=name, type=p_type, status=status or 1, teamId=team_id or 0, config=json.dumps(config, ensure_ascii=False) if config else dict(), createTime=create_time or time.strftime('%Y-%m-%d %H:%M:%S'))) except pymysql.Error as e: yield conn.rollback() log.error('添加项目 {} 失败#{}'.format(name, e)) flag, msg = False, '添加项目失败#{}'.format(e) else: yield conn.commit() log.info('添加项目 {} 成功'.format(name)) flag, msg = cursor.lastrowid, '添加项目成功' return flag, msg else: log.warning('项目 {} 已存在'.format(name)) return False, '项目 {} 已存在'.format(name)
def add_option(self, o_type, name, value): if o_type in ['teams', 'cate']: options = yield self.get_options_list(o_type=o_type, name=name) option = [] for op in options: if json.loads(op.value) == value: option.append(op) break else: option = yield self.get_option(o_type=o_type, name=name) if not option: sql = "INSERT INTO t_options (`type`, `name`, `value`) VALUE (%(type)s, %(name)s, %(value)s)" with (yield pool.Connection()) as conn: with conn.cursor() as cursor: try: yield cursor.execute( sql, dict(type=o_type, name=name, value=json.dumps(value, ensure_ascii=False) if isinstance(value, dict) else value)) except pymysql.Error as e: yield conn.rollback() log.error('添加系统配置失败#{}'.format(e)) flag, msg = False, '添加系统配置失败#{}'.format(e) else: yield conn.commit() log.info('添加系统配置 {} 成功'.format(value)) flag, msg = cursor.lastrowid, '添加系统配置 {} 成功'.format( value) else: log.error('配置 {} 已存在'.format(value)) flag, msg = False, '配置 {} 已存在'.format(value) return flag, msg
def register_user(self, email, password, username=None, real_name=None, profile=None, role=1, status=1): register_time = time.strftime('%Y-%m-%d %H:%M:%S') password = self.common_func.encode_password(password) try: cursor = yield pool.execute('SELECT COUNT(*) count FROM t_users') total = munchify(cursor.fetchone()) if total.count == 0: role = 0 status = 2 except pymysql.Error as e: log.error(e) username = username or '{}_{}'.format( email.split('@')[0], str(int(time.time() * 1000))) sql = """ INSERT INTO t_users (username, email, password, realname, profile, registerTime, lastLoginTime, role, status) VALUE(%(username)s, %(email)s, %(password)s, %(realname)s, %(profile)s, %(registerTime)s, %(lastLoginTime)s, %(role)s, %(status)s) """ user = yield self.get_user_info(email_or_username=email) if not user: with (yield pool.Connection()) as conn: with conn.cursor() as cursor: try: yield cursor.execute( sql, dict( username=username, email=email, password=password, realname=real_name or '', registerTime=register_time, lastLoginTime=register_time, profile=json.dumps(profile, ensure_ascii=False) or '', role=role, status=status)) except pymysql.Error as e: yield conn.rollback() log.error('注册用户 {} 失败#{}'.format(email, e)) flag, msg = False, '注册用户 {} 失败#{}'.format(email, e) else: yield conn.commit() log.info('注册用户 {} 成功'.format(email)) flag, msg = munchify( dict(id=cursor.lastrowid, status=status, role=role)), '注册用户成功!' else: log.error('该邮箱已注册!') flag, msg = False, '该邮箱已注册!' return flag, msg
def add_setting(self, s_type, name, value, pid=None, project=None, status=1, sort=1, create_time=None): if project is not None: pid = "SELECT p.id FROM t_projects p WHERE p.`name`='{}' and p.type='project'".format( project) if create_time is not None: time_param = ', `createTime`' time_value = ', %(createTime)s' else: time_param = '' time_value = '' sql = """INSERT INTO t_settings (`projectId`, `type`, `name`, `value`, `status`, `sort`{}) VALUE (({}), %(s_type)s, %(name)s, %(value)s, %(status)s, %(sort)s{})""".format( time_param, pid, time_value) if pid: with (yield pool.Connection()) as conn: with conn.cursor() as cursor: try: yield cursor.execute( sql, dict(s_type=s_type, name=name, status=status, createTime=create_time, value=json.dumps(value, ensure_ascii=False), sort=sort)) except pymysql.Error as e: yield conn.rollback() log.error('新增 {} 设置项 {} 失败#{}'.format(s_type, name, e)) flag, msg = False, '新增 {} 设置项 {} 失败#{}'.format( s_type, name, e) else: yield conn.commit() log.info('新增 {} 设置项 {} 成功'.format(s_type, name)) flag, msg = cursor.lastrowid, '新增 {} 设置项 {} 成功'.format( s_type, name) else: flag = False msg = '新增 {} 设置项 {} 失败#pid不能为空'.format(s_type, name) return flag, msg
def add_project(self, name): pj = yield self.get_project(name=name) if not pj: sql = "INSERT INTO t_projects (`name`) VALUE (%(name)s)" with (yield pool.Connection()) as conn: with conn.cursor() as cursor: try: yield cursor.execute(sql, dict(name=name)) except pymysql.Error as e: yield conn.rollback() log.error('添加项目失败#{}'.format(e)) flag, msg = False, '添加项目失败#{}'.format(e) else: yield conn.commit() log.info('添加项目成功') flag, msg = cursor.lastrowid, '添加项目成功' return flag, msg else: log.error('项目 {} 已存在'.format(name)) return False, '项目 {} 已存在'.format(name)
def add_message(self, user_id, m_type, content, status=1): sql = "INSERT INTO t_messages (`userId`, `type`, `content`, `status`) VALUE (%(userId)s, %(type)s, %(content)s, %(status)s)" with (yield pool.Connection()) as conn: with conn.cursor() as cursor: try: yield cursor.execute( sql, dict(userId=user_id, type=m_type, content=json.dumps(content, ensure_ascii=False), status=status)) except pymysql.Error as e: yield conn.rollback() log.error('新增动态消息失败#{}'.format(e)) flag, msg = False, '新增动态消息失败#{}'.format(e) else: yield conn.commit() log.info('新增动态消息成功') flag, msg = cursor.lastrowid, '新增动态消息成功' return flag, msg
def add_option(self, name, value): option = yield self.get_option(name=name) if not option: sql = "INSERT INTO t_options (`name`, `value`) VALUE (%(name)s, %(value)s)" with (yield pool.Connection()) as conn: with conn.cursor() as cursor: try: yield cursor.execute(sql, dict(name=name, value=value)) except pymysql.Error as e: yield conn.rollback() log.error('添加系统配置失败#{}'.format(e)) flag, msg = False, '添加系统配置失败#{}'.format(e) else: yield conn.commit() log.info('添加系统配置成功') flag, msg = cursor.lastrowid, '添加系统配置成功' else: log.error('系统配置已存在, 无法新增') flag, msg = False, '系统配置已存在, 无法新增' return flag, msg
def register_user(self, email, password): register_time = time.strftime('%Y-%m-%d %H:%M:%S') password = self.common_func.encode_password(password) cursor = yield pool.execute('SELECT COUNT(*) count FROM t_users') total = munchify(cursor.fetchone()) if total.count == 0: role = 0 else: role = 2 username = '******'.format( email.split('@')[0], str(int(time.time() * 1000))) sql = """ INSERT INTO t_users (username, email, password, registerTime, lastLoginTime, role) VALUE(%(username)s, %(email)s, %(password)s, %(registerTime)s, %(lastLoginTime)s, %(role)s) """ user = yield self.get_user_info(email_or_username=email) if not user: with (yield pool.Connection()) as conn: with conn.cursor() as cursor: try: yield cursor.execute( sql, dict(username=username, email=email, password=password, registerTime=register_time, lastLoginTime=register_time, role=role)) except pymysql.Error as e: yield conn.rollback() log.error('注册用户失败#{}'.format(e)) flag, msg = False, '注册用户失败#{}'.format(e) else: yield conn.commit() log.info('注册用户成功') flag, msg = cursor.lastrowid, '注册用户成功' else: log.error('该用户已存在, 请更换注册邮箱') flag, msg = False, '该用户已存在, 请更换注册邮箱' return flag, msg