コード例 #1
0
def put_an_org(org_element, user_cred, pk):
    internal_org_id = org_element.find('org_id').text
    user_cred.update(pk=pk)
    import pdb
    pdb.set_trace()
    try:
        organisation = Requester(
            method='put',
            url_template="http://{domain}/rest/v1/organisation/{pk}/",
            url_args=user_cred,
            headers={'content-type': 'application/xml', 'encoding': 'utf-8', 'Authorization': ' Token {}'.format(user_cred['api_key'])},
            data=etree.tostring(org_element),
            accept_codes=[HTTP_200_OK]
        )
    except Exception as e:
        return False, "{extra}", dict(
            internal_org_id=internal_org_id,
            event=ERROR_EXCEPTION,
            extra=str(e),
        )
    if organisation.response.status_code is HTTP_200_OK:
        return True, "Updated organisation ID: {pk}", dict(
            pk=organisation.response.json()['id'], event=ACTION_UPDATE_ORG,
        )
    else:
        return (
            False,
            "**** Error updating organisation: {pk}. HTTP status code: {extra}", dict(
                pk=pk,
                event=ERROR_UPLOAD_ORG,
                extra=organisation.response.status_code,
            )
        )
コード例 #2
0
def post_an_internal_id(user_cred, reporting_org_id, internal_identifier, pk):
    try:
        internal_org_id = Requester(
            method='post',
            url_template="http://{domain}/rest/v1/internal_organisation_id/",
            url_args=user_cred,
            headers={'content-type': 'application/json', 'encoding': 'utf-8', 'Authorization': 'Token {}'.format(user_cred['api_key'])},
            data=json.dumps(dict(
                recording_org=reporting_org_id,
                referenced_org=pk,
                identifier=internal_identifier,
            )),
            accept_codes=[HTTP_201_CREATED]
        )
    except Exception as e:
        return False, "{extra}", dict(
            pk,
            event=ERROR_EXCEPTION,
            extra=str(e),
        )
    if internal_org_id.response.status_code is HTTP_201_CREATED:
        import pdb
        pdb.set_trace()
        return True, "Created internal organisation ID: {identifier}", dict(
            pk=internal_org_id.response.json()['identifier'],
            event=ACTION_CREATE_IOI
        )
コード例 #3
0
def user_org(user_cred):
    try:
        profile = Requester(
            url_template="http://{domain}/api/{api_version}/user_profile/?"
            "format=json&api_key={api_key}&username={username}&user__username={username}",
            url_args=user_cred)
        # find the organisation ID in the path string, e.g. "/api/v1/organisation/42/"
        #non-intuitively split() returns an empty string first and last, thus [-2]
        return profile.response.json()['objects'][0]['organisation'].split(
            '/')[-2]
    except Exception, e:
        print "{message}".format(message=e.message)
        return False, None
コード例 #4
0
def post_an_org(org_element, user_cred):
    internal_org_id = org_element.find('org_id').text
    try:
        #iati_org_id = org_element.findall('iati_org_id')[0].text
        organisation = Requester(
            method='post',
            url_template="http://{domain}/rest/v1/organisation/",
            url_args=user_cred,
            headers={'content-type': 'application/xml', 'encoding': 'utf-8', 'Authorization': 'Token {}'.format(user_cred['api_key'])},
            data=etree.tostring(org_element),
            accept_codes=[HTTP_201_CREATED]
        )
    except Exception, e:
        return False, "{extra}", dict(
            internal_org_id=internal_org_id,
            event=ERROR_EXCEPTION,
            extra=e.message,
        )
コード例 #5
0
def api_user(domain, username, password='', api_key=''):
    user = dict(domain=domain, username=username, api_version=API_VERSION,)
    if api_key:
        user['api_key'] = api_key
        return user
    elif password:
        auth = Requester(
            method='post',
            url_template="http://{domain}/auth/token/",
            url_args=dict(domain=domain),
            data=dict(username=username, password=password),
        )
        xml = auth.response.text
        root = etree.fromstring(xml)
        user['api_key'] = root.find("api_key").text
        return user
    else:
        raise Exception("Either password or API key must be supplied")
コード例 #6
0
def post_an_internal_id(user_cred, reporting_org_id, internal_identifier, pk):
    try:
        internal_org_id = Requester(
            method='post',
            url_template="http://{domain}/rest/v1/internal_organisation_id/",
            url_args=user_cred,
            headers={'content-type': 'application/json', 'encoding': 'utf-8', 'Authorization': 'Token {}'.format(user_cred['api_key'])},
            data=json.dumps(dict(
                recording_org=reporting_org_id,
                referenced_org=pk,
                identifier=internal_identifier,
            )),
            accept_codes=[HTTP_201_CREATED]
        )
    except Exception, e:
        return False, "{extra}", dict(
            pk,
            event = ERROR_EXCEPTION,
            extra = e.message,
        )
コード例 #7
0
def find_org(user_cred, reporting_org_id, internal_org_id):
    """
    """
    url_args = user_cred
    url_args.update(
        recording_org=reporting_org_id,
        identifier=internal_org_id,
    )
    try:
        ioi = Requester(
            url_template="http://{domain}/rest/v1/internal_organisation_id/?"
                         "recording_org={recording_org}&identifier={identifier}&format=json",
            url_args=url_args,
            headers={
                'content-type': 'application/xml', 'encoding': 'utf-8',
                'Authorization': 'Token {}'.format(user_cred['api_key'])
            },
        )
        #TODO: check that we only get one object back
        org_id = ioi.response.json()[0]['referenced_org']
    except Exception, e:
        print "{message}".format(message=e.message)
        return False, None
コード例 #8
0
def put_an_org(org_element, user_cred, pk):
    internal_org_id = org_element.find('org_id').text
    user_cred.update(pk=pk)
    import pdb
    pdb.set_trace()
    try:
        organisation = Requester(
            method='put',
            url_template="http://{domain}/rest/v1/organisation/{pk}/",
            url_args=user_cred,
            headers={
                'content-type': 'application/xml',
                'encoding': 'utf-8',
                'Authorization': ' Token {}'.format(user_cred['api_key'])
            },
            data=etree.tostring(org_element),
            accept_codes=[HTTP_200_OK])
    except Exception, e:
        return False, "{extra}", dict(
            internal_org_id=internal_org_id,
            event=ERROR_EXCEPTION,
            extra=e.message,
        )