diff --git a/gramps/gen/lib/note.py b/gramps/gen/lib/note.py index 71280e5f0..9f1ae33a6 100644 --- a/gramps/gen/lib/note.py +++ b/gramps/gen/lib/note.py @@ -170,6 +170,28 @@ class Note(BasicPrimaryObject): reflist.extend(self.get_referenced_tag_handles()) return reflist + def has_handle_reference(self, classname, handle): + """ + Return True if the object has reference to a given handle of given + primary object type. + + :param classname: The name of the primary object class. + :type classname: str + :param handle: The handle to be checked. + :type handle: str + + :returns: + Returns whether the object has reference to this handle of + this object type. + + :rtype: bool + """ + for dom, obj, prop, hndl in self.get_links(): + if dom == "gramps" and prop == "handle" and \ + obj == classname and hndl == handle: + return True + return False + def remove_handle_references(self, classname, handle_list): """ Remove all references in this object to object handles in the list. @@ -193,6 +215,26 @@ class Note(BasicPrimaryObject): tags.append(styledtext_tag) self.text.set_tags(tags) + def replace_handle_reference(self, classname, old_handle, new_handle): + """ + Replace all references to old handle with those to the new handle. + + :param classname: The name of the primary object class. + :type classname: str + :param old_handle: The handle to be replaced. + :type old_handle: str + :param new_handle: The handle to replace the old one with. + :type new_handle: str + """ + for styledtext_tag in self.text.get_tags(): + if(styledtext_tag.name == StyledTextTagType.LINK and + styledtext_tag.value.startswith("gramps://")): + obj, prop, value = styledtext_tag.value[9:].split("/", 2) + if(obj == classname and prop == 'handle' and + value == old_handle): + styledtext_tag.value = styledtext_tag.value.replace( + old_handle, new_handle) + def merge(self, acquisition): """ Merge the content of acquisition into this note. diff --git a/gramps/gen/merge/mergecitationquery.py b/gramps/gen/merge/mergecitationquery.py index bd7e610e1..3e04af79b 100644 --- a/gramps/gen/merge/mergecitationquery.py +++ b/gramps/gen/merge/mergecitationquery.py @@ -28,8 +28,8 @@ Provide merge capabilities for citations. # Gramps modules # #------------------------------------------------------------------------- -from ..lib import (Person, Family, Event, Place, - Media, Repository, Citation, Source) +from ..lib import (Person, Family, Event, Place, Media, Repository, + Citation, Source, Note) from ..db import DbTxn from ..const import GRAMPS_LOCALE as glocale _ = glocale.translation.sgettext @@ -105,6 +105,12 @@ class MergeCitationQuery: source.replace_citation_references(old_handle, new_handle) self.database.commit_source(source, trans) + elif class_name == Note.__name__: + note = self.database.get_note_from_handle(handle) + assert(note.has_handle_reference('Citation', old_handle)) + note.replace_handle_reference( + 'Citation', old_handle, new_handle) + self.database.commit_note(note, trans) else: raise MergeError("Encounter an object of type %s that has " "a citation reference." % class_name) diff --git a/gramps/gen/merge/mergeeventquery.py b/gramps/gen/merge/mergeeventquery.py index 28250424e..ba32f64f2 100644 --- a/gramps/gen/merge/mergeeventquery.py +++ b/gramps/gen/merge/mergeeventquery.py @@ -27,7 +27,7 @@ Provide merge capabilities for events. # Gramps modules # #------------------------------------------------------------------------- -from ..lib import Person, Family +from ..lib import Person, Family, Note from ..db import DbTxn from ..const import GRAMPS_LOCALE as glocale _ = glocale.translation.sgettext @@ -88,6 +88,12 @@ class MergeEventQuery: family.replace_handle_reference("Event", old_handle, new_handle) self.database.commit_family(family, trans) + elif class_name == Note.__name__: + note = self.database.get_note_from_handle(handle) + assert(note.has_handle_reference('Event', old_handle)) + note.replace_handle_reference( + 'Event', old_handle, new_handle) + self.database.commit_note(note, trans) else: raise MergeError("Encounter an object of type %s that has " "an event reference." % class_name) diff --git a/gramps/gen/merge/mergefamilyquery.py b/gramps/gen/merge/mergefamilyquery.py index 4dc8aff93..7659c92d9 100644 --- a/gramps/gen/merge/mergefamilyquery.py +++ b/gramps/gen/merge/mergefamilyquery.py @@ -194,12 +194,17 @@ class MergeFamilyQuery: if phoenix_mother: phoenix_mother.remove_family_handle(old_handle) self.database.commit_person(phoenix_mother, trans) - # replace the family in lds ordinances - for (dummy, person_handle) in self.database.find_backlink_handles( - old_handle, ['Person']): - if person_handle in (self.titanic_fh, self.titanic_mh): + # replace the family in lds ordinances and notes + for (ref_obj, ref_handle) in self.database.find_backlink_handles( + old_handle, ['Person', 'Note']): + if ref_handle in (self.titanic_fh, self.titanic_mh): continue - person = self.database.get_person_from_handle(person_handle) - person.replace_handle_reference('Family', old_handle,new_handle) - self.database.commit_person(person, trans) + obj = self.database.method( + "get_%s_from_handle", ref_obj)(ref_handle) + assert obj.has_handle_reference('Family', old_handle) + obj.replace_handle_reference( + 'Family', old_handle, new_handle) + if ref_handle != old_handle: + self.database.method("commit_%s", ref_obj)(obj, trans) + self.database.remove_family(old_handle, trans) diff --git a/gramps/gen/merge/mergemediaquery.py b/gramps/gen/merge/mergemediaquery.py index b03e983b6..e153c784e 100644 --- a/gramps/gen/merge/mergemediaquery.py +++ b/gramps/gen/merge/mergemediaquery.py @@ -27,7 +27,7 @@ Provide merge capabilities for media objects. # Gramps modules # #------------------------------------------------------------------------- -from ..lib import Person, Family, Event, Source, Citation, Place +from ..lib import Person, Family, Event, Source, Citation, Place, Note from ..db import DbTxn from ..const import GRAMPS_LOCALE as glocale _ = glocale.translation.sgettext @@ -90,6 +90,12 @@ class MergeMediaQuery: assert(place.has_media_reference(old_handle)) place.replace_media_references(old_handle, new_handle) self.database.commit_place(place, trans) + elif class_name == Note.__name__: + note = self.database.get_note_from_handle(handle) + assert(note.has_handle_reference('Media', old_handle)) + note.replace_handle_reference( + 'Media', old_handle, new_handle) + self.database.commit_note(note, trans) else: raise MergeError("Encounter an object of type % s that has " "a media object reference." % class_name) diff --git a/gramps/gen/merge/mergenotequery.py b/gramps/gen/merge/mergenotequery.py index 3261ccb09..86e097ce8 100644 --- a/gramps/gen/merge/mergenotequery.py +++ b/gramps/gen/merge/mergenotequery.py @@ -28,7 +28,7 @@ Provide merge capabilities for notes. # #------------------------------------------------------------------------- from ..lib import (Person, Family, Event, Place, Source, Citation, Repository, - Media) + Media, Note) from ..db import DbTxn from ..const import GRAMPS_LOCALE as glocale _ = glocale.translation.sgettext @@ -99,6 +99,12 @@ class MergeNoteQuery: assert(repo.has_note_reference(old_handle)) repo.replace_note_references(old_handle, new_handle) self.database.commit_repository(repo, trans) + elif class_name == Note.__name__: + note = self.database.get_note_from_handle(handle) + assert(note.has_handle_reference('Note', old_handle)) + note.replace_handle_reference( + 'Note', old_handle, new_handle) + self.database.commit_note(note, trans) else: raise MergeError("Encounter object of type %s that has " "a note reference." % class_name) diff --git a/gramps/gen/merge/mergepersonquery.py b/gramps/gen/merge/mergepersonquery.py index 93f134945..dfb0b8028 100644 --- a/gramps/gen/merge/mergepersonquery.py +++ b/gramps/gen/merge/mergepersonquery.py @@ -100,14 +100,16 @@ class MergePersonQuery: spouse.remove_family_handle(family_handle) self.database.commit_person(spouse, trans) # replace the family in lds ordinances - for (dummy, person_handle) in self.database.find_backlink_handles( - family_handle, ['Person']): - if person_handle == old_handle: + for (ref_obj, ref_handle) in self.database.find_backlink_handles( + family_handle, ['Person', 'Note']): + if ref_handle == old_handle: continue - person = self.database.get_person_from_handle(person_handle) - person.replace_handle_reference('Family', family_handle, - main_family_handle) - self.database.commit_person(person, trans) + obj = self.database.method( + "get_%s_from_handle", ref_obj)(ref_handle) + assert obj.has_handle_reference('Family', family_handle) + obj.replace_handle_reference('Family', family_handle, + main_family_handle) + self.database.method("commit_%s", ref_obj)(obj, trans) self.database.remove_family(family_handle, trans) self.database.commit_family(main_family, trans) @@ -133,13 +135,15 @@ class MergePersonQuery: self.phoenix.merge(self.titanic) self.database.commit_person(self.phoenix, trans) - for (dummy, person_handle) in self.database.find_backlink_handles( - old_handle, ['Person']): - person = self.database.get_person_from_handle(person_handle) - assert person.has_handle_reference('Person', old_handle) - person.replace_handle_reference('Person', old_handle, new_handle) - if person_handle != old_handle: - self.database.commit_person(person, trans) + for (ref_obj, handle) in self.database.find_backlink_handles( + old_handle, ['Person', 'Note']): + obj = self.database.method( + "get_%s_from_handle", ref_obj)(handle) + assert obj.has_handle_reference('Person', old_handle) + obj.replace_handle_reference( + 'Person', old_handle, new_handle) + if handle != old_handle: + self.database.method("commit_%s", ref_obj)(obj, trans) for family_handle in self.phoenix.get_parent_family_handle_list(): family = self.database.get_family_from_handle(family_handle) diff --git a/gramps/gen/merge/mergeplacequery.py b/gramps/gen/merge/mergeplacequery.py index 95f89677c..c168b58b6 100644 --- a/gramps/gen/merge/mergeplacequery.py +++ b/gramps/gen/merge/mergeplacequery.py @@ -28,7 +28,7 @@ Provide merge capabilities for places. # Gramps modules # #------------------------------------------------------------------------- -from ..lib import Person, Family, Event, Place +from ..lib import Person, Family, Event, Place, Note from ..db import DbTxn from ..const import GRAMPS_LOCALE as glocale _ = glocale.translation.sgettext @@ -85,6 +85,12 @@ class MergePlaceQuery: place.replace_handle_reference('Place', old_handle, new_handle) self.database.commit_place(place, trans) + elif class_name == Note.__name__: + note = self.database.get_note_from_handle(handle) + assert(note.has_handle_reference('Place', old_handle)) + note.replace_handle_reference('Place', old_handle, + new_handle) + self.database.commit_note(note, trans) else: raise MergeError("Encounter an object of type %s that has " "a place reference." % class_name) diff --git a/gramps/gen/merge/mergerepositoryquery.py b/gramps/gen/merge/mergerepositoryquery.py index dc6c5100e..57a6f6b6d 100644 --- a/gramps/gen/merge/mergerepositoryquery.py +++ b/gramps/gen/merge/mergerepositoryquery.py @@ -27,7 +27,7 @@ Provide merge capabilities for repositories. # Gramps modules # #------------------------------------------------------------------------- -from ..lib import Source +from ..lib import Source, Note from ..db import DbTxn from ..const import GRAMPS_LOCALE as glocale _ = glocale.translation.sgettext @@ -65,6 +65,12 @@ class MergeRepositoryQuery: assert source.has_handle_reference('Repository', old_handle) source.replace_repo_references(old_handle, new_handle) self.database.commit_source(source, trans) + elif class_name == Note.__name__: + note = self.database.get_note_from_handle(handle) + assert(note.has_handle_reference('Repository', old_handle)) + note.replace_handle_reference( + 'Repository', old_handle, new_handle) + self.database.commit_note(note, trans) else: raise MergeError("Encounter an object of type %s that has " "a repository reference." % class_name) diff --git a/gramps/gen/merge/mergesourcequery.py b/gramps/gen/merge/mergesourcequery.py index 89d95b604..fb6c4085b 100644 --- a/gramps/gen/merge/mergesourcequery.py +++ b/gramps/gen/merge/mergesourcequery.py @@ -29,8 +29,7 @@ Provide merge capabilities for sources. # Gramps modules # #------------------------------------------------------------------------- -from ..lib import (Person, Family, Event, Place, Source, Repository, - Media, Citation) +from ..lib import (Citation, Note) from ..db import DbTxn from ..const import GRAMPS_LOCALE as glocale _ = glocale.translation.sgettext @@ -68,6 +67,12 @@ class MergeSourceQuery: assert(citation.get_reference_handle() == old_handle) citation.set_reference_handle(new_handle) self.database.commit_citation(citation, trans) + elif class_name == Note.__name__: + note = self.database.get_note_from_handle(handle) + assert(note.has_handle_reference('Source', old_handle)) + note.replace_handle_reference( + 'Source', old_handle, new_handle) + self.database.commit_note(note, trans) else: raise MergeError("Encounter an object of type %s that has " "a source reference." % class_name)