Trees | Indices | Help |
|
---|
|
1 # 2 # Gramps - a GTK+/GNOME based genealogy program 3 # 4 # Copyright (C) 2004-2006 Donald N. Allingham 5 # 6 # This program is free software; you can redistribute it and/or modify 7 # it under the terms of the GNU General Public License as published by 8 # the Free Software Foundation; either version 2 of the License, or 9 # (at your option) any later version. 10 # 11 # This program is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 # GNU General Public License for more details. 15 # 16 # You should have received a copy of the GNU General Public License 17 # along with this program; if not, write to the Free Software 18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 # 20 21 # $Id: dbutils.py 10103 2008-02-24 13:55:55Z acraphae $ 22 23 from gettext import gettext as _ 24 import copy 25 26 import gen.lib 27 from BasicUtils import UpdateCallback 28 2931 """ 32 Deletes a person from the database, cleaning up all associated references. 33 """ 34 35 # clear out the default person if the person is the default person 36 if db.get_default_person() == person: 37 db.set_default_person_handle(None) 38 39 # loop through the family list 40 for family_handle in [ f for f in person.get_family_handle_list() if f ]: 41 42 family = db.get_family_from_handle(family_handle) 43 44 if person.get_handle() == family.get_father_handle(): 45 family.set_father_handle(None) 46 else: 47 family.set_mother_handle(None) 48 49 if not family.get_father_handle() and not family.get_mother_handle() and \ 50 not family.get_child_ref_list(): 51 db.remove_family(family_handle, trans) 52 else: 53 db.commit_family(family, trans) 54 55 for family_handle in person.get_parent_family_handle_list(): 56 if family_handle: 57 family = db.get_family_from_handle(family_handle) 58 family.remove_child_handle(person.get_handle()) 59 db.commit_family(family, trans) 60 61 handle = person.get_handle() 62 63 person_list = [ 64 item[1] for item in 65 db.find_backlink_handles(handle,['Person'])] 66 67 for phandle in person_list: 68 p = db.get_person_from_handle(phandle) 69 p.remove_handle_references('Person', handle) 70 db.commit_person(person, trans) 71 db.remove_person(handle, trans)7274 family = db.get_family_from_handle(family_handle) 75 76 if trans == None: 77 need_commit = True 78 trans = db.transaction_begin() 79 else: 80 need_commit = False 81 82 for phandle in [ family.get_father_handle(), 83 family.get_mother_handle()]: 84 if phandle: 85 person = db.get_person_from_handle(phandle) 86 person.remove_family_handle(family_handle) 87 db.commit_person(person, trans) 88 89 for ref in family.get_child_ref_list(): 90 phandle = ref.ref 91 person = db.get_person_from_handle(phandle) 92 person.remove_parent_family_handle(family_handle) 93 db.commit_person(person, trans) 94 95 db.remove_family(family_handle, trans) 96 97 if need_commit: 98 db.transaction_commit(trans, _("Remove Family"))99101 """ 102 Remove a person as either the father or mother of a family, 103 deleting the family if it becomes empty. 104 """ 105 person = db.get_person_from_handle(person_handle) 106 family = db.get_family_from_handle(family_handle) 107 108 if trans == None: 109 need_commit = True 110 trans = db.transaction_begin() 111 else: 112 need_commit = False 113 114 person.remove_family_handle(family_handle) 115 if family.get_father_handle() == person_handle: 116 family.set_father_handle(None) 117 msg = _("Remove father from family") 118 elif family.get_mother_handle() == person_handle: 119 msg = _("Remove mother from family") 120 family.set_mother_handle(None) 121 122 child_list = family.get_child_ref_list() 123 if (not family.get_father_handle() and not family.get_mother_handle() and 124 len(child_list) <= 1): 125 db.remove_family(family_handle, trans) 126 if child_list: 127 child = db.get_person_from_handle(child_list[0].ref) 128 child.remove_parent_family_handle(family_handle) 129 db.commit_person(child, trans) 130 else: 131 db.commit_family(family, trans) 132 db.commit_person(person, trans) 133 134 if need_commit: 135 db.transaction_commit(trans,msg)136138 """ 139 Remove a person as a child of the family, deleting the family if 140 it becomes empty. 141 """ 142 person = db.get_person_from_handle(person_handle) 143 family = db.get_family_from_handle(family_handle) 144 person.remove_parent_family_handle(family_handle) 145 family.remove_child_handle(person_handle) 146 147 if trans == None: 148 need_commit = True 149 trans = db.transaction_begin() 150 else: 151 need_commit = False 152 153 child_list = family.get_child_ref_list() 154 if (not family.get_father_handle() and not family.get_mother_handle() and 155 len(child_list) <= 1): 156 db.remove_family(family_handle, trans) 157 if child_list: 158 child = db.get_person_from_handle(child_list[0].ref) 159 child.remove_parent_family_handle(family_handle) 160 db.commit_person(child, trans) 161 else: 162 db.commit_family(family, trans) 163 db.commit_person(person, trans) 164 165 if need_commit: 166 db.transaction_commit(trans,_("Remove child from family"))167169 for eventref in eventref_list: 170 event = db.get_event_from_handle(eventref.ref) 171 if int(event.get_type()) == gen.lib.EventType.MARRIAGE: 172 return event 173 else: 174 return None175176 -def add_child_to_family(db, family, child, 177 mrel=gen.lib.ChildRefType(), 178 frel=gen.lib.ChildRefType(), 179 trans=None):180 181 cref = gen.lib.ChildRef() 182 cref.ref = child.handle 183 cref.set_father_relation(frel) 184 cref.set_mother_relation(mrel) 185 186 family.add_child_ref(cref) 187 child.add_parent_family_handle(family.handle) 188 189 if trans == None: 190 need_commit = True 191 trans = db.transaction_begin() 192 else: 193 need_commit = False 194 195 db.commit_family(family,trans) 196 db.commit_person(child,trans) 197 198 if need_commit: 199 db.transaction_commit(trans, _('Add child to family') )200 201203 person_len = db.get_number_of_people() 204 family_len = db.get_number_of_families() 205 event_len = db.get_number_of_events() 206 source_len = db.get_number_of_sources() 207 place_len = db.get_number_of_places() 208 repo_len = db.get_number_of_repositories() 209 obj_len = db.get_number_of_media_objects() 210 211 return person_len + family_len + event_len + \ 212 place_len + source_len + obj_len + repo_len213215 """ 216 Copy all data in from_db into to_db. 217 218 Both databases must be loaded. 219 It is assumed that to_db is an empty database, 220 so no care is taken to prevent handle collision or merge data. 221 """ 222 223 uc = UpdateCallback(callback) 224 uc.set_total(get_total(from_db)) 225 226 tables = { 227 'Person': {'cursor_func': from_db.get_person_cursor, 228 'add_func' : to_db.add_person, 229 }, 230 'Family': {'cursor_func': from_db.get_family_cursor, 231 'add_func' : to_db.add_family, 232 }, 233 'Event': {'cursor_func': from_db.get_event_cursor, 234 'add_func' : to_db.add_event, 235 }, 236 'Place': {'cursor_func': from_db.get_place_cursor, 237 'add_func' : to_db.add_place, 238 }, 239 'Source': {'cursor_func': from_db.get_source_cursor, 240 'add_func' : to_db.add_source, 241 }, 242 'MediaObject': {'cursor_func': from_db.get_media_cursor, 243 'add_func' : to_db.add_object, 244 }, 245 'Repository': {'cursor_func': from_db.get_repository_cursor, 246 'add_func' : to_db.add_repository, 247 }, 248 'Note': {'cursor_func': from_db.get_note_cursor, 249 'add_func': to_db.add_note, 250 }, 251 } 252 253 # Start batch transaction to use async TXN and other tricks 254 trans = to_db.transaction_begin("", batch=True) 255 256 for table_name in tables.keys(): 257 cursor_func = tables[table_name]['cursor_func'] 258 add_func = tables[table_name]['add_func'] 259 260 cursor = cursor_func() 261 item = cursor.first() 262 while item: 263 (handle,data) = item 264 exec('obj = gen.lib.%s()' % table_name) 265 obj.unserialize(data) 266 add_func(obj,trans) 267 item = cursor.next() 268 uc.update() 269 cursor.close() 270 271 # Copy name grouping 272 group_map = from_db.get_name_group_keys() 273 for key in group_map: 274 value = from_db.get_name_group_mapping(key) 275 to_db.set_name_group_mapping(key, value) 276 277 # Commit batch transaction: does nothing, except undoing the tricks 278 to_db.transaction_commit(trans, "") 279 280 # Copy bookmarks over: 281 # we already know that there's no overlap in handles anywhere 282 to_db.bookmarks = copy.deepcopy(from_db.bookmarks) 283 to_db.family_bookmarks = copy.deepcopy(from_db.family_bookmarks) 284 to_db.event_bookmarks = copy.deepcopy(from_db.event_bookmarks) 285 to_db.source_bookmarks = copy.deepcopy(from_db.source_bookmarks) 286 to_db.place_bookmarks = copy.deepcopy(from_db.place_bookmarks) 287 to_db.media_bookmarks = copy.deepcopy(from_db.media_bookmarks) 288 to_db.repo_bookmarks = copy.deepcopy(from_db.repo_bookmarks) 289 to_db.note_bookmarks = copy.deepcopy(from_db.note_bookmarks) 290 291 # Copy name formats 292 to_db.name_formats = from_db.name_formats 293 294 # Copy db owner 295 to_db.owner = from_db.owner 296 297 # Copy other selected metadata 298 if from_db.get_mediapath() is not None: 299 to_db.set_mediapath(from_db.get_mediapath())300302 birth_ref_index = -1 303 death_ref_index = -1 304 event_ref_list = person.get_event_ref_list() 305 for index in range(len(event_ref_list)): 306 ref = event_ref_list[index] 307 event = db.get_event_from_handle(ref.ref) 308 if (int(event.get_type()) == gen.lib.EventType.BIRTH) \ 309 and (int(ref.get_role()) == gen.lib.EventRoleType.PRIMARY) \ 310 and (birth_ref_index == -1): 311 birth_ref_index = index 312 elif (int(event.get_type()) == gen.lib.EventType.DEATH) \ 313 and (int(ref.get_role()) == gen.lib.EventRoleType.PRIMARY) \ 314 and (death_ref_index == -1): 315 death_ref_index = index 316 317 person.birth_ref_index = birth_ref_index 318 person.death_ref_index = death_ref_index319
Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sun Mar 9 21:53:13 2008 | http://epydoc.sourceforge.net |