コード例 #1
0
ファイル: reservation.py プロジェクト: may22es/TensorHive
def create(reservation: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]:
    try:
        new_reservation = Reservation(title=reservation['title'],
                                      description=reservation['description'],
                                      resource_id=reservation['resourceId'],
                                      user_id=reservation['userId'],
                                      start=reservation['start'],
                                      end=reservation['end'])

        user = User.get(get_jwt_identity())
        if (is_admin() or __is_reservation_owner(new_reservation)) \
                and ReservationVerifier.is_reservation_allowed(user, new_reservation):
            new_reservation.save()
            content = {
                'msg': RESERVATION['create']['success'],
                'reservation': new_reservation.as_dict()
            }
            status = 201
        else:
            content = {'msg': RESERVATION['create']['failure']['forbidden']}
            status = 403

    except AssertionError as e:
        content = {
            'msg': RESERVATION['create']['failure']['invalid'].format(reason=e)
        }
        status = 422
    except Exception as e:
        print(e)
        content = {'msg': GENERAL['internal_error'] + str(e)}
        status = 500
    finally:
        return content, status
コード例 #2
0
ファイル: reservation.py プロジェクト: roscisz/TensorHive
def create(reservation: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]:
    try:
        new_reservation = Reservation(title=reservation['title'],
                                      description=reservation['description'],
                                      resource_id=reservation['resourceId'],
                                      user_id=reservation['userId'],
                                      start=reservation['start'],
                                      end=reservation['end'])

        if not is_admin() and not __is_reservation_owner(new_reservation):
            raise ForbiddenException(
                "Cannot reserve resources in another user's name")

        reservation_start = DateUtils.try_parse_string(new_reservation.start)
        request_time_limit = timedelta(minutes=1)
        starts_in_the_future = (reservation_start +
                                request_time_limit) >= datetime.utcnow()
        if not is_admin() and not starts_in_the_future:
            raise ForbiddenException("Cannot reserve resources in the past")

        user = User.get(get_jwt_identity())
        if not ReservationVerifier.is_reservation_allowed(
                user, new_reservation):
            raise ForbiddenException("Reservation not allowed")

        new_reservation.save()
        content = {
            'msg': RESERVATION['create']['success'],
            'reservation': new_reservation.as_dict()
        }
        status = 201
    except ForbiddenException as e:
        content = {
            'msg':
            RESERVATION['create']['failure']['forbidden'].format(reason=e)
        }
        status = 403
    except AssertionError as e:
        content = {
            'msg': RESERVATION['create']['failure']['invalid'].format(reason=e)
        }
        status = 422
    except Exception as e:
        print(e)
        content = {'msg': GENERAL['internal_error'] + str(e)}
        status = 500
    finally:
        return content, status
コード例 #3
0
def create(reservation):
    try:
        new_reservation = Reservation(
            title=reservation['title'],
            description=reservation['description'],
            protected_resource_id=reservation['resourceId'],
            user_id=reservation['userId'],
            starts_at=reservation['start'],
            ends_at=reservation['end'])
        new_reservation.save()
    except AssertionError as e:
        content = {'msg': R['create']['failure']['invalid'].format(reason=e)}
        status = 422
    except Exception:
        content = {'msg': G['internal_error']}
        status = 500
    else:
        content = {
            'msg': R['create']['success'],
            'reservation': new_reservation.as_dict
        }
        status = 201
    finally:
        return content, status