Note LINK support for merge of other objects

This commit is contained in:
prculley 2020-08-10 11:30:56 -05:00 committed by Nick Hall
parent 1d29196344
commit 18c61c3e1b
10 changed files with 122 additions and 30 deletions

View File

@ -170,6 +170,28 @@ class Note(BasicPrimaryObject):
reflist.extend(self.get_referenced_tag_handles()) reflist.extend(self.get_referenced_tag_handles())
return reflist 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): def remove_handle_references(self, classname, handle_list):
""" """
Remove all references in this object to object handles in the list. Remove all references in this object to object handles in the list.
@ -193,6 +215,26 @@ class Note(BasicPrimaryObject):
tags.append(styledtext_tag) tags.append(styledtext_tag)
self.text.set_tags(tags) 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): def merge(self, acquisition):
""" """
Merge the content of acquisition into this note. Merge the content of acquisition into this note.

View File

@ -28,8 +28,8 @@ Provide merge capabilities for citations.
# Gramps modules # Gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..lib import (Person, Family, Event, Place, from ..lib import (Person, Family, Event, Place, Media, Repository,
Media, Repository, Citation, Source) Citation, Source, Note)
from ..db import DbTxn from ..db import DbTxn
from ..const import GRAMPS_LOCALE as glocale from ..const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext _ = glocale.translation.sgettext
@ -105,6 +105,12 @@ class MergeCitationQuery:
source.replace_citation_references(old_handle, source.replace_citation_references(old_handle,
new_handle) new_handle)
self.database.commit_source(source, trans) 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: else:
raise MergeError("Encounter an object of type %s that has " raise MergeError("Encounter an object of type %s that has "
"a citation reference." % class_name) "a citation reference." % class_name)

View File

@ -27,7 +27,7 @@ Provide merge capabilities for events.
# Gramps modules # Gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..lib import Person, Family from ..lib import Person, Family, Note
from ..db import DbTxn from ..db import DbTxn
from ..const import GRAMPS_LOCALE as glocale from ..const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext _ = glocale.translation.sgettext
@ -88,6 +88,12 @@ class MergeEventQuery:
family.replace_handle_reference("Event", old_handle, family.replace_handle_reference("Event", old_handle,
new_handle) new_handle)
self.database.commit_family(family, trans) 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: else:
raise MergeError("Encounter an object of type %s that has " raise MergeError("Encounter an object of type %s that has "
"an event reference." % class_name) "an event reference." % class_name)

View File

@ -194,12 +194,17 @@ class MergeFamilyQuery:
if phoenix_mother: if phoenix_mother:
phoenix_mother.remove_family_handle(old_handle) phoenix_mother.remove_family_handle(old_handle)
self.database.commit_person(phoenix_mother, trans) self.database.commit_person(phoenix_mother, trans)
# replace the family in lds ordinances # replace the family in lds ordinances and notes
for (dummy, person_handle) in self.database.find_backlink_handles( for (ref_obj, ref_handle) in self.database.find_backlink_handles(
old_handle, ['Person']): old_handle, ['Person', 'Note']):
if person_handle in (self.titanic_fh, self.titanic_mh): if ref_handle in (self.titanic_fh, self.titanic_mh):
continue continue
person = self.database.get_person_from_handle(person_handle) obj = self.database.method(
person.replace_handle_reference('Family', old_handle,new_handle) "get_%s_from_handle", ref_obj)(ref_handle)
self.database.commit_person(person, trans) 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) self.database.remove_family(old_handle, trans)

View File

@ -27,7 +27,7 @@ Provide merge capabilities for media objects.
# Gramps modules # 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 ..db import DbTxn
from ..const import GRAMPS_LOCALE as glocale from ..const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext _ = glocale.translation.sgettext
@ -90,6 +90,12 @@ class MergeMediaQuery:
assert(place.has_media_reference(old_handle)) assert(place.has_media_reference(old_handle))
place.replace_media_references(old_handle, new_handle) place.replace_media_references(old_handle, new_handle)
self.database.commit_place(place, trans) 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: else:
raise MergeError("Encounter an object of type % s that has " raise MergeError("Encounter an object of type % s that has "
"a media object reference." % class_name) "a media object reference." % class_name)

View File

@ -28,7 +28,7 @@ Provide merge capabilities for notes.
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..lib import (Person, Family, Event, Place, Source, Citation, Repository, from ..lib import (Person, Family, Event, Place, Source, Citation, Repository,
Media) Media, Note)
from ..db import DbTxn from ..db import DbTxn
from ..const import GRAMPS_LOCALE as glocale from ..const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext _ = glocale.translation.sgettext
@ -99,6 +99,12 @@ class MergeNoteQuery:
assert(repo.has_note_reference(old_handle)) assert(repo.has_note_reference(old_handle))
repo.replace_note_references(old_handle, new_handle) repo.replace_note_references(old_handle, new_handle)
self.database.commit_repository(repo, trans) 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: else:
raise MergeError("Encounter object of type %s that has " raise MergeError("Encounter object of type %s that has "
"a note reference." % class_name) "a note reference." % class_name)

