Exemplo n.º 1
0
def study(admin_client, transactional_db):
    study = {
        'kf_id': 'SD_00000001',
        'name': 'Test Study',
        'visible': True,
    }
    # Study cannot be created through api, so it must be made with ORM
    study = Study(**study)
    study.save()
    return study
Exemplo n.º 2
0
def test_list_all_tasks_permissions(db, test_client, user_type, expected):
    """
    ADMIN - Can query all tasks
    DEV - Can query all tasks
    USER - Can query tasks from published releases, or releases that they
           have a study in.
    anonomous - Can query tasks from published releases
    """
    study = Study(kf_id="SD_00000001")
    study.save()

    releases = ReleaseFactory.create_batch(10, state="staged")
    releases = ReleaseFactory.create_batch(10, state="published")
    releases = ReleaseFactory.create_batch(
        10, state="staging", studies=[study]
    )

    client = test_client(user_type)
    resp = client.post("/graphql", data={"query": ALL_TASKS})
    # Test that the correct number of releases are returned
    assert len(resp.json()["data"]["allTasks"]["edges"]) == expected()
def test_list_all_tasks_permissions(db, test_client, user_type, expected):
    """
    ADMIN - Can query all task services
    DEV - Can query all task services
    USER - Cannot query any task services
    anonomous - Cannot query any task services

    The TaskServiceFactory maxes at 3 unique task services, so we should never
    expect more than than.
    """
    study = Study(kf_id="SD_00000001")
    study.save()

    releases = ReleaseFactory.create_batch(10, state="staged")
    releases = ReleaseFactory.create_batch(10, state="published")
    releases = ReleaseFactory.create_batch(10,
                                           state="staging",
                                           studies=[study])

    client = test_client(user_type)
    resp = client.post("/graphql", data={"query": ALL_TASK_SERVICES})
    # Test that the correct number of releases are returned
    assert len(resp.json()["data"]["allTaskServices"]["edges"]) == expected()
Exemplo n.º 4
0
def sync():
    if not settings.DATASERVICE_URL:
        return [], []

    resp = requests.get(settings.DATASERVICE_URL + "/studies?limit=100")
    resp.raise_for_status()

    studies = Study.objects.all()
    new = []
    deleted = []

    for study in resp.json()["results"]:
        try:
            s = Study.objects.get(kf_id=study["kf_id"])
        # We don't know about the study, create it
        except Study.DoesNotExist:
            s = Study(
                kf_id=study["kf_id"],
                name=study["name"],
                visible=study["visible"],
                created_at=study["created_at"],
            )
            s.save()
            new.append(s)
            continue

        # Check for updated fields
        for field in ["name", "visible"]:
            if getattr(s, field) != study[field]:
                setattr(s, field, study[field])
        s.save()

    # Check if any studies were deleted from the dataservice
    coord_studies = set(s.kf_id for s in studies)
    ds_studies = set(s["kf_id"] for s in resp.json()["results"])
    missing_studies = coord_studies - ds_studies
    deleted = list(missing_studies)
    for study in missing_studies:
        s = Study.objects.get(kf_id=study)
        s.deleted = True
        s.save()

    return new, deleted
Exemplo n.º 5
0
    def sync(self, request):
        """
        Synchronize studies with the dataservice

        Currently done within a request with the intent that the user sync
        manually from the ui and recieve fresh studies on the response

        If the process for syncing studies becomes automated, then this
        may be moved to a task
        """
        if not settings.DATASERVICE_URL:
            return

        resp = requests.get(settings.DATASERVICE_URL + '/studies?limit=100')
        if resp.status_code != 200:
            message = 'There was an error getting studies from the dataservice'
            if resp.json() and 'message' in resp.json():
                message = resp.json()['message']
            return Response({
                'status': 'error',
                'message': f'{message}'
            }, resp.status_code)

        studies = Study.objects.all()
        new = 0
        deleted = 0

        for study in resp.json()['results']:
            try:
                s = Study.objects.get(kf_id=study['kf_id'])
            # We don't know about the study, create it
            except Study.DoesNotExist:
                s = Study(kf_id=study['kf_id'],
                          name=study['name'],
                          visible=study['visible'],
                          created_at=study['created_at'])
                s.save()
                new += 1
                continue

            # Check for updated fields
            for field in ['name', 'visible']:
                if getattr(s, field) != study[field]:
                    setattr(s, field, study[field])
            s.save()

        # Check if any studies were deleted from the dataservice
        coord_studies = set(s.kf_id for s in studies)
        ds_studies = set(s['kf_id'] for s in resp.json()['results'])
        missing_studies = coord_studies - ds_studies
        deleted = len(missing_studies)
        for study in missing_studies:
            s = Study.objects.get(kf_id=study)
            s.deleted = True
            s.save()

        return Response(
            {
                'status': 'ok',
                'new': new,
                'deleted': deleted,
                'message': f'Synchronized with dataservice'
            }, 200)
Exemplo n.º 6
0
def study(admin_client, transactional_db):
    study = {"kf_id": "SD_00000001", "name": "Test Study", "visible": True}
    # Study cannot be created through api, so it must be made with ORM
    study = Study(**study)
    study.save()
    return study