Example #1
0
def put_room_attendance_check(data, db):
    req_list = ['user_id', 'room_id', 'pass_num']
    check_data(data, req_list)

    user = db.query(User).filter(User.id == data['user_id']).first()
    if not user:
        raise NotFound

    room = db.query(Room).filter(Room.id == data['room_id']).first()
    if not room:
        raise NotFound

    attendance_check = db.query(AttendanceCheck).filter(
        AttendanceCheck.room_id == data['room_id'],
        AttendanceCheck.user_id == data['user_id'],
        AttendanceCheck.pass_num == data['pass_num'],
        AttendanceCheck.is_valid.is_(True),
    ).first()
    if not attendance_check:
        raise NotFound

    attendance_check.is_checked = True  # 출석체크처리함
    db.commit()

    return jsonify({})
Example #2
0
def delete_room_management(data, db):  # 방 제거 함수
    req_list = ['user_id', 'room_id']
    check_data(data, req_list)

    room = db.query(Room).filter(
        Room.id == data['room_id'],
        Room.deleted_on.is_(None),
    ).first()

    if not room:  # 해당 방이 존재하지 않음
        raise NotFound

    if not str(room.master_id) == data['user_id']:  # 해당 유저가 마스터가 아니어서 권한이 없음
        raise Forbidden

    room.deleted_on = datetime.datetime.now()

    room_members = db.query(RoomMember).filter(
        RoomMember.room_id == data['room_id']).all()

    for room_member in room_members:  # 해당 방의 모든 유저 삭제
        room_member.deleted_on = datetime.datetime.now()

    db.commit()

    return jsonify({})
Example #3
0
def post_authorization_signup(data, db):
    req_list = ['email', 'pw', 'name', 'student_id', 'phone', 'rank']
    check_data(data, req_list)

    user = db.query(User).filter(User.email == data['email']).first()
    if user:  # 이미 존재하는 이메일
        raise Unauthorized

    user = db.query(User).filter(User.phone == data['phone']).first()
    if user:  # 이미 존재하는 핸드폰번호
        raise Unauthorized

    if not data['rank'] == 'teacher' and not data[
            'rank'] == 'student':  # 학생 또는 선생이 아닐 경우 잘못된 요청
        raise BadRequest

    new_user = User(email=data['email'],
                    pw=data['pw'],
                    name=data['name'],
                    student_id=data['student_id'],
                    phone=data['phone'],
                    rank=data['rank'])  # 새로운 유저 생성

    db.add(new_user)
    db.commit()

    return jsonify({})
Example #4
0
def get_room_attendance_all(data, db):  # 학생 한명의 출석 현황을 보는 함수
    req_list = ['user_id', 'room_id']
    check_data(data, req_list)

    user = db.query(User).filter(User.id == data['user_id']).first()

    if not user:
        raise NotFound

    room = db.query(Room).filter(Room.id == data['room_id']).first()

    if not room:
        raise NotFound

    attendance_checks = db.query(AttendanceCheck).filter(
        AttendanceCheck.room_id == data['room_id'],
        AttendanceCheck.user_id == data['user_id'],
    ).all()

    if not attendance_checks:
        raise NotFound

    result = {}
    for attendance_check in attendance_checks:
        result[str(
            attendance_check.created_on)[0:10]] = serialize(attendance_check)

    return jsonify({'Date': result})
Example #5
0
def put_room_management(data, db):  # 방 정보 수정
    req_list = ['user_id', 'room_id', 'title', 'maximum_population']
    check_data(data, req_list)

    user = db.query(User).filter(
        User.id == data['user_id']).first()  # 유저 객체 가져옴

    if not user:  # 해당 유저가 존재하지 않음
        raise NotFound

    room = db.query(Room).filter(
        Room.id == data['room_id'],
        Room.deleted_on.is_(None),
    ).first()

    if not room:  # 해당 방이 존재하지 않음
        raise NotFound

    if not user.id == room.master_id:  # 해당 유저가 마스터가 아니어서 수정 권한이 없음
        raise Forbidden

    room.title = data['title']
    room.maximum_population = data['maximum_population']

    db.commit()

    return jsonify(serialize(room))
Example #6
0
def put_room_attendance_check_close(data, db):  # 현재 유효한 출석 인증번호를 무효하게 수정하는 함수
    req_list = ['user_id', 'room_id']
    check_data(data, req_list)

    user = db.query(User).filter(User.id == data['user_id']).first()

    if not user:
        raise NotFound

    room = db.query(Room).filter(Room.id == data['room_id']).first()

    if not room:
        raise NotFound

    if not int(room.master_id) == int(data['user_id']):  # 방의 마스터가 아님
        raise Forbidden

    attendance_checks = db.query(AttendanceCheck).filter(
        AttendanceCheck.room_id == data['room_id'],
        AttendanceCheck.is_valid.is_(True),
    ).all()  # 아직 유요한 출첵을 가져옴

    if not attendance_checks:
        raise NotFound

    for attendance_check in attendance_checks:  # 모두 유요하지 않게 수정
        attendance_check.is_valid = False
    db.commit()

    return jsonify({})
