Beispiel #1
0
def isRequestArgsValid(request, validArgs, isFhir, argsRequired):
    request_copy = ImmutableMultiDict.copy(request.args)
    request_dict = {}
    required_arg = 0

    if isFhir and constants.FHIR_PRIMARY_ROLE in request_copy and constants.FHIR_ROLE not in request_copy:
        return False, constants.FHIR_PRIMARY_ROLE_WITHOUT_ROLE

    for arg in validArgs:
        list = request_copy.getlist(arg)
        if (len(list) > 1 and not isFhir):
            return False, str.format(constants.REPEATED_ARGUMENT, arg)
        elif isFhir and (len(list) > 1) and (arg is constants.FORMAT
                                             or arg is constants.FHIR_PAGE
                                             or arg is constants.FHIR_LIMIT
                                             or arg is constants.FHIR_SUMMARY):
            return False, str.format(constants.REPEATED_ARGUMENT, arg)
        elif (len(list) == 1 or (len(list) > 1 and isFhir)):
            request_dict[arg] = request_copy.pop(arg)
            if arg is not constants.OFFSET and arg is not constants.LIMIT and arg is not constants.FORMAT:
                required_arg += 1
    if (len(request_copy) > 0):
        return False, constants.UNKNOWN_REQUEST_ARG

    if argsRequired and required_arg < 1 and not isFhir:
        return False, constants.INSUFFICIANT_REQUIRED_ARGS

    return True, ""
Beispiel #2
0
def isFhirRequestArgsValid(request, validArgs):
    request_copy = ImmutableMultiDict.copy(request.args)

    valid = True
    errors_list = []

    if constants.FHIR_PRIMARY_ROLE in request_copy and constants.FHIR_ROLE not in request_copy:
        valid = False
        errors_list.append(constants.FHIR_PRIMARY_ROLE_WITHOUT_ROLE)

    for key in request_copy:
        if key not in validArgs:
            valid = False
            errors_list.append(
                str.format(constants.UNKNOWN_FHIR_REQUEST_ARG, key))
        if (key is constants.FORMAT or key is constants.FHIR_PAGE
                or key is constants.FHIR_LIMIT
                or key is constants.FHIR_SUMMARY):
            list = request_copy.getlist(key)
            if len(list) > 1:
                valid = False
                errors_list.append(str.format(constants.REPEATED_ARGUMENT,
                                              key))

    if valid:
        return True, []
    else:
        return False, errors_list
def get_fhir_organisations(request, format, request_id):
    log_utils.log_layer_entry(constants.SERVICE, request_id )

    request_args = ImmutableMultiDict.copy(request.args)

    limit, page = get_pagination_parameters(request_args)

    ods_codes_dict, total_record_count, returned_record_count = model_entry.get_fhir_ods_codes(
        request_args, page, limit, request_id)

    list_of_org_dicts = []
    for ods_code in ods_codes_dict:
        org, address, role, contact, relationship, successor = model_entry.get_org_data(ods_code['odscode'], request_id,
                                                                                        is_fhir=True)
        org_dict = service_utils.build_org(org, address, role, contact, relationship, successor, is_fhir=True)
        list_of_org_dicts.append(org_dict)

    structured_bundle_dict = translation_entry.structure_fhir_organisations_bundle(list_of_org_dicts,
                                                                                   total_record_count, limit, page,
                                                                                   request, request_id)

    response_body = serialisation_entry.create_response_body(structured_bundle_dict, format, request_id, is_fhir=True)

    log_utils.log_layer_exit(constants.SERVICE, request_id)

    return response_body, returned_record_count, total_record_count
