コード例 #1
0
    def get():
        name = get_query_param_str('name')
        location = get_query_param_str('location')
        entity_type = get_query_param_str('entity_type_cd')
        request_action = get_query_param_str('request_action_cd')

        service = None

        if not validate_name_request(location, entity_type, request_action):
            return  # TODO: Return invalid response! What is it?

        valid_location = location in [
            ValidLocations.CA_NOT_BC.value, ValidLocations.INTL.value
        ]
        valid_entity_type = entity_type in XproUnprotectedNameEntityTypes.list(
        )

        try:
            if valid_location and valid_entity_type and request_action in (
                    AnalysisRequestActions.NEW.value,
                    AnalysisRequestActions.AML.value,
                    AnalysisRequestActions.CHG.value,
                    AnalysisRequestActions.ASSUMED.value,
                    AnalysisRequestActions.REN.value,
                    AnalysisRequestActions.REH.value):

                # Use UnprotectedNameAnalysisService
                service = XproNameAnalysisService()
                builder = NameAnalysisBuilder(service)

            else:
                raise Exception('Invalid scenario')

            if not service:
                raise ValueError('Invalid service provided')
            if not builder:
                raise ValueError('Invalid builder provided')

            # Register and initialize the builder
            service.use_builder(builder)  # Required step! TODO: Enforce this!
            service.set_entity_type(
                entity_type)  # Required step! TODO: Enforce this!
            service.set_name(name)  # Required step! TODO: Enforce this!

        except Exception as err:
            print('Error initializing XproNameAnalysis service: ' +
                  repr(err.with_traceback(None)))
            raise

        # Perform the name analysis - execute analysis using the supplied builder
        # @:return an array of ProcedureResult[]
        analysis = service.execute_analysis()

        # Build the appropriate response for the analysis result
        analysis_response = AnalysisResponse(service, analysis)
        payload = analysis_response.build_response().to_json()

        response = make_response(payload, 200)
        return response
コード例 #2
0
ファイル: name_requests.py プロジェクト: riyazuddinsyed/namex
    def get(self):
        try:
            filters = []

            # Validate the request
            if len(request.args) == 0:
                raise InvalidInputError(
                    message='No query parameters were specified in the request'
                )

            nr_num_query_str = get_query_param_str('nrNum')
            email_address_query_str = get_query_param_str('emailAddress')
            phone_number_query_str = get_query_param_str('phoneNumber')

            if not nr_num_query_str:
                raise InvalidInputError(message='An nrNum must be provided')
            else:
                if not email_address_query_str and not phone_number_query_str:
                    raise InvalidInputError(
                        message=
                        'Either an emailAddress or phoneNumber must be provided'
                    )

            # Continue
            nr_num = parse_nr_num(nr_num_query_str)
            email_address = email_address_query_str

            phone_number = get_query_param_str('phoneNumber')
            # Filter on addresses
            # address_line = get_query_param_str('addrLine1')

            if nr_num:
                filters.append(func.lower(Request.nrNum) == nr_num.lower())
            if phone_number:
                strip_phone_number_chars_regex = r"[^0-9]"
                filters.append(
                    Request.applicants.any(
                        func.regexp_replace(
                            Applicant.phoneNumber,
                            strip_phone_number_chars_regex, '', 'g').contains(
                                re.sub(strip_phone_number_chars_regex, '',
                                       phone_number))))

            if email_address:
                filters.append(
                    Request.applicants.any(
                        func.lower(Applicant.emailAddress).startswith(
                            email_address.lower())))
            '''
            Filter on addresses
            if address_line:
                filters.append(
                    Request.applicants.any(
                        func.lower(Applicant.addrLine1).startswith(address_line.lower())
                    )
                )
            '''

            criteria = RequestQueryCriteria(nr_num=nr_num, filters=filters)

            results = Request.find_by_criteria(criteria)

            if not results:
                results = []

        except InvalidInputError as err:
            return handle_exception(err, err.message, 400)
        except Exception as err:
            return handle_exception(err,
                                    'Error retrieving the NR from the db.',
                                    500)

        if nr_num and len(results) == 1:
            nr_model = results[0]

            if nr_model.requestTypeCd and (not nr_model.entity_type_cd
                                           or not nr_model.request_action_cd):
                # If requestTypeCd is set, but a request_entity (entity_type_cd) and a request_action (request_action_cd)
                # are not, use get_mapped_entity_and_action_code to map the values from the requestTypeCd
                entity_type, request_action = get_mapped_entity_and_action_code(
                    nr_model.requestTypeCd)
                nr_model.entity_type_cd = entity_type
                nr_model.request_action_cd = request_action

            response_data = nr_model.json()
            # Add the list of valid Name Request actions for the given state to the response
            response_data['actions'] = get_nr_state_actions(
                results[0].stateCd, results[0])
            return jsonify(response_data), 200
        elif len(results) > 0:
            # We won't add the list of valid Name Request actions for the given state to the response if we're sending back a list
            # If the user / client accessing this data needs the Name Request actions, GET the individual record using NameRequest.get
            # This method, NameRequests.get is for Existing NR Search
            return jsonify(list(map(lambda result: result.json(),
                                    results))), 200

        # We won't add the list of valid Name Request actions for the given state to the response if we're sending back a list
        # If the user / client accessing this data needs the Name Request actions, GET the individual record using NameRequest.get
        # This method, NameRequests.get is for Existing NR Search
        return jsonify(results), 200
