Esempio n. 1
0
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
        }))
Esempio n. 2
0
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
            }))
Esempio n. 3
0
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
        }))
Esempio n. 4
0
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
            }))
Esempio n. 5
0
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"))
Esempio n. 6
0
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}))
Esempio n. 7
0
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}))
Esempio n. 8
0
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
        }))
Esempio n. 9
0
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
        }))
Esempio n. 10
0
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}))
Esempio n. 11
0
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}))
Esempio n. 12
0
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
Esempio n. 13
0
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
Esempio n. 14
0
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'))
Esempio n. 15
0
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'))
Esempio n. 16
0
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}))
Esempio n. 17
0
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))
Esempio n. 18
0
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))
Esempio n. 19
0
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}))
Esempio n. 20
0
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}))
Esempio n. 21
0
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
        }))
Esempio n. 22
0
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
        }))
Esempio n. 23
0
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}))
Esempio n. 24
0
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
        }))
Esempio n. 25
0
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
        }))
Esempio n. 26
0
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"))