def date_multiplier(date_from):
    date_now = datetime.now()
    num_days = (date_from - date_now).days
    if num_days < 0:
        raise illegal_date.IllegalDateException(date_from)
    elif num_days < 2:
        return 2.25
    elif num_days < 7:
        return 1.75
    elif num_days < 14:
        return 1.5
    elif num_days < 21:
        return 1.2
    elif num_days < 45:
        return 1
    elif num_days < 90:
        return 0.8
    else:
        raise illegal_date.IllegalDateException(date_from)
def get_onestop_flights(from_, to, filters, context):
    if filters["date_to"] - filters["date_from"] < timedelta(0):
        raise illegal_date.IllegalDateException(
            "from date can not be greater than to date")

    if os.environ["DATABASE"] == "janus":
        flights = janus_service.get_onestop_flights_from_janus(
            from_, to, context)
    else:
        raise database_not_found.DatabaseNotFoundException(
            os.environ["DATABASE"])

    return update_cost(flights, filters["date_from"], 0.75)
def get_hotels(country, city, filters, context):
    if filters["date_to"] - filters["date_from"] < timedelta(0):
        raise illegal_date.IllegalDateException(
            "from date can not be greater than to date")

    context.start("getHotelDataFromLocal")
    metadata = copy.deepcopy(get_hotel_data())
    context.stop()

    hotels_data = list(
        filter(lambda h: filter_city_hotels(h, country, city, filters),
               metadata))

    return update_cost(hotels_data, filters["date_from"])
def get_hotel_by_id(id, filters, context):
    if filters["date_to"] - filters["date_from"] < timedelta(0):
        raise illegal_date.IllegalDateException(
            "from date can not be greater than to date")

    context.start("getHotelDataFromLocal")
    data = get_hotel_data()
    context.stop()

    try:
        res = list(filter(lambda item: item["id"] == id, data))[0]
    except IndexError:
        raise item_not_found.ItemNotFoundException(id)

    multiplier = date_multiplier(filters["date_from"])
    res["cost"] = res["cost"] * multiplier
    res["dateFrom"] = filters["date_from"].strftime("%Y-%m-%d")
    res["dateTo"] = filters["date_to"].strftime("%Y-%m-%d")
    return res
Пример #5
0
def get_twostop(from_, to):
    """
    /**
    * GET /api/v1/flights/twostop/{from}/{to}
    * @description Get all two stop flight to destination
    * @pathParam from source airport id
    * @pathParam to destination airport id0
    * @queryParam {string} dateFrom - Date From
    * @queryParam {string} dateTo - Date To
    * @response 200 - OK
    * @response 400 - Error
    */
    """

    context.start("twostop", request)
    try:
        query_data = request.args
        try:
            date_from = get_query_param("dateFrom", query_data, parse_date)
            date_to = get_query_param("dateTo", query_data, parse_date)
        except Exception:
            raise illegal_date.IllegalDateException("needs a date")
        data = breaker.call(
            data_handler.get_twostop_flights,
            from_,
            to,
            {
                "date_from": date_from,
                "date_to": date_to
            },
            context,
        )
        status_code = 200
    except Exception as e:
        data = {"error": e.args[0]}
        status_code = 400
    finally:
        context.stop(status_code)
        return jsonify(data), status_code