def test_closest_value(self):
     with rac_vcr.use_cassette("test_closest_value.json"):
         repository = ASpace(
             baseurl="http://localhost:8089").repositories(2)
         archival_object = repository.archival_objects(7)
         value = data_helpers.closest_value(archival_object, "extents")
         self.assertTrue(len(value) > 0)
Exemple #2
0
def test_fetch():
    aspace = ASpace()
    assert isinstance(aspace.repositories, JSONModelRelation)
    resolved = list(aspace.repositories)
    assert resolved[0].jsonmodel_type == "repository"
    repo_id = resolved[0].uri.split("/")[-1]
    assert isinstance(aspace.repositories(repo_id), JSONModelObject)
def test_object_locations():
    """Checks whether the function returns a list of dicts."""
    client = ASpace().client
    locations = utils.get_object_locations("/repositories/2/archival_objects/7", client)
    assert len(list(locations)) == 1
    for obj in locations:
        assert isinstance(obj, dict)
    def update(self, request, pk=None, *args, **kwargs):
        """Overrides default update method.

        Publishes or unpublishes resource records in ArchivesSpace based on
        publish attribute of parent ArrangementMap.
        """
        response = super(ArrangementMapViewset,
                         self).update(request, *args, **kwargs)
        try:
            map = ArrangementMap.objects.get(pk=pk)
            aspace = ASpace(baseurl=settings.ASPACE['baseurl'],
                            username=settings.ASPACE['username'],
                            password=settings.ASPACE['password'])
            for component in ArrangementMapComponent.objects.filter(map=map):
                resource = aspace.client.get(
                    component.archivesspace_uri).json()
                resource["publish"] = map.publish
                updated = aspace.client.post(component.archivesspace_uri,
                                             json=resource)
                updated.raise_for_status()
            return response
        except Exception as e:
            return Response(
                f"Error handling publish action in ArchivesSpace: {e}",
                status=500)
def do_it():
    global omd
    global s3
    global ss  # the pickle
    global tmpdir
    global ctr
    global solr
    global repo_ctr
    omd = get_details()
    main_log.debug("temp: {} url: {} s3 yaml:{} ".format(
        omd.get('tmpdir'), omd.get('pdfurl'), omd.get('s3_yaml')))
    instance = omd.get('instance')
    main_log.info("Instance: " + instance)
    main_log.info("retrieving saved state, if any, at {}".format(
        omd.get("savedstate")))
    ss = savestate(omd.get("savedstate"))
    if all:
        ss.clear()
    solr = SolrClient(omd.get('solr_url'))
    tmpdir = omd.get('tmpdir')
    try:
        s3 = S3(configpath=omd.get('s3_yaml'))
    except Exception as e:
        raise e
    aspace = ASpace()
    for repo in aspace.repositories:
        if all or repo_code is None or repo.repo_code == repo_code:
            process_repository(repo)
            repo_ctr += 1
    ss.save()  # last time for good luck!
Exemple #6
0
 def __init__(self):
     config = configparser.ConfigParser()
     config.read('local_settings.cfg')
     self.aspace = ASpace(baseurl=config.get('ArchivesSpace', 'baseURL'),
                          username=config.get('ArchivesSpace', 'username'),
                          password=config.get('ArchivesSpace', 'password'))
     self.repo = self.aspace.repositories(
         config.get('ArchivesSpace', 'repository'))
def setup():
    global aspace, conf_file
    '''Point ASNAKE_CONFIG_FILE at non-extant path so local config DEF HAPPENS even if you have a config'''
    try:
        conf_file = os.environ.pop('ASNAKE_CONFIG_FILE')
    except: pass
    os.environ['ASNAKE_CONFIG_FILE'] = "NONSENSEFILETHATDOESNOTEXIST"
    aspace = ASpace()
