예제 #1
0
파일: utils.py 프로젝트: oakey-b1/refstack
def parse_input_params(expected_input_params):
    """Parse input parameters from request.

    :param expected_input_params: (array) Expected input
                                  params specified in constants.
    """
    raw_filters = _get_input_params_from_request(expected_input_params)
    filters = copy.deepcopy(raw_filters)
    date_fmt = CONF.api.input_date_format

    for key, value in filters.items():
        if key == const.START_DATE or key == const.END_DATE:
            try:
                filters[key] = timeutils.parse_strtime(value, date_fmt)
            except (ValueError, TypeError) as exc:
                raise api_exc.ParseInputsError('Invalid date format: %(exc)s' %
                                               {'exc': exc})

    start_date = filters.get(const.START_DATE)
    end_date = filters.get(const.END_DATE)
    if start_date and end_date:
        if start_date > end_date:
            raise api_exc.ParseInputsError(
                'Invalid dates: %(start)s more than %(end)s'
                '' % {
                    'start': const.START_DATE,
                    'end': const.END_DATE
                })
    if const.SIGNED in filters:
        if is_authenticated():
            filters[const.OPENID] = get_user_id()
        else:
            raise api_exc.ParseInputsError(
                'To see signed test results you need to authenticate')
    return filters
예제 #2
0
파일: utils.py 프로젝트: oakey-b1/refstack
def get_page_number(records_count):
    """Get page number from request.

    :param records_count: (int) total records count.
    """
    page_number = pecan.request.GET.get(const.PAGE)
    per_page = CONF.api.results_per_page

    total_pages = _calculate_pages_number(per_page, records_count)
    # The first page exists in any case
    if page_number is None:
        return (1, total_pages)
    try:
        page_number = int(page_number)
    except (ValueError, TypeError):
        raise api_exc.ParseInputsError(
            'Invalid page number: The page number can not be converted to '
            'an integer')

    if page_number == 1:
        return (page_number, total_pages)

    if page_number <= 0:
        raise api_exc.ParseInputsError('Invalid page number: '
                                       'The page number less or equal zero.')

    if page_number > total_pages:
        raise api_exc.ParseInputsError(
            'Invalid page number: '
            'The page number is greater than the total number of pages.')

    return (page_number, total_pages)
예제 #3
0
 def test_get_failed_in_get_test_records_number(self, parse_inputs,
                                                db_get_count):
     db_get_count.side_effect = api_exc.ParseInputsError()
     self.assertRaises(api_exc.ParseInputsError, self.controller.get)
예제 #4
0
    def test_get_failed_in_get_page_number(self, get_page, parse_input,
                                           db_get_count):

        get_page.side_effect = api_exc.ParseInputsError()
        self.assertRaises(api_exc.ParseInputsError, self.controller.get)
예제 #5
0
    def test_get_failed_in_parse_input_params(self, parse_inputs):

        parse_inputs.side_effect = api_exc.ParseInputsError()
        self.assertRaises(api_exc.ParseInputsError, self.controller.get)