Ejemplo n.º 1
0
    def handle(self, *args, **kwargs):
        context_id = kwargs['context_id']
        collection_id = kwargs['collection_id']
        platform_name = kwargs['platform_name']
        userid_list = None
        username_list = None
        if kwargs['userid_list']:
            userid_list = kwargs['userid_list'].strip().split(',')
        if kwargs['username_list']:
            username_list = kwargs['username_list'].strip().split(',')

        # search by params
        qset = CRUD.select_annos(context_id=context_id,
                                 collection_id=collection_id,
                                 platform_name=platform_name,
                                 userid_list=userid_list,
                                 username_list=username_list,
                                 is_copy=False)  # return replies and deleted

        # serialize results as in api search
        resp = []
        for a in qset:
            catcha = a.serialized
            if a.anno_deleted:  # hack! have to flag it's a deleted
                catcha['platform']['deleted'] = True
            resp.append(catcha)

        print(json.dumps(resp, indent=4))
Ejemplo n.º 2
0
def test_copy_except_deleted_and_reply(wa_list):
    # insert a reply
    wa_list.append(make_wa_object(
        age_in_hours=8, reply_to=wa_list[0]['id']))
    # add a deleted
    wa_list[1]['platform']['deleted'] = True
    original_total = len(wa_list)

    # import catcha list
    import_resp = CRUD.import_annos(wa_list)
    assert int(import_resp['original_total']) == original_total
    assert int(import_resp['total_success']) == original_total
    assert int(import_resp['total_failed']) == 0

    anno_list = CRUD.select_annos(
            context_id=wa_list[0]['platform']['context_id'],
            collection_id=wa_list[0]['platform']['collection_id'],
            platform_name=wa_list[0]['platform']['platform_name']
            )

    select_total = len(anno_list)
    for x in anno_list:
        print('search returned ({})'.format(x.anno_id))

    # discount the deleted and reply
    assert select_total == (original_total - 2)

    copy_resp = CRUD.copy_annos(
            anno_list,
            'another_fake_context',
            'collection_x')
    assert int(copy_resp['original_total']) == (original_total - 2)
    assert int(copy_resp['total_success']) == (original_total - 2)
    assert int(copy_resp['total_failed']) == 0
Ejemplo n.º 3
0
def test_copy_ok(wa_list):
    original_total = len(wa_list)

    # import catcha list
    import_resp = CRUD.import_annos(wa_list)
    assert int(import_resp['original_total']) == original_total
    assert int(import_resp['total_success']) == original_total
    assert int(import_resp['total_failed']) == 0

    anno_list = CRUD.select_annos(
            context_id=wa_list[0]['platform']['context_id'],
            collection_id=wa_list[0]['platform']['collection_id'],
            platform_name=wa_list[0]['platform']['platform_name'],
            )

    select_total = len(anno_list)
    assert select_total == original_total

    copy_resp = CRUD.copy_annos(
            anno_list,
            'another_fake_context',
            'collection_x')
    assert int(copy_resp['original_total']) == original_total
    assert int(copy_resp['total_success']) == original_total
    assert int(copy_resp['total_failed']) == 0