def calculate_child_count(sender, instance, **kwargs):
    if instance.archivesspace_uri and not kwargs["raw"]:
        aspace = ASpace(baseurl=settings.ASPACE['baseurl'],
                        username=settings.ASPACE['username'],
                        password=settings.ASPACE['password'])
        escaped_uri = instance.archivesspace_uri.replace('/', r'\/')
        search_uri = f"search?q=resource:/{escaped_uri}/ AND publish:true&page=1&fields[]=uri&type[]=archival_object&page_size=1"
        resource = aspace.client.get(search_uri).json()
        instance.child_count = resource["total_hits"]
 def test_get_orphans(self):
     with rac_vcr.use_cassette("test_get_orphans.json"):
         repository = ASpace(
             baseurl="http://localhost:8089").repositories(2)
         archival_objects = repository.archival_objects
         orphans = data_helpers.get_orphans(archival_objects,
                                            "linked_agents")
         for o in orphans:
             self.assertIsInstance(o, JSONModelObject)
Exemple #10
0
def test_trees():
    aspace = ASpace()
    resource_tree = aspace.repositories(2).resources(1).tree
    assert isinstance(resource_tree.children[0], TreeNode)
    records_via_walk = list(resource_tree.walk)
    assert aspace.repositories(2).resources(1).json() == records_via_walk[0].json()
    assert isinstance(records_via_walk[1], ComponentObject)
    subtree_walk = list(records_via_walk[1].tree.walk)
    assert records_via_walk[1].uri == subtree_walk[0].uri
Exemple #11
0
 def __init__(self, resource):
     config = configparser.ConfigParser()
     config.read("local_settings.cfg")
     self.aspace = ASpace(
         baseurl=config.get("ArchivesSpace", "baseURL"),
         username=config.get("ArchivesSpace", "user"),
         password=config.get("ArchivesSpace", "password"),
     )
     self.resource = self.aspace.repositories(2).resources(resource)
def test_resolve_to_json():
    client = ASpace().client
    expected = load_fixture("archival_object.json")
    uri = "/repositories/2/archival_objects/1"
    for to_resolve in [
            uri,
            wrap_json_object(expected)]:
        resolved = utils.resolve_to_json(to_resolve, client)
        assert resolved == expected
def test_get_date_display():
    """Tests whether the date display function works as intended."""
    client = ASpace().client
    for fixture, expected in [
            ("date_expression.json", "1905-1980"),
            ("date_no_expression.json", "1905-1980"),
            ("date_no_expression_no_end.json", "1905")]:
        date = load_fixture(fixture)
        result = utils.get_date_display(date, client)
        assert result == expected
Exemple #14
0
 def __init__(self, levels, always_add=False, resource=None):
     config = configparser.ConfigParser()
     config.read('local_settings.cfg')
     self.aspace = ASpace(baseurl=config.get('ArchivesSpace', 'baseURL'),
                          username=config.get('ArchivesSpace', 'username'),
                          password=config.get('ArchivesSpace', 'password'))
     self.repo = self.aspace.repositories(2)
     self.levels = levels if levels else LEVELS
     self.always_add = always_add
     self.resource = resource
 def test_object_locations(self):
     """Checks whether the function returns a list of JSONModelObjects."""
     with rac_vcr.use_cassette("test_get_locations.json"):
         repository = ASpace(
             baseurl="http://localhost:8089").repositories(2)
         archival_object = repository.archival_objects(7)
         locations = data_helpers.object_locations(archival_object)
         self.assertIsInstance(locations, list)
         self.assertEqual(len(locations), 1)
         for obj in locations:
             self.assertIsInstance(obj, JSONModelObject)
Exemple #16
0
def main():
    """Main function, which is run when this script is executed"""
    parser = get_parser()
    args = parser.parse_args()
    aspace = ASpace(baseurl=config.get("ArchivesSpace", "baseurl"),
                    username=config.get("ArchivesSpace", "username"),
                    password=config.get("ArchivesSpace", "password"))
    process_tree(
        aspace.client,
        aspace.repositories(config.get(
            "ArchivesSpace", "repository")).resources(args.resource_id))
