Browse Source

Numerous updates

main
astanley 2 years ago
parent
commit
167bccbc3f
  1. 4
      FoxmlWorker.py
  2. 53
      ImportProcessor.py
  3. 80
      ImportUtilities.py

4
FoxmlWorker.py

@ -132,5 +132,5 @@ class FWorker:
if __name__ == '__main__': if __name__ == '__main__':
FW = FWorker('inputs/sample_foxml.xml') FW = FWorker('assets/sample_fox.xml')
print(FW.get_dc()) print(FW.get_rels_ext_values())

53
ImportProcessor.py

@ -27,7 +27,7 @@ class ImportProcessor:
'islandora:newspaperIssueCModel': ['OBJ', 'PDF'], 'islandora:newspaperIssueCModel': ['OBJ', 'PDF'],
'islandora:sp-audioCModel': ['OBJ'], 'islandora:sp-audioCModel': ['OBJ'],
} }
self.iu = IU.ImportUtilities() self.iu = IU.ImportUtilities(namespace)
self.ms = IS.ImportServerUtilities(namespace) self.ms = IS.ImportServerUtilities(namespace)
self.namespace = namespace self.namespace = namespace
self.export_dir = '/opt/islandora/upei_migration/export' self.export_dir = '/opt/islandora/upei_migration/export'
@ -95,24 +95,49 @@ class ImportProcessor:
writer.writerow(row) writer.writerow(row)
# Prepares worksheets for workbench ingest. # Prepares worksheets for workbench ingest.
def prepare_worksheet(self, content_model, output_file): def prepare_intial_ingest_worksheet(self, output_file):
details = self.iu.get_worksheet_details(self.namespace, content_model) details = self.iu.get_worksheet_details()
if not details: # Check if details is None or empty
print("No worksheet details found.")
return
with open(output_file, 'w', newline='') as csvfile: with open(output_file, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=self.fieldnames) writer = csv.DictWriter(csvfile, fieldnames=['id', 'title', 'field_pid', 'field_model'])
writer.writeheader() writer.writeheader()
id = 1
for detail in details: for detail in details:
mods = self.iu.extract_from_mods(detail['field_pid']) if not detail or not detail.get('field_pid'): # Add safer checks for detail and field_pid
row = mods | detail continue
node_id = self.iu.get_nid_from_pid(self.namespace, row['field_member_of'])
row['id'] = row['field_pid']
dc = self.iu.get_dc_values(detail['field_pid'], self.namespace)
if 'title' not in row:
row['title'] = dc['title']
if node_id:
row['field_member_of'] = node_id
writer.writerow(row)
# Fetch DC values and ensure it's valid
dc = self.iu.get_dc_values(detail['field_pid'])
if not dc or 'title' not in dc: # Ensure dc is not None and has a 'title' key
print(f"Warning: Missing DC values for PID {detail['field_pid']}")
continue
# Prepare the row for CSV
row = {
'id': id,
'title': dc['title'],
'field_pid': detail['field_pid'],
'field_model': detail.get('field_model', 'Unknown') # Use .get() with fallback for safety
}
writer.writerow(row)
id += 1
def prepare_relationship_worksheet(self, output_file):
realtionships = self.iu.get_relationships()
with open(output_file, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=['node_id', 'field_member_of'])
writer.writeheader()
for relatiopship in realtionships:
row = {}
row['node_id'] = relatiopship['node_id']
row['field_member_of'] = relatiopship['member_of']
writer.writerow(row)
writer.writeheader()
MP = ImportProcessor('ivoices') MP = ImportProcessor('ivoices')
MP.prepare_relationship_worksheet('ivoices_relationships.csv')

80
ImportUtilities.py