Example #7
0
def delete_room_member_management(data, db):  # 방 탈퇴 함수
    req_list = ['user_id', 'room_id']
    check_data(data, req_list)

    user = db.query(User).filter(User.id == data['user_id']).first()
    if not user:  # 해당 유저 존재하지 않음
        raise NotFound

    room = db.query(Room).filter(Room.id == data['room_id']).first()
    if not room:  # 해당 방 존재하지 않음
        raise NotFound

    room_member = db.query(RoomMember).filter(
        RoomMember.room_id == data['room_id'],
        RoomMember.member_id == data['user_id'],
        RoomMember.deleted_on.is_(None),
    ).first()

    if not room_member:  # 가입되지 않은 방
        raise NotFound

    room_member.deleted_on = datetime.datetime.now()
    db.commit()

    return jsonify({})
Example #8
0
def get_personal_attendance(data, db):  # 출석률 확인 함수
    req_list = ['room_id']
    check_data(data, req_list)

    room_members = db.query(RoomMember).filter(
        RoomMember.room_id == data['room_id'],
        RoomMember.deleted_on.is_(None),
    ).all()

    if not room_members:  # 해당 방에 멤버가 없음
        raise NotFound

    result = dict()
    for room_member in room_members:
        # 개인 출석체크 모두 가져옴
        attendance_checks = db.query(AttendanceCheck).filter(
            AttendanceCheck.room_id == data['room_id'],
            AttendanceCheck.user_id == room_member.member_id,
        ).all()
        temp = {
            'checked': 0,
            'unchecked': 0,
        }
        for attendance_check in attendance_checks:
            if attendance_check.is_checked:
                temp['checked'] = temp['checked'] + 1
            else:
                temp['unchecked'] = temp['unchecked'] + 1
        temp['rate'] = temp['checked'] / (temp['checked'] + temp['unchecked'])
        result[str(room_member.member_id)] = temp

    return jsonify(result)
Example #9
0
def get_room_attendance_check(data, db):
    req_list = ['user_id', 'room_id']
    check_data(data, req_list)

    room = db.query(Room).filter(Room.id == data['room_id']).first()
    if not room.master_id == int(data['user_id']):  # 마스터가 아님
        raise Forbidden

    attendance_checks = db.query(AttendanceCheck).filter(
        AttendanceCheck.room_id == data['room_id'],
        AttendanceCheck.is_valid.is_(True)).all()
    result = {
        'checked': {},
        'unchecked': {},
    }
    for attendance_check in attendance_checks:
        if attendance_check.is_checked is True:
            result['checked'][str(attendance_check.user_id)] = serialize(
                db.query(User).filter(
                    User.id == attendance_check.user_id).first())
        else:
            result['unchecked'][str(attendance_check.user_id)] = serialize(
                db.query(User).filter(
                    User.id == attendance_check.user_id).first())

    return jsonify(result)
Example #10
0
def post_room_attendance_check(data, db):  # 출석체크 생성 함수
    req_list = ['user_id', 'room_id', 'pass_num']
    check_data(data, req_list)

    user = db.query(User).filter(User.id == data['user_id']).first()
    if not user:  # 해당 유저 존재하지 않음
        raise NotFound

    room = db.query(Room).filter(Room.id == data['room_id']).first()
    if not room:  # 해당 방 존재하지 않음
        raise NotFound

    if not int(room.master_id) == int(data['user_id']):  # 방의 마스터가 아님
        raise Forbidden

    attendance_checks = db.query(AttendanceCheck).filter(
        AttendanceCheck.room_id == data['room_id'],
        AttendanceCheck.is_valid.is_(True),
    ).all()  # 아직 유요한 출첵을 가져옴
    for attendance_check in attendance_checks:  # 모두 유효하지 않게 수정
        attendance_check.is_valid = False
    db.commit()

    room_members = db.query(RoomMember).filter(
        RoomMember.room_id == data['room_id'],  # 해당 방의 멤버들 가져옴
        RoomMember.deleted_on is not None).all()

    for room_member in room_members:
        new_attendance_check = AttendanceCheck(room_id=data['room_id'],
                                               user_id=room_member.member_id,
                                               pass_num=data['pass_num'])
        db.add(new_attendance_check)
    db.commit()

    return jsonify({})
Example #11
0
def post_room_management(data, db):  # 방 생성
    req_list = ['user_id', 'title']
    check_data(data, req_list)

    master = db.query(User).filter(
        User.id == data['user_id']).first()  # 유저 객체를 가져옴

    if not master:  # 해당 유저가 존재 하지 않음
        raise NotFound

    new_room = Room(
        master_id=master.id,  # 새로운 방 생성
        title=data['title'])

    if 'maximum_population' in data:  # 최대 인원을 제한했다면 정보 넣어줌
        new_room.maximum_population = data['maximum_population']

    db.add(new_room)
    db.commit()

    new_room_member = RoomMember(
        room=new_room,  # 룸 멤버로 마스터를 생성함
        member_id=master.id)

    db.add(new_room_member)
    db.commit()

    return jsonify(serialize(new_room))
