Skip to content

Commit

Permalink
Merge pull request #27 from dag-hammarskjold-library/jb/25
Browse files Browse the repository at this point in the history
Update change tag edits
  • Loading branch information
jbukhari authored Jan 14, 2025
2 parents a7e0dee + 30ec370 commit 338095e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 90 deletions.
76 changes: 29 additions & 47 deletions batch_edits/scripts/batch_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,38 +152,18 @@ def edit_7(bib):
return bib

# change_tag
def edit_8(bib):
# 8. BIBLIOGRAPHIC - Transfer field 100 - to 700
def edit_8_9_10_11_14(bib):
# 8. BIBLIOGRAPHIC - Transfer field 100 - to 700 - Clean indicators before transfer
# 9. BIBLIOGRAPHIC - Transfer field 110 - to 710 - Clean indicators before transfer
# 10. BIBLIOGRAPHIC - Transfer field 111 - to 711 - Clean indicators before transfer
# 11. BIBLIOGRAPHIC - Transfer field 130 - to 730 - Clean indicators before transfer
# 14. BIBLIOGRAPHIC - Transfer field 440 - To 830 - Clean indicators before transfer
if not any([x == 'Speeches' or x == 'Voting Data' for x in bib.get_values('989', 'a')]):
for field in bib.get_fields('100'):
field.tag = '700'
for from_tag, to_tag in [('100', '700'), ('110', '710'), ('111', '711'), ('130', '730'), ('440', '830')]:
for field in bib.get_fields(from_tag):
field.ind1, field.ind1 = None, None

return bib

# change_tag
def edit_9(bib):
# 9. BIBLIOGRAPHIC - Transfer field 110 - to 710
if not any([x == 'Speeches' or x == 'Voting Data' for x in bib.get_values('989', 'a')]):
for field in bib.get_fields('110'):
field.tag = '710'

return bib

# change_tag
def edit_10(bib):
# 10. BIBLIOGRAPHIC - Transfer field 111 - to 711
if not any([x == 'Speeches' or x == 'Voting Data' for x in bib.get_values('989', 'a')]):
for field in bib.get_fields('111'):
field.tag = '711'

return bib

# change_tag
def edit_11(bib):
# 11. BIBLIOGRAPHIC - Transfer field 130 - to 730
if not any([x == 'Speeches' or x == 'Voting Data' for x in bib.get_values('989', 'a')]):
for field in bib.get_fields('130'):
field.tag = '730'
bib = change_tag(bib, from_tag, to_tag)

return bib

Expand All @@ -203,15 +183,6 @@ def edit_13(bib):

return bib

# change_tag
def edit_14(bib):
# 14. BIBLIOGRAPHIC - Transfer field 440 - To 830
if not any([x == 'Speeches' or x == 'Voting Data' for x in bib.get_values('989', 'a')]):
for field in bib.get_fields('440'):
field.tag = '830'

return bib

# no function
def edit_15(bib):
# 15. BIBLIOGRAPHIC - Transfer field 490 $x - Transfer to 022 $a if the field with the same value does not exists
Expand Down Expand Up @@ -380,6 +351,9 @@ def edit_55(bib):

### future - abstracted functions

def change_value():
pass

def delete_field(record, tag, conditions=[]):
if conditions:
assert all([isinstance(c, Condition) for c in conditions])
Expand All @@ -395,16 +369,18 @@ def delete_field(record, tag, conditions=[]):
return record

def change_tag(record, from_tag, to_tag, conditions=[]):
if conditions:
assert all([isinstance(c, Condition) for c in conditions])
assert all([isinstance(c, Condition) for c in conditions]) if conditions else True

for field in record.get_fields(from_tag):
for subfields in [x.subfields for x in conditions]:
for code, val in subfields:
if val in field.get_values(code):
field.tag = to_tag

return record
if conditions:
for subfields in [x.subfields for x in conditions]:
for code, val in subfields:
if val in field.get_values(code):
field.tag = to_tag
else:
field.tag = to_tag

return record

def delete_indicators(record, tag, conditions=[]):
if conditions:
Expand All @@ -430,6 +406,12 @@ def delete_subfield(record, tag, subfield_code, conditions=[]):

return record

def delete_indicators():
pass

def change_indicators():
pass

###

if __name__ == '__main__':
Expand Down
56 changes: 13 additions & 43 deletions batch_edits/tests/test_batch_edit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import sys, pytest, random
from dlx import DB
from dlx.marc import Bib, Auth, BibSet, AuthSet, Query, Condition
from batch_edits.scripts import batch_edit

DB.connect('mongomock://localhost')