Beispiel #4
0
    def manual_parse(self, view, req, **mainkwargs):
        ''' Manually parse the args

        Manually parses and validates the input arguements of a Flask route

        Parameters:
            view:
                The instance of the Flask View
            req:
                The Flask request object
            use_params (str|list):
                one (or list) of the param keys defining which sets of parameters to consider
                for validation
            required (str|list):
                string or list of string parameter names that are required as input
            makemulti (bool):
                If True, converts the arguments dictionary into an ImmutableMultiDict object

        Returns:
            A dictionary of arguments if all passed validation or an error dictionary specifying
            any missing parameters, or parameters that failed validation.  There errors are already
            caught properly in the Marvin system.

        Example:
            >>> # to validate on the Galaxy View page, and check the galaxy request parameters
            >>> # and require that the plateifu parameter is set
            >>> args = av.manual_parse(self, request, use_params='galaxy', required='plateifu')

        '''
        self._get_release_endpoint(view)
        url = self._get_url()
        self._check_mainkwargs(**mainkwargs)
        self.create_args()
        self._pop_kwargs(**mainkwargs)
        newargs = parser.parse(self.final_args,
                               req,
                               force_all=True,
                               **self._main_kwargs)

        # see if we make it a multidict
        makemulti = mainkwargs.get('makemulti', None)
        if makemulti:
            from werkzeug.datastructures import ImmutableMultiDict
            newargs = ImmutableMultiDict(newargs.copy())
        return newargs
Beispiel #5
0
    def manual_parse(self, view, req, **mainkwargs):
        ''' Manually parse the args '''
        # args = parser.parse(user_args, request)
        self._get_release_endpoint(view)
        url = self._get_url()
        self._check_mainkwargs(**mainkwargs)
        self.create_args()
        self._pop_kwargs(**mainkwargs)
        newargs = parser.parse(self.final_args,
                               req,
                               force_all=True,
                               **self._main_kwargs)

        # see if we make it a multidict
        makemulti = mainkwargs.get('makemulti', None)
        if makemulti:
            from werkzeug.datastructures import ImmutableMultiDict
            newargs = ImmutableMultiDict(newargs.copy())
        return newargs
Beispiel #6
0
def get_ord_organisations_changed_since(request_args, format, app_hostname,
                                        request_id):
    log_utils.log_layer_entry(constants.SERVICE, request_id)

    request_args = ImmutableMultiDict.copy(request_args)

    ods_codes = model_entry.get_ord_ods_codes_last_changed_since(
        request_args, request_id)

    total_record_count = len(ods_codes)

    structured_last_changed_since_summary = translation_entry.build_ord_last_changed_summary(
        ods_codes, format, app_hostname)

    response_body = serialisation_entry.create_response_body(
        structured_last_changed_since_summary,
        format,
        request_id,
        is_fhir=False)

    log_utils.log_layer_exit(constants.SERVICE, request_id)

    return response_body, total_record_count
Beispiel #7
0
def get_organisations():
    """
    Endpoint returning a list of ODS organisations
    ---
    parameters:
      - name: Name
        description: Filters results by names which contain the specified string
        in: query
        type: string
        required: false
      - name: primaryRoleCode
        description: Filters results to only those with one of the specified role codes assigned as a Primary role.
          Ignored if used alongside roleCode parameter.
        in: query
        type: array
        collectionFormat: csv
        required: false
      - name: lastUpdatedSince
        description: Filters results to only those with a lastChangeDate after the specified date.
        in: query
        type: string
        format: date
        required: false
    responses:
      200:
        description: A filtered list of organisation resources
    """
    request_id = log_utils.get_request_id(request)
    log_utils.log_handler_entry(request_id, request)

    format, error_description = handler_utils.determine_format(request,
                                                               is_fhir=False)
    if error_description:
        handler_utils.ord_error_log_and_abort(406, error_description,
                                              request_id)

    valid, description = handler_utils.isValidOrganisationAPIRequest(request)
    if not valid:
        handler_utils.ord_error_log_and_abort(406, description, request_id)

    request_args = ImmutableMultiDict.copy(request.args)
    route = request.base_url

    try:
        resp_data, returned_record_count, total_record_count = \
            ord_organisations_service.get_ord_organisations_summary(request, format, route, request_id)
    except (exception.ServiceError, exception.InvalidDataError):
        handler_utils.ord_error_log_and_abort(500, "Service Unavailable",
                                              request_id)
    except:
        handler_utils.ord_error_log_and_abort(404, "Unknown Error", request_id)

    response = Response(resp_data)

    handler_utils.add_content_type_header_to_response(format, response)

    response.headers['X-Total-Count'] = total_record_count
    response.headers['Access-Control-Expose-Headers'] = 'X-Total-Count'

    if returned_record_count != total_record_count:
        response = create_pagination_headers(request_args, response,
                                             returned_record_count, route,
                                             total_record_count)

    log_utils.log_handler_exit(request_id, response.status, constants.SUCCESS)

    return response