Example #12
0
def post_room_member_management(data, db):  # 방 가입 함수
    req_list = ['user_id', 'invite_code']
    check_data(data, req_list)

    user = db.query(User).filter(User.id == data['user_id']).first()
    if not user:  # 해당 유저 존재하지 않음
        raise NotFound

    room = db.query(Room).filter(
        Room.invite_code == data['invite_code'],
        Room.deleted_on.is_(None),
    ).first()
    if not room:  # 해당 초대코드 존재하지 않음
        raise NotFound

    room_member = db.query(RoomMember).filter(
        RoomMember.member_id == data['user_id'], RoomMember.room_id == room.id,
        RoomMember.deleted_on.is_(None)).first()
    if room_member:  # 이미 가입된 방
        raise Conflict

    all_room_member_count = db.query(RoomMember).filter(
        RoomMember.member_id == data['user_id'],
        RoomMember.deleted_on.is_(None),
    ).count()
    if room.maximum_population == all_room_member_count:  # 가입 가능 인원수 초과
        raise Conflict

    new_room_member = RoomMember(room_id=room.id, member_id=data['user_id'])
    db.add(new_room_member)
    db.commit()

    return jsonify(serialize(room))
Example #13
0
def get_authorization_email(data, db):
    req_list = ['email']
    check_data(data, req_list)

    user = db.query(User).filter(User.email == data['email']).first()
    if user:  # 이미 사용중인 이메일
        raise Conflict

    return jsonify({})
Example #14
0
def get_authorization_phone(data, db):
    req_list = ['phone']
    check_data(data, req_list)

    user = db.query(User).filter(User.phone == data['phone']).first()
    if user:  # 이미 사용중인 전화번호
        raise Conflict

    return jsonify({})
Example #15
0
def post_authorization_login(data, db):
    req_list = ['email', 'pw']
    check_data(data, req_list)

    user = db.query(User).filter(User.email == data['email'],
                                 User.pw == data['pw']).first()
    if not user:  # email 또는 pw이 틀림
        return jsonify({'reason': 'login fail'}), 404

    return jsonify(serialize(user))
Example #16
0
def get_room_management(data, db):  # 해당 방의 정보를 가져옴
    req_list = ['room_id']
    check_data(data, req_list)

    room = db.query(Room).filter(
        Room.id == data['room_id'],
        Room.deleted_on.is_(None),
    ).first()
    if not room:  # 해당 방이 존재하지 않음
        raise NotFound

    return jsonify(serialize(room))
Example #17
0
def get_room_member_all(data, db):  # 해당 방의 정보를 가져옴
    req_list = ['room_id']
    check_data(data, req_list)

    room = db.query(Room).filter(
        Room.id == data['room_id'],
        Room.deleted_on.is_(None),
    ).first()

    if not room:  # 해당 방이 존재하지 않음
        raise NotFound

    room_members = db.query(RoomMember).filter(
        RoomMember.room_id == data['room_id'],
        RoomMember.deleted_on.is_(None),
    ).all()

    if not room_members:  # 해당 방에 멤버가 없음
        raise NotFound

    result = dict()
    for room_member in room_members:
        attendance_checks = db.query(AttendanceCheck).filter(
            AttendanceCheck.room_id == data['room_id'],
            AttendanceCheck.user_id == room_member.member_id,
        ).all()
        temp = {
            'checked': 0,
            'unchecked': 0,
        }
        for attendance_check in attendance_checks:
            if attendance_check.is_checked:
                temp['checked'] = temp['checked'] + 1
            else:
                temp['unchecked'] = temp['unchecked'] + 1

        if temp['unchecked'] < 1:
            if temp['checked'] > 0:
                temp['rate'] = 1.0
            else:
                temp['rate'] = 0.0
        else:
            temp['rate'] = temp['checked'] / (temp['checked'] +
                                              temp['unchecked'])

        user = db.query(User).filter(User.id == room_member.member_id).first()
        suser = serialize(user)
        suser['rate_info'] = temp
        result[str(user.id)] = suser

    return jsonify({'User': result})
Example #18
0
def get_room_member_management(data, db):  # 가입된 방을 모두 가져오는 함수
    req_list = ['user_id']
    check_data(data, req_list)

    user = db.query(User).filter(
        User.id == data['user_id']).first()  # 유저 객체 가져옴

    if not user:  # 해당 유저가 존재하지 않음
        raise NotFound

    room_members = db.query(RoomMember).filter(
        RoomMember.member_id == user.id,
        RoomMember.deleted_on.is_(None),
    ).all()  # 유저가 가입된 방을 모두 가져옴

    if not room_members:  # 가입된 방이 없음
        raise NotFound

    result = {}
    for index, room_member in enumerate(room_members):
        room = db.query(Room).filter(Room.id == room_member.room_id).first()
        result[str(index)] = serialize(room)

    return jsonify(result)