def update_password(): uid = request.form['uid'] password = request.form['password'] db = database.init() cursor = db.cursor() cursor.execute('UPDATE user SET password = %s WHERE id = %s', (password, uid)) db.commit() db.close() return 'ok'
def get_post_by_uid(): uid = request.form['uid'] db = database.init() cursor = db.cursor() cursor.execute( 'SELECT id, content, image FROM post WHERE uid = %s ORDER BY date DESC', uid) posts = list() for data in cursor.fetchall(): post = {'pid': data[0], 'content': data[1], 'image': data[2]} posts.append(post) return json.dumps(posts)
def update_avatar(): uid = request.form['uid'] image64 = request.form['avatar'] img_info = re.search('data:image/(?P<ext>.*?);base64,(?P<data>.*)', image64, re.DOTALL) if img_info: res_path = tool.save_base64_img(img_info) db = database.init() cursor = db.cursor() cursor.execute('UPDATE user SET avatar = %s WHERE id = %s', (res_path, uid)) db.commit() db.close() return 'ok'
def update_username_info(): uid = request.form['uid'] username = request.form['username'] gender = request.form['gender'] introduction = request.form['introduction'] db = database.init() cursor = db.cursor() cursor.execute( "UPDATE user SET username = %s, gender = %s, introduction = %s WHERE id = %s", (username, gender, introduction, uid)) db.commit() db.close() return 'ok'
def get_user_by_uid(): uid = request.form['uid'] db = database.init() cursor = db.cursor() cursor.execute('SELECT username, avatar, gender, introduction FROM user WHERE id=%s', uid) username, avatar, gender, introduction = cursor.fetchone() db.close() res = { 'username': username, 'uid': uid, 'avatar': avatar, 'gender': gender, 'introduction': introduction } return json.dumps(res)
def get_user(): username = session.get('user') db = database.init() cursor = db.cursor() cursor.execute('SELECT id, avatar, gender, introduction FROM user WHERE username=%s', username) uid, avatar, gender, introduction = cursor.fetchone() db.close() res = { 'username': username, 'uid': uid, 'avatar': avatar, 'gender': gender, 'introduction': introduction } return json.dumps(res)
def create_post(): uid = request.form['uid'] text = request.form['text'] image64 = request.form['image'] img_info = re.search('data:image/(?P<ext>.*?);base64,(?P<data>.*)', image64, re.DOTALL) if img_info: res_path = tool.save_base64_img(img_info) db = database.init() cursor = db.cursor() cursor.execute( 'INSERT INTO post(uid, content, image, date) VALUES(%s, %s, %s, %s)', (int(uid), text, res_path, int(round(time.time() * 1000)))) db.commit() db.close() return 'ok' else: raise Exception('Cannot parse!')
def register(): username = request.form['username'] password = request.form['password'] db = database.init() cursor = db.cursor() cursor.execute('SELECT username FROM user WHERE username = %s', username) username_ = cursor.fetchone() if not username_: cursor.execute( 'INSERT INTO user(username, password, avatar) VALUES(%s, %s, %s)', (username, password, 'http://188.131.253.83:8092/img/default/avatar.png')) db.commit() db.close() session['user'] = username return 'ok' else: db.close() return '用户名已存在'
def login(): username = request.form['username'] password = request.form['password'] print(username, password) db = database.init() cursor = db.cursor() cursor.execute('SELECT username FROM user WHERE username= %s', username) username_ = cursor.fetchone() if username_: cursor.execute('SELECT password FROM user WHERE username= %s', username) password_ = cursor.fetchone()[0] if password == password_: session['user'] = username return 'ok' else: return '用户名或密码错误' else: return '用户名不存在'
def get_post(): db = database.init() cursor = db.cursor() cursor.execute('SELECT * FROM post ORDER BY date DESC') posts = list() for data in cursor.fetchall(): cursor.execute('SELECT username, avatar FROM USER WHERE id = %s', data[1]) username, avatar = cursor.fetchone() post = { 'id': data[0], 'uid': data[1], 'username': username, 'avatar': avatar, 'content': data[2], 'image': data[3] } posts.append(post) db.close() return json.dumps(posts)