@ -35,7 +35,7 @@ class ImportUtilities:
with open(csv_file, newline='') as csvfile: with open(csv_file, newline='') as csvfile:
reader = csv.DictReader(csvfile) reader = csv.DictReader(csvfile)
for row in reader: for row in reader:
command = f"update {table} set node_id = '{row['ID']}' where pid = '{row['PID']}'" command = f"update {table} set nid = '{row['ID']}' where pid = '{row['PID']}'"
cursor.execute(command) cursor.execute(command)
self.conn.commit() self.conn.commit()
@ -122,9 +122,12 @@ class ImportUtilities:
pids.append(row['pid']) pids.append(row['pid'])
return pids return pids
# Utility function to prepare database selections for workbenchl # Utility function to prepare database selections for workbench
def get_worksheet_details(self, table, content_model): def get_worksheet_details(self, content_model=None):
command = f"Select * from {table} where content_model = 'islandora:{content_model}'" command = f"Select * from {self.namespace}"
if content_model is not None:
command = f"{command} where content_model = '{content_model}'"
cursor = self.conn.cursor() cursor = self.conn.cursor()
details = [] details = []
for row in cursor.execute(command): for row in cursor.execute(command):
@ -146,23 +149,28 @@ class ImportUtilities:
'sequence': 'field_weight', 'sequence': 'field_weight',
} }
content_map = { content_map = {
'islandora:collectionCModel': 'collection', 'islandora:collectionCModel': 'Collection',
'islandora:sp_large_image_cmodel': 'image', 'islandora:sp_large_image_cmodel': 'Image',
'islandora:sp-audioCModel': 'audio', 'islandora:sp-audioCModel': 'Audio',
'islandora:pageCModel': 'page', 'islandora:pageCModel': 'Page',
'islandora:bookCModel': 'Paged Content', 'islandora:bookCModel': 'Paged Content',
'islandora:compoundCModel': 'Compound Object', 'islandora:compoundCModel': 'Compound Object',
'islandora:sp_pdf': 'Digital Document', 'islandora:sp_pdf': 'Digital Document',
'islandora:sp_basic_image': 'image', 'islandora:sp_basic_image': 'Image',
'islandora:newspaperCModel': 'newspaper', 'islandora:newspaperCModel': 'Newspaper',
'islandora:newspaperIssueCModel': 'Publication Issue', 'islandora:newspaperIssueCModel': 'Publication Issue',
'islandora:newspaperPageCModel': 'page', 'islandora:newspaperPageCModel': 'Page',
'islandora:oralhistoriesCModel': 'Compound Object', 'islandora:oralhistoriesCModel': 'Compound Object',
'islandora:sp_videoCModel': 'Video', 'islandora:sp_videoCModel': 'Video',
'ir:thesisCModel': 'Digital Document', 'ir:thesisCModel': 'Digital Document',
'islandora:rootSerialCModel': 'Compound Object', 'islandora:rootSerialCModel': 'Compound Object',
'islandora:intermediateCModel': 'Compound Object', 'islandora:intermediateCModel': 'Compound Object',
'ir:citationCModel': 'Citation' 'ir:citationCModel': 'Citation',
'islandora:audioCModel|islandora:sp-audioCModel': 'Audio',
'islandora:slideCModel|islandora:sp_large_image_cmodel': 'Image',
'islandora:videoCModel|islandora:sp_videoCModel': 'Video',
'islandora:audioCModel': 'Audio',
} }
cleaned_line = {} cleaned_line = {}
@ -240,9 +248,9 @@ class ImportUtilities:
return result['node_id'] if result is not None else '' return result['node_id'] if result is not None else ''
# Get key - value pairs from stored dublin core. # Get key - value pairs from stored dublin core.
def get_dc_values(self, pid, table): def get_dc_values(self, pid):
cursor = self.conn.cursor() cursor = self.conn.cursor()
result = cursor.execute(f"select dublin_core from {table} where pid = '{pid}'") result = cursor.execute(f"select dublin_core from {self.namespace} where pid = '{pid}'")
dc = result.fetchone()['dublin_core'] dc = result.fetchone()['dublin_core']
root = ET.fromstring(dc) root = ET.fromstring(dc)
namespaces = { namespaces = {
@ -255,8 +263,46 @@ class ImportUtilities:
dc_vals[tag] = value dc_vals[tag] = value
return dc_vals return dc_vals
def add_title(self):
cursor = self.conn.cursor()
command = f"SELECT PID, title from {self.namespace} where title is null"
pids = cursor.execute(command).fetchall()
for pid in pids:
dc = self.get_dc_values(pid['PID'])
query = f"UPDATE {self.namespace} SET title = ? WHERE pid = ?"
values = (dc.get('title'), pid['PID'])
cursor.execute(query, values)
self.conn.commit()
if __name__ == '__main__': def get_relationships(self):
MU = ImportUtilities() cursor = self.conn.cursor()
cursor.execute("SELECT pid, nid FROM ivoices")
pairs = cursor.fetchall() # Fetch all rows
relationships = []
MU.add_node_ids('msvu', 'inputs/nid-pid.csv') for pid, nid in pairs:
if not pid:
continue
cursor.execute("SELECT collection_pid FROM ivoices WHERE pid = ?", (pid,))
collection_pid = cursor.fetchone()
if collection_pid: # Ensure there is a valid collection_pid
collection_pid = collection_pid[0] # Extract the value
cursor.execute("SELECT nid FROM ivoices WHERE pid = ?", (collection_pid,))
nid_result = cursor.fetchone()
if nid_result: # Ensure a valid nid is found
relationships.append({
'node_id': nid,
'member_of': nid_result[0] # Extract nid value
})
return relationships
if __name__ == '__main__':
MU = ImportUtilities('ivoices')
MU.add_node_ids('ivoices', 'inputs/ivoice-nid-pid.csv')

Loading…
Cancel
Save