Exemple #1
0
def get_identifier_source_id(requests, identifier_type):
    """
    Returns the ID of the identifier source to be used for generating
    values for identifiers of the given type.

    The idgen module doesn't offer an API to list identifier sources.
    This function scrapes /module/idgen/manageIdentifierSources.list
    """
    response = requests.get('/ws/rest/v1/patientidentifiertype/{}'.format(identifier_type))
    identifier_type_name = response.json()['name']

    response = requests.get('/module/idgen/manageIdentifierSources.list', headers={'Accept': 'text/html'})
    if not 200 <= response.status_code < 300:
        raise OpenmrsHtmlUiChanged(
            'Domain "{}": Unexpected response from OpenMRS idgen module at "{}". '
            'Is it installed?'.format(requests.domain_name, response.url)
        )

    tree = html.fromstring(response.content)
    for row in tree.xpath('//table[@id="sourceTable"]/tbody/tr'):
        ident_type, source_type, source_name, actions = row.xpath('td')
        if ident_type.text == identifier_type_name:
            try:
                onclick = actions.xpath('button')[1].attrib['onclick']
            except (AttributeError, IndexError, KeyError):
                raise OpenmrsHtmlUiChanged(
                    'Domain "{}": Unexpected page format at "{}".'.format(requests.domain_name, response.url)
                )
            match = re.match(r"document\.location\.href='viewIdentifierSource\.form\?source=(\d+)';", onclick)
            if not match:
                raise OpenmrsHtmlUiChanged(
                    'Domain "{}": Unexpected "onclick" value at "{}".'.format(requests.domain_name, response.url)
                )
            source_id = match.group(1)
            return source_id
Exemple #2
0
def get_identifier_source_id(requests, identifier_type):
    """
    Returns the ID of the identifier source to be used for generating
    values for identifiers of the given type.
    """
    # The idgen module doesn't offer an API to list identifier sources.
    # Log in using a requests Session, and scrape /module/idgen/manageIdentifierSources.list
    # (Urgh.)

    response = requests.get(
        '/ws/rest/v1/patientidentifiertype/{}'.format(identifier_type))
    identifier_type_name = response.json()['name']

    session = Session()
    login_data = {
        'uname': requests.username,
        'pw': requests.password,
        'submit': 'Log In',
        'redirect': '',
        'refererURL': '',
    }
    response = session.post(requests.get_url('/ms/legacyui/loginServlet'),
                            login_data,
                            verify=requests.verify)
    if not 200 <= response.status_code < 300:
        raise OpenmrsHtmlUiChanged(
            'Domain "{}": Unexpected OpenMRS login page at "{}".'.format(
                requests.domain_name, response.url))

    response = session.get(
        requests.get_url('/module/idgen/manageIdentifierSources.list'),
        verify=requests.verify)
    if not 200 <= response.status_code < 300:
        raise OpenmrsHtmlUiChanged(
            'Domain "{}": Unexpected response from OpenMRS idgen module at "{}". '
            'Is it installed?'.format(requests.domain_name, response.url))

    tree = html.fromstring(response.content)
    for row in tree.xpath('//table[@id="sourceTable"]/tbody/tr'):
        ident_type, source_type, source_name, actions = row.xpath('td')
        if ident_type.text == identifier_type_name:
            try:
                onclick = actions.xpath('button')[1].attrib['onclick']
            except (AttributeError, IndexError, KeyError):
                raise OpenmrsHtmlUiChanged(
                    'Domain "{}": Unexpected page format at "{}".'.format(
                        requests.domain_name, response.url))
            match = re.match(
                r"document\.location\.href='viewIdentifierSource\.form\?source=(\d+)';",
                onclick)
            if not match:
                raise OpenmrsHtmlUiChanged(
                    'Domain "{}": Unexpected "onclick" value at "{}".'.format(
                        requests.domain_name, response.url))
            source_id = match.group(1)
            return source_id
Exemple #3
0
def authenticate_session(requests):
    login_data = {
        'uname': requests.username,
        'pw': requests.password,
        'submit': 'Log In',
        'redirect': '',
        'refererURL': '',
    }
    response = requests.post('/ms/legacyui/loginServlet', login_data, headers={'Accept': 'text/html'})
    if not 200 <= response.status_code < 300:
        raise OpenmrsHtmlUiChanged('Domain "{}": Unexpected OpenMRS login page at "{}".'.format(
            requests.domain_name, response.url
        ))
Exemple #4
0
def authenticate_session(requests):
    if not isinstance(requests.auth_manager, BasicAuthManager):
        raise OpenmrsConfigurationError(
            f'OpenMRS server at {requests.base_url!r} needs to be configured '
            'for basic authentication.')
    login_data = {
        'uname': requests.auth_manager.username,
        'pw': requests.auth_manager.password,
        'submit': 'Log In',
        'redirect': '',
        'refererURL': '',
    }
    response = requests.post('/ms/legacyui/loginServlet',
                             login_data,
                             headers={'Accept': 'text/html'})
    if not 200 <= response.status_code < 300:
        raise OpenmrsHtmlUiChanged(
            f'Unexpected OpenMRS login page at {response.url!r}.')