#/usr/bin/python3 #The code takes a list of top container IDs and Location Codes and UPDATES a location. It will not add a new location. #PLEASE NOTE that the repository ID (7) is hardcoded into the tc_uri below, but that can easily be fixed. import csv import json import traceback import getpass import requests import pprint import archivesspace_login as as_login def opencsv(): '''This function opens a csv file''' input_csv = input('Please enter path to input CSV: ') file = open(input_csv, 'r') csvfile = csv.reader(file) #Skip header row next(csvfile, None) return csvfile def changeTC_location(): #replaces a location value in a top container. api_url, headers = as_login.login() csvfile = opencsv() for row in csvfile: topContainer_id = row[0] newLoc = row[1] tc_uri='repositories/7/top_containers/' + str(topContainer_id) newLocation='/locations/' + str(newLoc) try: tc_json = requests.get(f"{api_url}/{tc_uri}", headers=headers).json() #pprint.pprint(tc_json) tc_json['container_locations'][0]['ref']=newLocation post_record = requests.post(f"{api_url}/{tc_uri}", headers=headers, json=tc_json).json() log = open("c:\\temp\Log.txt", "a") print(post_record, file=log) #print(post_record) except Exception: print(traceback.format_exc()) changeTC_location()