예제 #1
0
def delete_lesson(user_id, lesson_id):
    lesson = Lesson.query.get(lesson_id)
    if lesson is None:
        raise NoSuchElementError(extra_message='Lesson does not exist.')
    owner = lesson.owner
    if owner is None:
        raise NoSuchElementError(extra_message='User does not exist.')
    if owner.id != user_id:
        raise InsufficientPermissionError()
    db.session.delete(lesson)
    db.session.commit()
예제 #2
0
def select_teacher(lesson_id, owner_id, user_id):
    lesson = Lesson.query.get(lesson_id)
    if lesson.status == LessonStatus.BIDDING \
            or lesson.status == LessonStatus.FINISHED:
        raise BadRequestError(cause=ErrorCause.EXPIRED_LESSON_DEALING,
                              extra_message='Lesson status is not dealing.')
    owner = lesson.owner
    if owner.id != owner_id:
        raise InsufficientPermissionError()
    selected_user = User.query.get(user_id)
    if selected_user is None:
        raise NoSuchElementError(
            extra_message='The selected User does not exist.')
    lesson_bidding = LessonBidding.query \
        .filter(LessonBidding.lesson_id == lesson_id) \
        .filter(LessonBidding.user_id == user_id).first()
    if lesson_bidding is None:
        raise NoSuchElementError(extra_message='User does not bid.')
    # 학생의 coin 값 차감
    #subtract_coins(owner_id,
    #               max(int((lesson.preferred_price - lesson_bidding.price) * 3),
    #                   CoinValueOfTransactionType.MIN_SELECT_USER),
    #               TransactionType.SELECT_TEACHER)

    subtract_coins(owner_id, CoinValueOfTransactionType.MIN_SELECT_USER,
                   TransactionType.SELECT_TEACHER)
    lesson.status = LessonStatus.FINISHED
    lesson.selected_user_id = selected_user.id
    lesson_bidding.is_selected = True
    if not is_checked_phone_number(owner_id, user_id):
        check = CheckPhoneNumber()
        check.user_id = owner_id
        check.checked_user_id = user_id
        check.created_at = current_millis()
        db.session.add(check)
    if not is_checked_phone_number(user_id, owner_id):
        check = CheckPhoneNumber()
        check.user_id = user_id
        check.checked_user_id = owner_id
        check.created_at = current_millis()
        db.session.add(check)
    db.session.commit()

    # 선택되지 않은 teacher들 coin 값 반환
    unselected_biddings = LessonBidding.query \
        .filter(LessonBidding.lesson_id == lesson_id) \
        .filter(LessonBidding.user_id != user_id).all()
    for bidding in unselected_biddings:
        add_coins(bidding.user_id, CoinValueOfTransactionType.BIDDING,
                  TransactionType.REFUND_FOR_UNSELECTED)
    add_notification(NotificationRequest().to(
        selected_user.id).on_user_selected(selected_user.id, lesson.id))
    return selected_user
예제 #3
0
def set_profile_photos(user_id, images):
    user = User.query.get(user_id)
    if ListUtils.is_null_or_empty(images):
        return user
    if user is None:
        raise NoSuchElementError(id=user_id)
    user.profile_photos = images
    db.session.commit()
    return user
예제 #4
0
def issue_token(email_credential_req):
    if not is_all_attributes_not_none(email_credential_req):
        raise InvalidArgumentError()
    email = email_credential_req.email
    password = email_credential_req.password
    user = User.query.filter_by(email=email).first()
    if user is None:
        raise NoSuchElementError()
    if not user.verify_password(password):
        raise BadRequestError(cause=ErrorCause.PASSWORD_NOT_MATCHED, message='Password not matched.')
    token = user.generate_auth_token(current_app.config['AUTH_TOKEN_EXPIRES_IN'])
    return IssueTokenResult(user=user, token=token)