コード例 #3
0
    def get():
        """Get structure analysis for a name."""
        name = get_query_param_str('name')
        allowed_special_chars = [
            '/', '[', ']', '^', '*', '+', '=', '&', '(', ')', ',', '”', '’',
            '#', '@', '!', '?', ';', ':'
        ]
        for special_char in allowed_special_chars:
            name = name.replace(special_char, ' ')
        location = get_query_param_str('location')
        entity_type = get_query_param_str('entity_type_cd')
        request_action = get_query_param_str('request_action_cd')
        designation_only = get_query_param_str('analysis_type') in [
            'designation', None
        ]
        # TODO: take out xpro flow entirely (not used in name analysis flow anymore)
        xpro = get_query_param_str('jurisdiction') not in ['BC', None]
        if xpro:
            return jsonify(
                message=['xpro not supported']), HTTPStatus.NOT_IMPLEMENTED
        service = None

        errors = bc_validate_name_request(location, entity_type,
                                          request_action)  # \
        # if not xpro else xpro_validate_name_request(location, entity_type, request_action)
        if errors:
            return jsonify(message=errors), HTTPStatus.BAD_REQUEST

        # # used for BC
        # valid_protected_entity_type = None
        # valid_unprotected_entity_type = None
        # is_protected_action = None
        # is_unprotected_action = None
        # # used for XPRO
        # valid_xpro_action = None
        # # used for both
        # valid_location = None
        # valid_entity_type = None
        # if xpro:
        #     valid_location = location in [ValidLocations.CA_NOT_BC.value, ValidLocations.INTL.value]
        #     valid_entity_type = entity_type in XproUnprotectedNameEntityTypes.list()
        #     valid_xpro_action = request_action in [
        #         AnalysisRequestActions.NEW.value,
        #         AnalysisRequestActions.AML.value,
        #         AnalysisRequestActions.CHG.value,
        #         AnalysisRequestActions.ASSUMED.value,
        #         AnalysisRequestActions.REN.value,
        #         AnalysisRequestActions.REH.value,
        #         AnalysisRequestActions.MVE.value
        #     ]
        # else:
        #     valid_location = location == ValidLocations.CA_BC.value
        #     valid_protected_entity_type = entity_type in BCProtectedNameEntityTypes.list()
        #     valid_unprotected_entity_type = entity_type in BCUnprotectedNameEntityTypes.list()
        #     is_protected_action = request_action in [
        #         AnalysisRequestActions.NEW.value,
        #         AnalysisRequestActions.CHG.value,
        #         AnalysisRequestActions.MVE.value
        #     ]
        #     is_unprotected_action = request_action in [AnalysisRequestActions.NEW.value]

        try:
            # if xpro:
            #     if valid_location and valid_entity_type and valid_xpro_action:
            #         service = ProtectedNameAnalysisService() \
            #             if request_action == AnalysisRequestActions.MVE.value else XproNameAnalysisService()
            #         builder = NameAnalysisBuilder(service)
            #     else:
            #         return jsonify(message=['Invalid scenario']), HTTPStatus.BAD_REQUEST

            # else:
            # if valid_location and valid_protected_entity_type:  # and is_protected_action:
            #  # Use ProtectedNameAnalysisService
            service = ProtectedNameAnalysisService()
            builder = NameAnalysisBuilder(service)
            # else:  # valid_location and valid_unprotected_entity_type:  # and is_unprotected_action:
            # Use UnprotectedNameAnalysisService
            # service = UnprotectedNameAnalysisService()
            # builder = NameAnalysisBuilder(service)
            # else:
            #     return jsonify(message=['Invalid scenario']), HTTPStatus.BAD_REQUEST

            if not service:
                return jsonify(message=['Failed to initialize service'
                                        ]), HTTPStatus.INTERNAL_SERVER_ERROR
            if not builder:
                return jsonify(message=['Failed to initialize builder'
                                        ]), HTTPStatus.INTERNAL_SERVER_ERROR

            # Register and initialize the builder
            service.use_builder(builder)  # Required step! TODO: Enforce this!
            service.set_entity_type(
                entity_type)  # Required step! TODO: Enforce this!
            service.set_name(
                name, designation_only)  # Required step! TODO: Enforce this!

        except Exception as err:
            current_app.logger.error(
                'Error initializing NameAnalysis service: ' +
                repr(err.with_traceback(None)))
            raise

        # Perform the name analysis - execute analysis using the supplied builder
        # @:return an array of ProcedureResult[]
        analysis = service.execute_analysis(designation_only)

        # Build the appropriate response for the analysis result
        analysis_response = BcAnalysisResponse(service, analysis)  # \
        # if not xpro else XproAnalysisResponse(service, analysis)

        # Remove issues for end designation more than once if they are not duplicates
        valid_issues = []
        for issue in analysis_response.issues:
            if issue.issue_type == AnalysisIssueCodes.END_DESIGNATION_MORE_THAN_ONCE:
                valid_name_actions = []
                seen_designations = []
                for name_action in issue.name_actions:
                    if name_action.word in seen_designations:
                        valid_name_actions.append(name_action)
                    else:
                        seen_designations.append(name_action.word)
                if len(valid_name_actions) > 0:
                    issue.name_actions = valid_name_actions
                    valid_issues.append(issue)
                # else ^ this issue will not be added to the response
            else:
                valid_issues.append(issue)

        analysis_response.issues = valid_issues
        payload = analysis_response.build_response().to_json()
        response = make_response(payload, HTTPStatus.OK)
        return response