Exemplo n.º 1
0
def get_or_create(item_name, extra_info, get_url, create_url, data):

    response = Request.get(get_url)

    if response.status_code != 200:
        message = "GET error ({}): {}\n{}".format(response.status_code, get_url, response.text)
        raise ValueError(message)

    pks = [item["id"] for item in response.json()]

    if pks == []:
        response = Request.post(create_url, data)

        if response.status_code != 201:
            message = "Create POST error ({}): {}\n{}\n{}".format(response.status_code, create_url, data, response.text)
            raise ValueError(message)

        pk = response.json()["id"]

        if verbose:
            print("Created {} ({}, pk={})".format(item_name, extra_info, pk))

        return pk, True
    else:
        pk = pks[0]

        if len(pks) > 1:
            message = "Found multiple {} instances ({}) for {}; using pk={}".format(item_name, pks, extra_info, pk)
            warnings.warn(message)

        if verbose:
            print("Existing {} ({}, pk={})".format(item_name, extra_info, pk))

        return pk, False
Exemplo n.º 2
0
class TestRequest(TestCase):
    @classmethod
    def setUp(self):
        self.request = Request()

    def test_get_requests_of_projects(self):
        r = self.request.get('projects')
        self.assertEqual(r.status_code, 200)
Exemplo n.º 3
0
class TestRequest(TestCase):

    @classmethod
    def setUp(self):
        self.request = Request()

    def test_get_requests_of_projects(self):
        r = self.request.get('projects')
        self.assertEqual(r.status_code, 200)
Exemplo n.º 4
0
def get_or_create(item_name, extra_info, get_url, create_url, data):

    response = Request.get(get_url)

    if response.status_code != 200:
        message = "GET error ({}): {}\n{}".format(response.status_code,
                                                  get_url, response.text)
        raise ValueError(message)

    pks = [item["id"] for item in response.json()]

    if pks == []:
        response = Request.post(create_url, data)

        if response.status_code != 201:
            message = "Create POST error ({}): {}\n{}\n{}".format(
                response.status_code, create_url, data, response.text)
            raise ValueError(message)

        pk = response.json()["id"]

        if verbose:
            print("Created {} ({}, pk={})".format(item_name, extra_info, pk))

        return pk, True
    else:
        pk = pks[0]

        if len(pks) > 1:
            message = (
                "Found multiple {} instances ({}) for {}; using pk={}".format(
                    item_name, pks, extra_info, pk))
            warnings.warn(message)

        if verbose:
            print("Existing {} ({}, pk={})".format(item_name, extra_info, pk))

        return pk, False