defaults = [Bib() for i in range(0, 10)]
speeches = [Bib().set('989', 'a', 'Speeches') for i in range(0, 10)]
votes = [Bib().set('989', 'a', 'Voting Data') for i in range(0, 10)]
Expand Down Expand Up @@ -82,46 +85,23 @@ def test_edit_7():
assert not any([bib.get_value('069', 'b') for bib in defaults])
assert all([bib.get_value('069', 'a') for bib in speeches + votes])

def test_edit_8():
# 8. BIBLIOGRAPHIC - Transfer field 100 - to 700
def test_edit_8_9_10_11_14():
# 8. BIBLIOGRAPHIC - Transfer field 100 - to 700 - Clean indicators before transfer
# 9. BIBLIOGRAPHIC - Transfer field 110 - to 710 - Clean indicators before transfer
# 10. BIBLIOGRAPHIC - Transfer field 111 - to 711 - Clean indicators before transfer
# 11. BIBLIOGRAPHIC - Transfer field 130 - to 730 - Clean indicators before transfer
# 14. BIBLIOGRAPHIC - Transfer field 440 - To 830 - Clean indicators before transfer
Auth().set('100', 'a', 'dummy').commit()
[bib.set('100', 'a', 'dummy') for bib in all_records()]
[setattr(bib.get_field('100'), 'ind1', 9) for bib in all_records()]
assert all([bib.get_value('100', 'a') for bib in all_records()])
[batch_edit.edit_8(bib) for bib in all_records()]
assert all([bib.get_field('100').ind1 for bib in defaults])
[batch_edit.edit_8_9_10_11_14(bib) for bib in all_records()]
assert not any([bib.get_value('100', 'a') for bib in defaults])
assert all([bib.get_value('700', 'a') for bib in defaults])
assert not any([bib.get_field('700').ind1 for bib in defaults])
assert all([bib.get_value('100', 'a') for bib in speeches + votes])

def test_edit_9():
# 9. BIBLIOGRAPHIC - Transfer field 110 - to 710
Auth().set('110', 'a', 'dummy').commit()
[bib.set('110', 'a', 'dummy') for bib in all_records()]
assert all([bib.get_value('110', 'a') for bib in all_records()])
[batch_edit.edit_9(bib) for bib in all_records()]
assert not any([bib.get_value('110', 'a') for bib in defaults])
assert all([bib.get_value('710', 'a') for bib in defaults])
assert all([bib.get_value('110', 'a') for bib in speeches + votes])

def test_edit_10():
# 10. BIBLIOGRAPHIC - Transfer field 111 - to 711
Auth().set('111', 'a', 'dummy').commit()
[bib.set('111', 'a', 'dummy') for bib in all_records()]
assert all([bib.get_value('111', 'a') for bib in all_records()])
[batch_edit.edit_10(bib) for bib in all_records()]
assert not any([bib.get_value('111', 'a') for bib in defaults])
assert all([bib.get_value('711', 'a') for bib in defaults])
assert all([bib.get_value('111', 'a') for bib in speeches + votes])

def test_edit_11():
# 11. BIBLIOGRAPHIC - Transfer field 130 - to 730
Auth().set('130', 'a', 'dummy').commit()
[bib.set('130', 'a', 'dummy') for bib in all_records()]
assert all([bib.get_value('130', 'a') for bib in all_records()])
[batch_edit.edit_11(bib) for bib in all_records()]
assert not any([bib.get_value('130', 'a') for bib in defaults])
assert all([bib.get_value('730', 'a') for bib in defaults])
assert all([bib.get_value('130', 'a') for bib in speeches + votes])

def test_edit_12():
# 12. BIBLIOGRAPHIC - Delete field 222 - No condition
[bib.set('222', 'a', 'dummy') for bib in all_records()]
Expand All @@ -138,16 +118,6 @@ def test_edit_13():
assert not any([bib.get_value('269', 'a') for bib in speeches + votes])
assert all([bib.get_value('269', 'a') for bib in defaults])

def test_edit_14():
# 14. BIBLIOGRAPHIC - Transfer field 440 - To 830
Auth().set('140', 'a', 'dummy').commit()
[bib.set('440', 'a', 'dummy') for bib in all_records()]
assert all([bib.get_value('440', 'a') for bib in all_records()])
[batch_edit.edit_14(bib) for bib in all_records()]
assert not any([bib.get_value('440', 'a') for bib in defaults])
assert all([bib.get_value('830', 'a') == 'dummy' for bib in defaults])
assert all([bib.get_value('440', 'a') for bib in speeches + votes])

def test_edit_15():
# 15. BIBLIOGRAPHIC - Transfer field 490 $x - Transfer to 022 $a if the field with the same value does not exists
[bib.set('490', 'x', 'dummy') for bib in all_records()]
Expand Down

0 comments on commit 338095e

Please sign in to comment.