def change_info(db, email, username, oldpassword, password): # 修改用户信息 cursor = db.cursor() sql = 'SELECT id FROM user WHERE password = "******"'.format(SomeTool.key(oldpassword)) cursor.execute(sql) result = cursor.fetchone() if result == None: return False sql = 'UPDATE user SET email = "{}", username = "******", password = "******" WHERE id = {}' \ ''.format(email, username, SomeTool.key(password), int(result[0])) cursor.execute(sql) cursor.close() try: db.commit() return True except: db.rollback() return False
def login(db, email, password): # 登录 cursor = db.cursor() sql = 'SELECT id FROM user WHERE email = "{}" AND password = "******"' \ ''.format(email, SomeTool.key(password)) cursor.execute(sql) result = cursor.fetchone() cursor.close() return result if result != None else False
def upload_pic(pic, username): # 上传图片 filename = 'static/resource/pic/' + username + SomeTool.current_date( ).replace(':', '-') + '.png' with open( filename, 'wb', ) as file: file.write(pic) return filename
def post_update(db, version, content): cursor = db.cursor() sql = 'INSERT INTO update_list (version, content, created) VALUES ("{}", \'{}\', "{}")' \ ''.format(version, str(content).replace('\'', '"'), SomeTool.current_date()) cursor.execute(sql) cursor.close() try: db.commit() return True except: db.rollback() return False
def register(db, email, username, password): # 注册普通用户 cursor = db.cursor() sql = 'INSERT INTO user ("email", "username", "password", "type", "created") VALUES ' \ '("{}", "{}", "{}", "normal", "{}")'.format(email, username, SomeTool.key(password), SomeTool.current_date()) cursor.execute(sql) cursor.close() try: db.commit() return MainTool.login(db, email, password) except: db.rollback() return False
def add_title(db, title, hidden, userId): # 添加分类 cursor = db.cursor() sql = 'INSERT INTO title (title, userId, created, hidden) VALUES (\'{}\', {}, "{}", {})' \ ''.format(str(title).replace('\'', '"'), userId, SomeTool.current_date(), hidden) cursor.execute(sql) cursor.close() try: db.commit() return True except: db.rollback() return False
def edit_content(db, contentId, head, titleId, content, hidden): cursor = db.cursor() created = SomeTool.current_date() sql = 'UPDATE content SET head = \'{}\', titleId = {}, content = \'{}\', hidden = {}, created = \'{}\' ' \ 'WHERE id = {}'.format(str(head).replace('\'', '"'), titleId, str(content).replace('\'', '"'), hidden, created, contentId) cursor.execute(sql) try: db.commit() sql = 'SELECT title FROM title WHERE id = {}'.format(titleId) cursor.execute(sql) results = list(cursor.fetchone()) results.append(created) cursor.close() return results except: db.rollback() cursor.close() return False
def add_notice(db, content): cursor = db.cursor() sql = 'INSERT INTO notice (content, created) VALUES (\'{}\', "{}")'.format(str(content).replace('\'', '"'), SomeTool.current_date()) cursor.execute(sql) cursor.close() try: db.commit() return True except: db.rollback() return False
def add_comment(db, contentId, email, comment): # 新增评论 cursor = db.cursor() sql = 'INSERT INTO comment (email, comment, contentId, created, hidden) VALUES ' \ '("{}", \'{}\', {}, "{}", 0)'.format(email, str(comment).replace('\'', '"'), contentId, SomeTool.current_date()) cursor.execute(sql) cursor.close() try: db.commit() return True except: db.rollback() return False
async def get(self, *args, **kwargs): # 返回搜索结果 self.write( json.dumps( SomeTool.search(self.application.db, self.get_argument('key'), self.get_argument('userId'))))
async def post(self, *args, **kwargs): # 添加 ban ip SomeTool.add_ip(self.application.db, self.get_argument('ip'))
def add_content(db, titleId, head, content, hidden): cursor = db.cursor() sql = 'INSERT INTO content (head, content, titleId, created, hidden) VALUES ' \ '(\'{}\', \'{}\', \'{}\', \'{}\', {})' \ ''.format(str(head).replace('\'', '"'), str(content).replace('\'', '"'), titleId, SomeTool.current_date(), hidden) print(sql) cursor.execute(sql) cursor.close() try: db.commit() return True except: db.rollback() return False