Exemple #17
0
 def __init__(self, separator, resource, containers_list):
     config = configparser.ConfigParser()
     config.read('local_settings.cfg')
     self.aspace = ASpace(baseurl=config.get('ArchivesSpace', 'baseURL'),
                          username=config.get('ArchivesSpace', 'username'),
                          password=config.get('ArchivesSpace', 'password'))
     self.repo = self.aspace.repositories(
         config.get('ArchivesSpace', 'repository'))
     self.separator = separator
     self.resource = resource
     self.containers_list = containers_list
Exemple #18
0
 def __init__(self, resource_id):
     config = configparser.ConfigParser()
     config.read('local_settings.cfg')
     if os.path.isfile('data.csv'):
         raise Exception(
             "data.csv already exists and would be overwritten. Please move or delete this file before running the script again."
         )
     self.aspace = ASpace(baseurl=config.get('ArchivesSpace', 'baseURL'),
                          username=config.get('ArchivesSpace', 'username'),
                          password=config.get('ArchivesSpace', 'password'))
     self.repo = self.aspace.repositories(2)
     self.resource_id = int(resource_id)
def test_text_in_note():
    """Checks whether the query string and note content are close to a match."""
    client = ASpace().client
    for fixture, query_string, expected in [
        ("note_single.json", "Go Mets!", True),
        ("note_single.json", "hello", False),
        ("note_multi.json", "materials are restricted", True),
        ("note_multi.json", "Boo Yankees", False)
    ]:
        note = load_fixture(fixture)
        result = utils.text_in_note(note, query_string, client)
        assert result == expected
def main():
    ctr = 0
    main_log.info("Starting analysis {}".format(
        datetime.now().strftime(DATEFORMAT)))
    aspace = ASpace()
    for repo in aspace.repositories:
        if not repo.publish:
            continue
        main_log.info("\n ****** Checking {} **********\n".format(repo.name))
        check_resources(repo)

    main_log.info("Completed! {}".format(datetime.now().strftime(DATEFORMAT)))
def test_format_resource_id():
    """Checks whether the function returns a concatenated string as expected."""
    client = ASpace().client
    for fixture, formatted, separator in [
            ("resource.json", "1;2;3;4", ";"),
            ("resource_2.json", "1:2:3", None)]:
        resource = load_fixture(fixture)
        if not separator:
            result = utils.format_resource_id(resource, client)
        else:
            result = utils.format_resource_id(resource, client, separator)
        assert isinstance(result, str)
        assert result == formatted
 def get(self, request, *args, **kwargs):
     try:
         self.repo = ASpace(
             baseurl=settings.ASPACE['baseurl'],
             username=settings.ASPACE['username'],
             password=settings.ASPACE['password']).repositories(
                 settings.ASPACE['repo_id'])
         resource = self.repo.resources(kwargs.get('resource_id'))
         if isinstance(resource, JSONModelObject):
             return Response(resource.json(), status=200)
         return Response(resource['error'], status=404)
     except Exception as e:
         return Response(str(e), status=500)
Exemple #23
0
def instantiate_aspace(self, config=None):
    """Instantiates and returns an ASpace object with a repository as an attribute.

    Args:
        config (dict): optional config dict

    An optional config object can be passed to this function, otherwise the
    default configs are targeted.
    """
    config = config if config else settings.ARCHIVESSPACE
    aspace = ASpace(
        baseurl=config['baseurl'],
        username=config['username'],
        password=config['password'])
    return aspace
Exemple #24
0
def main():
    global aspace
    global repos
    global all
    global usernames
    user_ctr = 0
    main_log.info('Beginning report')
    aspace = ASpace()
    for repo in aspace.repositories:
        repos[repo.uri] = repo.name
    for user in aspace.users:
        if all or user.username in usernames:
            user_ctr += 1
            user_repo(user)
    main_log.info('Final count: of {} users'.format(user_ctr))
def test_get_orphans():
    key = "linked_agents"
    client = ASpace().client
    archival_objects = [
        "/repositories/2/archival_objects/1",
        "/repositories/2/archival_objects/2",
        "/repositories/2/archival_objects/3",
        "/repositories/2/archival_objects/4",
        "/repositories/2/archival_objects/5",
        "/repositories/2/archival_objects/6",
        "/repositories/2/archival_objects/7"]
    orphans = utils.get_orphans(
        archival_objects, key, client)
    for o in orphans:
        assert isinstance(o, dict)
        assert o.get(key) in ["", [], {}, None]