Ejemplo n.º 4
0
def test_copy_back_compat(wa_list):
    original_total = len(wa_list)

    # import catcha list
    import_resp = CRUD.import_annos(wa_list)
    assert int(import_resp['original_total']) == original_total
    assert int(import_resp['total_success']) == original_total
    assert int(import_resp['total_failed']) == 0

    anno_list = CRUD.select_annos(
        context_id='fake_context',
        collection_id='fake_collection',
        platform_name=CATCH_DEFAULT_PLATFORM_NAME,
    )
    select_total = anno_list.count()
    assert select_total == original_total

    # setup copy call to client
    params = {
        'platform_name': CATCH_DEFAULT_PLATFORM_NAME,
        'source_context_id': 'fake_context',
        'source_collection_id': 'fake_collection',
        'target_context_id': 'another_fake_context',
        'target_collection_id': 'another_fake_collection',
        'back_compat': True,
    }
    c = Consumer._default_manager.create()
    payload = make_jwt_payload(apikey=c.consumer,
                               user='******',
                               override=['CAN_COPY'])
    token = make_encoded_token(c.secret_key, payload)

    client = Client()
    copy_url = reverse('copy_api')
    response = client.post(copy_url,
                           data=json.dumps(params),
                           HTTP_X_ANNOTATOR_AUTH_TOKEN=token,
                           content_type='application/json')

    assert response.status_code == 200
    resp = json.loads(response.content.decode('utf-8'))
    assert int(resp['original_total']) == original_total
    assert int(resp['total_success']) == original_total
    assert int(resp['total_failed']) == 0

    # search via back-compat api
    compat_search_url = reverse('compat_search')
    response = client.get(compat_search_url,
                          data={'context_id': 'another_fake_context'},
                          HTTP_X_ANNOTATOR_AUTH_TOKEN=token)

    assert response.status_code == 200
    content = json.loads(response.content.decode('utf-8'))
    assert int(content['total']) == original_total
    assert int(content['size_failed']) == 0
Ejemplo n.º 5
0
def test_remove_in_2step(wa_list):
    # insert a reply
    wa_list.append(make_wa_object(
        age_in_hours=8, reply_to=wa_list[0]['id']))
    # add a deleted
    wa_list[1]['platform']['deleted'] = True
    original_total = len(wa_list)

    # import catcha list
    import_resp = CRUD.import_annos(wa_list)
    assert int(import_resp['original_total']) == original_total
    assert int(import_resp['total_success']) == original_total
    assert int(import_resp['total_failed']) == 0

    # delete annotations (not all soft-deleted)
    delete_resp = CRUD.delete_annos(
            context_id=wa_list[1]['platform']['context_id'])
    assert int(delete_resp['failed']) == 0
    # discount the deleted and reply
    assert int(delete_resp['succeeded']) == (original_total -2)

    anno_list = CRUD.select_annos(
            context_id=wa_list[0]['platform']['context_id'],
            is_copy=False
            )
    # didn't true-delete anything yet
    assert len(anno_list) == original_total

    # delete annotations (true-delete)
    delete2_resp = CRUD.delete_annos(
            context_id=wa_list[1]['platform']['context_id'])
    assert int(delete2_resp['failed']) == 0
    assert int(delete2_resp['succeeded']) == original_total

    anno_list = CRUD.select_annos(
            context_id=wa_list[0]['platform']['context_id'],
            is_copy=False
            )
    # true-delete all annotations
    assert len(anno_list) == 0
Ejemplo n.º 6
0
    def handle(self, *args, **kwargs):
        filepath = kwargs['filepath']
        source_context_id = kwargs['source_context_id']
        target_context_id = kwargs['target_context_id']
        platform_name = kwargs['platform_name']
        userid_list = None
        username_list = None
        if kwargs['userid_list']:
            userid_list = kwargs['userid_list'].strip().split(',')
        if kwargs['username_list']:
            username_list = kwargs['username_list'].strip().split(',')
        if kwargs['start_datetime_iso']:
           start_datetime = dateutil.parser.isoparse(kwargs['start_datetime_iso'])

        with open(filepath, 'r') as f:
            collection_map = json.load(f)

        results = []
        # TODO: not testing for repeated collection_id in input.
        for collection_row in collection_map:
            selected = CRUD.select_annos(
                    context_id=source_context_id,
                    collection_id=collection_row[0],
                    platform_name=platform_name,
                    userid_list=userid_list,
                    username_list=username_list,
                    start_datetime=start_datetime,
                    is_copy=True)  # do NOT return replies and deleted

            copy_result = CRUD.copy_annos_with_replies(
                    anno_list=selected,
                    target_context_id=target_context_id,
                    target_collection_id=collection_row[1],
                    )

            results.append({
                    'source_context_id': source_context_id,
                    'target_context_id': target_context_id,
                    'source_collection_id': collection_row[0],
                    'target_collection_id': collection_row[1],
                    'copy_result': copy_result
                    })

        print(json.dumps(results, indent=4))