View File

@ -100,14 +100,16 @@ class MergePersonQuery:
spouse.remove_family_handle(family_handle) spouse.remove_family_handle(family_handle)
self.database.commit_person(spouse, trans) self.database.commit_person(spouse, trans)
# replace the family in lds ordinances # replace the family in lds ordinances
for (dummy, person_handle) in self.database.find_backlink_handles( for (ref_obj, ref_handle) in self.database.find_backlink_handles(
family_handle, ['Person']): family_handle, ['Person', 'Note']):
if person_handle == old_handle: if ref_handle == old_handle:
continue continue
person = self.database.get_person_from_handle(person_handle) obj = self.database.method(
person.replace_handle_reference('Family', family_handle, "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) main_family_handle)
self.database.commit_person(person, trans) self.database.method("commit_%s", ref_obj)(obj, trans)
self.database.remove_family(family_handle, trans) self.database.remove_family(family_handle, trans)
self.database.commit_family(main_family, trans) self.database.commit_family(main_family, trans)
@ -133,13 +135,15 @@ class MergePersonQuery:
self.phoenix.merge(self.titanic) self.phoenix.merge(self.titanic)
self.database.commit_person(self.phoenix, trans) self.database.commit_person(self.phoenix, trans)
for (dummy, person_handle) in self.database.find_backlink_handles( for (ref_obj, handle) in self.database.find_backlink_handles(
old_handle, ['Person']): old_handle, ['Person', 'Note']):
person = self.database.get_person_from_handle(person_handle) obj = self.database.method(
assert person.has_handle_reference('Person', old_handle) "get_%s_from_handle", ref_obj)(handle)
person.replace_handle_reference('Person', old_handle, new_handle) assert obj.has_handle_reference('Person', old_handle)
if person_handle != old_handle: obj.replace_handle_reference(
self.database.commit_person(person, trans) '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(): for family_handle in self.phoenix.get_parent_family_handle_list():
family = self.database.get_family_from_handle(family_handle) family = self.database.get_family_from_handle(family_handle)

View File

@ -28,7 +28,7 @@ Provide merge capabilities for places.
# Gramps modules # Gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..lib import Person, Family, Event, Place from ..lib import Person, Family, Event, Place, Note
from ..db import DbTxn from ..db import DbTxn
from ..const import GRAMPS_LOCALE as glocale from ..const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext _ = glocale.translation.sgettext
@ -85,6 +85,12 @@ class MergePlaceQuery:
place.replace_handle_reference('Place', old_handle, place.replace_handle_reference('Place', old_handle,
new_handle) new_handle)
self.database.commit_place(place, trans) 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: else:
raise MergeError("Encounter an object of type %s that has " raise MergeError("Encounter an object of type %s that has "
"a place reference." % class_name) "a place reference." % class_name)

View File

@ -27,7 +27,7 @@ Provide merge capabilities for repositories.
# Gramps modules # Gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..lib import Source from ..lib import Source, Note
from ..db import DbTxn from ..db import DbTxn
from ..const import GRAMPS_LOCALE as glocale from ..const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext _ = glocale.translation.sgettext
@ -65,6 +65,12 @@ class MergeRepositoryQuery:
assert source.has_handle_reference('Repository', old_handle) assert source.has_handle_reference('Repository', old_handle)
source.replace_repo_references(old_handle, new_handle) source.replace_repo_references(old_handle, new_handle)
self.database.commit_source(source, trans) 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: else:
raise MergeError("Encounter an object of type %s that has " raise MergeError("Encounter an object of type %s that has "
"a repository reference." % class_name) "a repository reference." % class_name)

View File

@ -29,8 +29,7 @@ Provide merge capabilities for sources.
# Gramps modules # Gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..lib import (Person, Family, Event, Place, Source, Repository, from ..lib import (Citation, Note)
Media, Citation)
from ..db import DbTxn from ..db import DbTxn
from ..const import GRAMPS_LOCALE as glocale from ..const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext _ = glocale.translation.sgettext
@ -68,6 +67,12 @@ class MergeSourceQuery:
assert(citation.get_reference_handle() == old_handle) assert(citation.get_reference_handle() == old_handle)
citation.set_reference_handle(new_handle) citation.set_reference_handle(new_handle)
self.database.commit_citation(citation, trans) 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: else:
raise MergeError("Encounter an object of type %s that has " raise MergeError("Encounter an object of type %s that has "
"a source reference." % class_name) "a source reference." % class_name)