예제 #5
0
def add_favorite(from_user_id, to_user_id):
    favorite = get_favorite(from_user_id, to_user_id)
    if favorite is not None:
        raise ElementAlreadyExists(extra_message='Already favorites.')
    if User.query.get(from_user_id) is None or User.query.get(
            to_user_id) is None:
        raise NoSuchElementError(extra_message='User does not exist.')
    favorite = UserFavorite()
    favorite.from_user_id = from_user_id
    favorite.to_user_id = to_user_id
    favorite.created_at = current_millis()
    db.session.add(favorite)
    add_notification(NotificationRequest().to(to_user_id).on_user_favorited(
        to_user_id, from_user_id))
    db.session.commit()
    return favorite
예제 #6
0
def cancel_lesson(user_id, lesson_id):
    lesson = Lesson.query.get(lesson_id)
    if lesson is None:
        raise NoSuchElementError(extra_message='Lesson does not exist.')
    if lesson.owner.id != user_id:
        raise InsufficientPermissionError()
    lesson.status = LessonStatus.CANCELED
    db.session.commit()

    biddings = LessonBidding.query.filter(
        LessonBidding.lesson_id == lesson_id).all()
    for bidding in biddings:
        add_notification(NotificationRequest().to(
            bidding.user.id).on_lesson_canceled(lesson.owner.id, lesson.id))
        add_coins(bidding.user_id, CoinValueOfTransactionType.BIDDING,
                  TransactionType.REFUND)
    return lesson
예제 #7
0
def check_phone_number(user_id, checked_user_id):
    user = User.query.get(user_id)
    checked_user = User.query.get(checked_user_id)
    if user is None or checked_user is None:
        raise NoSuchElementError(extra_message='User does not exist.')
    check = get_check_phone_number(user_id, checked_user_id)
    if check is not None:
        return checked_user

    # 전화번호를 확인하면 coin을 차감한다.
    subtract_coins(user_id, CoinValueOfTransactionType.CHECK_USER_PHONE_NUMBER,
                   TransactionType.CHECK_USER_PHONE_NUMBER)
    check = CheckPhoneNumber()
    check.user_id = user_id
    check.checked_user_id = checked_user_id
    check.created_at = current_millis()
    db.session.add(check)
    db.session.commit()
    return checked_user
예제 #8
0
def add_lesson_bidding(lesson_id, user_id, bidding_request):
    lesson = Lesson.query.get(lesson_id)
    if lesson is None:
        raise NoSuchElementError(extra_message='Lesson is not existed.')
    if lesson.status != LessonStatus.BIDDING:
        raise BadRequestError(cause=ErrorCause.EXPIRED_LESSON_BIDDING,
                              message='Lesson is expired.')
    user = User.query.get(user_id)
    if user.type != UserType.TEACHER:
        raise InvalidArgumentError(
            extra_message='It can be available only by teacher.')
    bidding = LessonBidding.query \
        .filter(LessonBidding.lesson_id == lesson_id) \
        .filter(LessonBidding.user_id == user.id).first()
    if bidding is not None:
        raise ElementAlreadyExists(extra_message='Already bidding.')
    bidding = LessonBidding()
    bidding.lesson_id = lesson.id
    bidding.user_id = user.id
    if bidding_request is None:
        raise InvalidArgumentError(
            extra_message='Bidding price must not be null.')
    price = StringUtils.to_int(bidding_request.price)
    if price is None:
        raise InvalidArgumentError(extra_message='Price is wrong.')
    bidding.price = price
    bidding.created_at = current_millis()
    db.session.add(bidding)
    db.session.commit()

    # 선생님은 입찰할 때마다 coin을 차감시킨다.
    subtract_coins(user_id, CoinValueOfTransactionType.BIDDING,
                   TransactionType.BIDDING)
    add_notification(NotificationRequest().to(
        lesson.owner.id).on_bidding_lesson(user_id, lesson.id))
    return bidding
예제 #9
0
def get_user(user_id):
    user = User.query.get(user_id)
    if user is None:
        raise NoSuchElementError(id=user_id)
    return user