Exemple #26
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("search_directory")
    parser.add_argument("parent_folder_name")
    parser.add_argument("-u", '--user')
    parser.add_argument('-pw', '--password')
    args = parser.parse_args()
    search_directory = args.search_directory
    parent_folder = args.parent_folder_name
    username = args.user
    password = args.password
    aspace = ASpace(baseurl="http://as02.coalliance.org:8080",
                    username=username,
                    password=password)
    folder_list = get_file_names(search_directory)
    entries = get_as_data(folder_list, aspace, parent_folder)
    write_csv(entries)
def test_get_note_text():
    """Checks whether the returned note text matches the selected query string."""
    client = ASpace().client
    for fixture, expected in [
            ("note_bibliography.json", [
                "bibliography", "item 1", "item 2"]),
            ("note_index.json", ["title1", "title2"]),
            ("note_multi.json", ["Materials are restricted until a future date."]),
            ("note_multi_chronology.json", [
                "general note with chronology", "date", "event1", "event2"]),
            ("note_multi_defined.json", [
                "bioghist with defined list", "item", "1", "item", "2"]),
            ("note_multi_ordered.json", [
                "Bioghist with ordered list", "item1", "item2"]),
            ("note_single.json", ["Go Mets! They are the best!"])]:
        note = load_fixture(fixture)
        result = utils.get_note_text(note, client)
        assert isinstance(result, list)
        assert set(result) == set(expected)
def test_is_restricted():
    """Tests whether the function can find restrictions."""
    client = ASpace().client
    for fixture, query_string, restriction_acts, expected in [
        ("resource.json", "materials are restricted",
         ["disallow", "conditional"], True),
        ("resource.json", "materials are restricted",
         ["allow"], True),
        ("resource.json", "test", ["allow"], False),
        ("resource_2.json", "materials are restricted",
         ["disallow", "conditional"], True),
        ("resource_2.json", "test", ["allow"], False),
        ("resource_3.json", "materials are restricted",
         ["allow"], False)
    ]:
        archival_object = load_fixture(fixture)
        result = utils.is_restricted(
            archival_object, query_string, restriction_acts, client)
        assert result == expected
def test_format_from_obj():
    """Test that format strings can be passed to objects as expected."""
    client = ASpace().client
    for fixture, format_string in [
        ("date_expression.json", "{begin} - {end} ({expression})"),
        ("date_expression.json", None),
    ]:
        date = load_fixture(fixture)
        if not format_string:
            with raises(Exception) as excpt:
                utils.format_from_obj(date, format_string, client)
            assert str(excpt.value) == "No format string provided."
        else:
            formatted = utils.format_from_obj(
                date, format_string, client)
            assert formatted == "1905 - 1980 (1905-1980)"
            with raises(KeyError) as excpt:
                formatted = utils.format_from_obj(
                    date, "{start} - {end} ({expression})", client)
            assert "was not found in this object" in str(excpt.value)
def test_indicates_restriction():
    """Tests whether rights statements are correctly parsed for restrictions."""
    client = ASpace().client
    for fixture, restriction_acts, expected in [
            ("rights_statement_restricted.json",
             ["disallow", "conditional"], True),
            ("rights_statement_restricted.json", ["allow"], False),
            ("rights_statement_open.json",
             ["disallow", "conditional"], False),
            ("rights_statement_open.json", ["allow"], False),
            ("rights_statement_conditional.json",
             ["disallow", "conditional"], True),
            ("rights_statement_conditional.json", ["allow"], False),
            ("rights_statement_not_restricted.json",
             ["disallow", "conditional"], False),
            ("rights_statement_not_restricted.json", ["allow"], True)]:
        statement = load_fixture(fixture)
        status = utils.indicates_restriction(
            statement, restriction_acts, client)
        assert status == expected