def modify_room(): if 'manager_id' not in session: return jsonify(api_format(status.HTTP_406_NOT_ACCEPTABLE, "you are not login")) manager_id = session['manager_id'] json_content = request.get_json() room_id = json_content['room_id'] modified_items = json_content['modified_items'] result_room = Room.query.filter_by(id=room_id, manager_id=manager_id).first() if result_room is None: return jsonify(api_format(status.HTTP_404_NOT_FOUND, "room is not existed")) for item_name in modified_items: if item_name == "start_time" or item_name == "end_time": time_item = int(modified_items[item_name]) setattr(result_room, item_name, time_item) else: setattr(result_room, item_name, modified_items[item_name]) db.session.commit() return jsonify(api_format( status.HTTP_200_OK, "ok", { 'room_id': result_room.id, 'room_name': result_room.name, 'start_time': result_room.start_time, 'end_time': result_room.end_time, 'description': result_room.description }))
def modify_room(): if 'manager_id' not in session: return jsonify( api_format(status.HTTP_406_NOT_ACCEPTABLE, "you are not login")) manager_id = session['manager_id'] json_content = request.get_json() room_id = json_content['room_id'] modified_items = json_content['modified_items'] result_room = Room.query.filter_by(id=room_id, manager_id=manager_id).first() if result_room is None: return jsonify( api_format(status.HTTP_404_NOT_FOUND, "room is not existed")) for item_name in modified_items: if item_name == "start_time" or item_name == "end_time": time_item = int(modified_items[item_name]) setattr(result_room, item_name, time_item) else: setattr(result_room, item_name, modified_items[item_name]) db.session.commit() return jsonify( api_format( status.HTTP_200_OK, "ok", { 'room_id': result_room.id, 'room_name': result_room.name, 'start_time': result_room.start_time, 'end_time': result_room.end_time, 'description': result_room.description }))
def create_name(): """Create nick name for user to join a room Add user info to session and redis Args: nick_name: the user's nick name room_id: the id of room Returns: uid: the user_id to identify the user nick_name: the user's nick_name """ json_content = request.get_json() if 'uid' not in session: session['uid'] = str(uuid.uuid4()) nick_name = json_content['nick_name'] room_id = json_content['room_id'] room = Room.query.get(room_id) if room is None: session.pop(room_id, None) return jsonify(api_format(status.HTTP_404_NOT_FOUND, "no such room")) session[room_id] = nick_name redis.hmset(room_id + ':' + session['uid'], { 'uid': session['uid'], 'nick_name': nick_name }) return jsonify( api_format(status.HTTP_200_OK, "ok", { 'uid': session['uid'], 'nick_name': nick_name }))
def create_room(): if 'manager_id' not in session: return jsonify( api_format(status.HTTP_406_NOT_ACCEPTABLE, "you are not login")) json_content = request.get_json() room_name = json_content['room_name'] description = json_content['description'] start_time = int(json_content['start_time']) end_time = int(json_content['end_time']) manager_id = session['manager_id'] while True: room_id = generate_random_num_str(6) if Room.query.get(room_id) is None: break db.session.add( Room(room_id=room_id, room_name=room_name, start_time=start_time, end_time=end_time, description=description, manager_id=manager_id)) db.session.commit() redis.set(room_id + ':message_num', 0) return jsonify( api_format( status.HTTP_200_OK, "ok", { 'room_id': room_id, 'room_name': room_name, 'start_time': start_time, 'end_time': end_time, 'description': description }))
def get_name(): room_id = request.values['room_id'] if 'uid' and room_id in session: return jsonify( api_format(status.HTTP_200_OK, "ok", { 'uid': session['uid'], 'nick_name': session[room_id] })) return jsonify(api_format(status.HTTP_404_NOT_FOUND, "not found"))
def delete_room(): if 'manager_id' not in session: return jsonify(api_format(status.HTTP_406_NOT_ACCEPTABLE, "you are not login")) manager_id = session['manager_id'] room_id = request.get_json()['room_id'] result_room = Room.query.filter_by(id=room_id, manager_id=manager_id).first() if result_room is None: return jsonify(api_format(status.HTTP_404_NOT_FOUND, "room is not existed")) db.session.delete(result_room) Message.query.filter_by(room_id=room_id).delete() db.session.commit() redis.delete(room_id + ':message_num') return jsonify(api_format(status.HTTP_200_OK, "delete finish", {"room_id": result_room.id}))
def get_room_list(): if 'manager_id' not in session: return jsonify(api_format(status.HTTP_406_NOT_ACCEPTABLE, "you are not login")) manager_id = session['manager_id'] result_list = Room.query.filter_by(manager_id=manager_id).all() room_list = [] for room in result_list: room_list.append({ 'room_id': room.id, 'room_name': room.name, 'start_time': room.start_time, 'end_time': room.end_time, 'description': room.description }) return jsonify(api_format(status.HTTP_200_OK, "ok", {'room_list': room_list}))
def login(): json_content = request.get_json() manager_email = json_content['manager_email'] manager_password = json_content['manager_password'] result_manager = Manager.query.filter_by(email=manager_email, password=manager_password).first() if result_manager is None: return jsonify(api_format(status.HTTP_406_NOT_ACCEPTABLE, "email or password wrong")) session['manager_id'] = result_manager.id return jsonify(api_format( status.HTTP_200_OK, "ok", { 'manager_id': result_manager.id, 'manager_name': result_manager.name }))
def login(): json_content = request.get_json() manager_email = json_content['manager_email'] manager_password = json_content['manager_password'] result_manager = Manager.query.filter_by( email=manager_email, password=manager_password).first() if result_manager is None: return jsonify( api_format(status.HTTP_406_NOT_ACCEPTABLE, "email or password wrong")) session['manager_id'] = result_manager.id return jsonify( api_format(status.HTTP_200_OK, "ok", { 'manager_id': result_manager.id, 'manager_name': result_manager.name }))
def get_room_list(): if 'manager_id' not in session: return jsonify( api_format(status.HTTP_406_NOT_ACCEPTABLE, "you are not login")) manager_id = session['manager_id'] result_list = Room.query.filter_by(manager_id=manager_id).all() room_list = [] for room in result_list: room_list.append({ 'room_id': room.id, 'room_name': room.name, 'start_time': room.start_time, 'end_time': room.end_time, 'description': room.description }) return jsonify( api_format(status.HTTP_200_OK, "ok", {'room_list': room_list}))
def delete_room(): if 'manager_id' not in session: return jsonify( api_format(status.HTTP_406_NOT_ACCEPTABLE, "you are not login")) manager_id = session['manager_id'] room_id = request.get_json()['room_id'] result_room = Room.query.filter_by(id=room_id, manager_id=manager_id).first() if result_room is None: return jsonify( api_format(status.HTTP_404_NOT_FOUND, "room is not existed")) db.session.delete(result_room) Message.query.filter_by(room_id=room_id).delete() db.session.commit() redis.delete(room_id + ':message_num') return jsonify( api_format(status.HTTP_200_OK, "delete finish", {"room_id": result_room.id}))
def upload_file(): """ Handle the file upload Args: flowIdentifier: 文件的ID flowFilename: 文件的名字 flowTotalSize: 文件的大小 flowChunkNumber: 当前分片序号 flowCurrentChunkSize: 当前分片的大小 roomId: 当前房间的目录 """ if request.method == 'GET': if request.args.get('flowChunkNumber') == '1': tmp_file = TmpFileMap.query.get(request.args.get('flowIdentifier')) if not tmp_file: db.session.add( TmpFileMap( file_identifer=request.args.get('flowIdentifier'), file_name=request.args.get('flowFilename'), total_size=int(request.args.get('flowTotalSize')), current_size=0, current_chunk=0, room_id=request.args.get('roomId'), )) db.session.commit() return "Ready for content", status.HTTP_204_NO_CONTENT elif request.method == 'POST': file_chunk = request.files['file'].read() tmp_file = TmpFileMap.query.get(request.form['flowIdentifier']) if tmp_file.current_chunk + 1 == int(request.form['flowChunkNumber']): tmp_file.current_size += int(request.form['flowCurrentChunkSize']) tmp_file.current_chunk += 1 with open( os.path.join(app.config['TMP_UPLOAD_FOLDER'], request.form['flowIdentifier']), 'a') as f: f.write(file_chunk) if tmp_file.current_size == tmp_file.total_size: new_file = File(file_name=tmp_file.file_name, file_format=tmp_file.file_format, file_size=tmp_file.total_size, room_id=tmp_file.room_id) db.session.add(new_file) db.session.commit() os.rename( os.path.join(app.config['TMP_UPLOAD_FOLDER'], tmp_file.id), os.path.join(app.config['UPLOAD_FOLDER'], str(new_file.id))) db.session.delete(tmp_file) db.session.commit() return jsonify( api_format( status.HTTP_200_OK, "ok", { 'file_id': new_file.id, 'file_name': new_file.file_name, 'file_format': new_file.file_format, 'file_size': new_file.file_size })), status.HTTP_200_OK
def upload_file(): """ Handle the file upload Args: flowIdentifier: 文件的ID flowFilename: 文件的名字 flowTotalSize: 文件的大小 flowChunkNumber: 当前分片序号 flowCurrentChunkSize: 当前分片的大小 roomId: 当前房间的目录 """ if request.method == 'GET': if request.args.get('flowChunkNumber') == '1': tmp_file = TmpFileMap.query.get(request.args.get('flowIdentifier')) if not tmp_file: db.session.add( TmpFileMap( file_identifer=request.args.get('flowIdentifier'), file_name=request.args.get('flowFilename'), total_size=int(request.args.get('flowTotalSize')), current_size=0, current_chunk=0, room_id=request.args.get('roomId'), ) ) db.session.commit() return "Ready for content", status.HTTP_204_NO_CONTENT elif request.method == 'POST': file_chunk = request.files['file'].read() tmp_file = TmpFileMap.query.get(request.form['flowIdentifier']) if tmp_file.current_chunk + 1 == int(request.form['flowChunkNumber']): tmp_file.current_size += int(request.form['flowCurrentChunkSize']) tmp_file.current_chunk += 1 with open(os.path.join(app.config['TMP_UPLOAD_FOLDER'], request.form['flowIdentifier']), 'a') as f: f.write(file_chunk) if tmp_file.current_size == tmp_file.total_size: new_file = File( file_name=tmp_file.file_name, file_format=tmp_file.file_format, file_size=tmp_file.total_size, room_id=tmp_file.room_id ) db.session.add(new_file) db.session.commit() os.rename( os.path.join(app.config['TMP_UPLOAD_FOLDER'], tmp_file.id), os.path.join(app.config['UPLOAD_FOLDER'], str(new_file.id))) db.session.delete(tmp_file) db.session.commit() return jsonify(api_format(status.HTTP_200_OK, "ok", { 'file_id': new_file.id, 'file_name': new_file.file_name, 'file_format': new_file.file_format, 'file_size': new_file.file_size })), status.HTTP_200_OK
def download_file(): file_id = request.values['file_id'] result_file = File.query.get(int(file_id)) if result_file is None: return jsonify(api_format(status.HTTP_404_NOT_FOUND, "No such file")) return send_from_directory( app.config['UPLOAD_FOLDER'], str(file_id), as_attachment=True, attachment_filename=result_file.file_name.encode('utf8'))
def get_file_list(): room_id = request.values['room_id'] result_list = File.query.filter_by(room_id=room_id) file_list = [] for file_item in result_list: file_list.append({ 'file_id': file_item.id, 'file_name': file_item.file_name, 'file_format': file_item.file_format, 'file_size': file_item.file_size }) return jsonify(api_format(status.HTTP_200_OK, "ok", {'room_id': room_id, 'file_list': file_list}))
def get_room_info(): """Get room info of the pointed room Args: room_id: the room that pointed Returns: room_info """ room_id = request.values['room_id'] room = Room.query.get(room_id) if room is None: return jsonify(api_format(status.HTTP_404_NOT_FOUND, "not found")) else: data = { "room_id": room.id, "name": room.name, "start_time": room.start_time, "end_time": room.end_time, "description": room.description } return jsonify(api_format(status.HTTP_200_OK, "ok", data))
def get_room_messages(): room_id = request.values['room_id'] message_num = int(request.values['message_num']) result_list = Message.query.filter_by(room_id=room_id).order_by(Message.message_time).limit(message_num).all() message_list = [] for message in result_list: message_list.append({ 'serial_number': message.serial_number, 'uid': message.uid, 'nick_name': message.nick_name, 'message_time': message.message_time, 'content': message.content }) return jsonify(api_format(status.HTTP_200_OK, "ok", {'room_id': room_id, 'message_list': message_list}))
def create_name(): """Create nick name for user to join a room Add user info to session and redis Args: nick_name: the user's nick name room_id: the id of room Returns: uid: the user_id to identify the user nick_name: the user's nick_name """ json_content = request.get_json() if 'uid' not in session: session['uid'] = str(uuid.uuid4()) nick_name = json_content['nick_name'] room_id = json_content['room_id'] room = Room.query.get(room_id) if room is None: session.pop(room_id, None) return jsonify(api_format(status.HTTP_404_NOT_FOUND, "no such room")) session[room_id] = nick_name redis.hmset(room_id + ':' + session['uid'], {'uid': session['uid'], 'nick_name': nick_name}) return jsonify(api_format(status.HTTP_200_OK, "ok", {'uid': session['uid'], 'nick_name': nick_name}))
def create_room(): if 'manager_id' not in session: return jsonify(api_format(status.HTTP_406_NOT_ACCEPTABLE, "you are not login")) json_content = request.get_json() room_name = json_content['room_name'] description = json_content['description'] start_time = int(json_content['start_time']) end_time = int(json_content['end_time']) manager_id = session['manager_id'] while True: room_id = generate_random_num_str(6) if Room.query.get(room_id) is None: break db.session.add( Room( room_id=room_id, room_name=room_name, start_time=start_time, end_time=end_time, description=description, manager_id=manager_id ) ) db.session.commit() redis.set(room_id + ':message_num', 0) return jsonify(api_format( status.HTTP_200_OK, "ok", { 'room_id': room_id, 'room_name': room_name, 'start_time': start_time, 'end_time': end_time, 'description': description }))
def get_file_list(): room_id = request.values['room_id'] result_list = File.query.filter_by(room_id=room_id) file_list = [] for file_item in result_list: file_list.append({ 'file_id': file_item.id, 'file_name': file_item.file_name, 'file_format': file_item.file_format, 'file_size': file_item.file_size }) return jsonify( api_format(status.HTTP_200_OK, "ok", { 'room_id': room_id, 'file_list': file_list }))
def get_room_members(): """Get user_id list of the pointed room Args: room_id: the room that pointed Returns: room_id: the room that pointed user_list: the list of user_id """ room_id = request.values['room_id'] uid_list = list(redis.smembers(room_id)) user_list = [] for uid in uid_list: user_list.append(redis.hgetall(uid)) return jsonify(api_format(status.HTTP_200_OK, "ok", {'room_id': room_id, 'user_list': user_list}))
def get_room_messages(): room_id = request.values['room_id'] message_num = int(request.values['message_num']) result_list = Message.query.filter_by(room_id=room_id).order_by( Message.message_time).limit(message_num).all() message_list = [] for message in result_list: message_list.append({ 'serial_number': message.serial_number, 'uid': message.uid, 'nick_name': message.nick_name, 'message_time': message.message_time, 'content': message.content }) return jsonify( api_format(status.HTTP_200_OK, "ok", { 'room_id': room_id, 'message_list': message_list }))
def get_room_members(): """Get user_id list of the pointed room Args: room_id: the room that pointed Returns: room_id: the room that pointed user_list: the list of user_id """ room_id = request.values['room_id'] uid_list = list(redis.smembers(room_id)) user_list = [] for uid in uid_list: user_list.append(redis.hgetall(uid)) return jsonify( api_format(status.HTTP_200_OK, "ok", { 'room_id': room_id, 'user_list': user_list }))
def get_name(): room_id = request.values['room_id'] if 'uid' and room_id in session: return jsonify(api_format(status.HTTP_200_OK, "ok", {'uid': session['uid'], 'nick_name': session[room_id]})) return jsonify(api_format(status.HTTP_404_NOT_FOUND, "not found"))