def make_cache(self): from gramps.webapp.libdjango import DjangoInterface if self.dji is None: self.dji = DjangoInterface() if isinstance(self, Person): raw = self.dji.get_person(self) elif isinstance(self, Family): raw = self.dji.get_family(self) elif isinstance(self, Place): raw = self.dji.get_place(self) elif isinstance(self, Media): raw = self.dji.get_media(self) elif isinstance(self, Source): raw = self.dji.get_source(self) elif isinstance(self, Citation): raw = self.dji.get_citation(self) elif isinstance(self, Repository): raw = self.dji.get_repository(self) elif isinstance(self, Note): raw = self.dji.get_note(self) elif isinstance(self, Event): raw = self.dji.get_event(self) elif isinstance(self, Tag): raw = self.dji.get_tag(self) else: raise Exception("Don't know how to get raw '%s'" % type(item)) return str(base64.encodebytes(pickle.dumps(raw)), "utf-8")
def __init__(self, db, filename, callback): if not callable(callback): callback = lambda percent: None # dummy self.db = db self.dji = DjangoInterface() self.filename = filename self.callback = callback self.debug = 0
def save(self, commit=True): from gramps.webapp.utils import dp from gramps.webapp.libdjango import DjangoInterface dji = DjangoInterface() model = super(CitationForm, self).save(commit=False) dobj = dp(self.cleaned_data['text']) dji.add_date(model, dobj.serialize()) if commit: model.save() return model
def load(self, directory, callback=None, mode=None, force_schema_upgrade=False, force_bsddb_upgrade=False, force_bsddb_downgrade=False, force_python_upgrade=False): # Django-specific loads: from django.conf import settings LOG.info("Django loading...") default_settings = { "__file__": os.path.join(directory, "default_settings.py") } settings_file = os.path.join(directory, "default_settings.py") with open(settings_file) as f: code = compile(f.read(), settings_file, 'exec') exec(code, globals(), default_settings) class Module(object): def __init__(self, dictionary): self.dictionary = dictionary def __getattr__(self, item): return self.dictionary[item] LOG.info("Django loading defaults from: " + directory) try: settings.configure(Module(default_settings)) except RuntimeError: LOG.info("Django already configured error! Shouldn't happen!") # already configured; ignore import django django.setup() from gramps.webapp.libdjango import DjangoInterface self.dji = DjangoInterface() super().load(directory, callback, mode, force_schema_upgrade, force_bsddb_upgrade, force_bsddb_downgrade, force_python_upgrade)
class Tag(models.Model): handle = models.CharField(max_length=19, unique=True) gramps_id = models.TextField(blank=True, null=True) last_saved = models.DateTimeField('last changed', auto_now=True) last_changed = models.DateTimeField('last changed', null=True, blank=True) # user edits last_changed_by = models.TextField(blank=True, null=True) name = models.TextField('name') color = models.CharField(max_length=13, blank=True, null=True) # "#000000000000" # Black priority = models.IntegerField('priority', blank=True, null=True) cache = models.TextField(blank=True, null=True) dji = None def __unicode__(self): return cuni(self.name) def get_url(self): return cuni("/tag/%s") % self.handle def get_link(self): return cuni("<a href='%s'>%s</a>") % (self.get_url(), self.name) def make_cache(self): from gramps.webapp.libdjango import DjangoInterface if self.dji is None: self.dji = DjangoInterface() raw = self.dji.get_tag(self) return base64.encodestring(pickle.dumps(raw)) def from_cache(self): return pickle.loads(base64.decodestring(self.cache)) def save_cache(self): cache = self.make_cache() if cache != self.cache: self.cache = cache models.Model.save(self) def save(self, *args, **kwargs): save_cache = True if "save_cache" in kwargs: save_cache = kwargs["save_cache"] del kwargs["save_cache"] if not save_cache: self.cache = "" else: self.cache = self.make_cache() models.Model.save(self, *args, **kwargs) # save to db
class Tag(models.Model): handle = models.CharField(max_length=19, unique=True) gramps_id = models.TextField(blank=True, null=True) last_saved = models.DateTimeField('last changed', auto_now=True) last_changed = models.DateTimeField('last changed', null=True, blank=True) # user edits last_changed_by = models.TextField(blank=True, null=True) name = models.TextField('name') color = models.CharField(max_length=13, blank=True, null=True) # "#000000000000" # Black priority = models.IntegerField('priority', blank=True, null=True) cache = models.TextField(blank=True, null=True) dji = None def __str__(self): return str(self.name) def get_url(self): return "/tag/%s" % self.handle def get_link(self): return "<a href='%s'>%s</a>" % (self.get_url(), self.name) def make_cache(self): from gramps.webapp.libdjango import DjangoInterface if self.dji is None: self.dji = DjangoInterface() raw = self.dji.get_tag(self) return str(base64.encodebytes(pickle.dumps(raw)), "utf-8") def from_cache(self): return pickle.loads(base64.decodebytes(bytes(self.cache, "utf-8"))) def save_cache(self): cache = self.make_cache() if cache != self.cache: self.cache = cache models.Model.save(self) def save(self, *args, **kwargs): if "save_cache" in kwargs: self.save_cache_q = kwargs.pop("save_cache") if hasattr(self, "save_cache_q") and self.save_cache_q: # Tag doesn't have a cache self.cache = self.make_cache() models.Model.save(self, *args, **kwargs) # save to db
def load(self, directory, callback=None, mode=None, force_schema_upgrade=False, force_bsddb_upgrade=False, force_bsddb_downgrade=False, force_python_upgrade=False): # Django-specific loads: from django.conf import settings LOG.info("Django loading...") default_settings = {"__file__": os.path.join(directory, "default_settings.py")} settings_file = os.path.join(directory, "default_settings.py") with open(settings_file) as f: code = compile(f.read(), settings_file, 'exec') exec(code, globals(), default_settings) class Module(object): def __init__(self, dictionary): self.dictionary = dictionary def __getattr__(self, item): return self.dictionary[item] LOG.info("Django loading defaults from: " + directory) try: settings.configure(Module(default_settings)) except RuntimeError: LOG.info("Django already configured error! Shouldn't happen!") # already configured; ignore import django django.setup() from gramps.webapp.libdjango import DjangoInterface self.dji = DjangoInterface() super().load(directory, callback, mode, force_schema_upgrade, force_bsddb_upgrade, force_bsddb_downgrade, force_python_upgrade)
class DjangoReader(object): def __init__(self, db, filename, callback): if not callable(callback): callback = lambda percent: None # dummy self.db = db self.dji = DjangoInterface() self.filename = filename self.callback = callback self.debug = 0 def process(self): sql = None total = (self.dji.Note.count() + self.dji.Person.count() + self.dji.Event.count() + self.dji.Family.count() + self.dji.Repository.count() + self.dji.Place.count() + self.dji.Media.count() + self.dji.Citation.count() + self.dji.Source.count() + self.dji.Tag.count()) #self.trans = self.db.transaction_begin("",batch=True) self.db.disable_signals() count = 0.0 self.t = time.time() # --------------------------------- # Process note # --------------------------------- notes = self.dji.Note.all() for note in notes: data = self.dji.get_note(note) self.db.note_map[str(note.handle)] = data count += 1 self.callback(100 * count / total) # --------------------------------- # Process event # --------------------------------- events = self.dji.Event.all() for event in events: data = self.dji.get_event(event) self.db.event_map[str(event.handle)] = data count += 1 self.callback(100 * count / total) # --------------------------------- # Process person # --------------------------------- ## Do this after Events to get the birth/death data people = self.dji.Person.all() for person in people: data = self.dji.get_person(person) self.db.person_map[str(person.handle)] = data count += 1 self.callback(100 * count / total) # --------------------------------- # Process family # --------------------------------- families = self.dji.Family.all() for family in families: data = self.dji.get_family(family) self.db.family_map[str(family.handle)] = data count += 1 self.callback(100 * count / total) # --------------------------------- # Process repository # --------------------------------- repositories = self.dji.Repository.all() for repo in repositories: data = self.dji.get_repository(repo) self.db.repository_map[str(repo.handle)] = data count += 1 self.callback(100 * count / total) # --------------------------------- # Process place # --------------------------------- places = self.dji.Place.all() for place in places: data = self.dji.get_place(place) self.db.place_map[str(place.handle)] = data count += 1 self.callback(100 * count / total) # --------------------------------- # Process citation # --------------------------------- citations = self.dji.Citation.all() for citation in citations: data = self.dji.get_citation(citation) self.db.citation_map[str(citation.handle)] = data count += 1 self.callback(100 * count / total) # --------------------------------- # Process source # --------------------------------- sources = self.dji.Source.all() for source in sources: data = self.dji.get_source(source) self.db.source_map[str(source.handle)] = data count += 1 self.callback(100 * count / total) # --------------------------------- # Process media # --------------------------------- media = self.dji.Media.all() for med in media: data = self.dji.get_media(med) self.db.media_map[str(med.handle)] = data count += 1 self.callback(100 * count / total) # --------------------------------- # Process tag # --------------------------------- tags = self.dji.Tag.all() for tag in tags: data = self.dji.get_tag(tag) self.db.tag_map[str(tag.handle)] = data count += 1 self.callback(100 * count / total) return None def cleanup(self): self.t = time.time() - self.t msg = ngettext('Import Complete: %d second', 'Import Complete: %d seconds', self.t) % self.t #self.db.transaction_commit(self.trans, _("Django import")) self.db.enable_signals() self.db.request_rebuild() LOG.debug(msg)
def export_all(database, filename, error_dialog, option_box=None, callback=None): if not callable(callback): callback = lambda percent: None # dummy start = time.time() total = ( database.get_number_of_notes() + database.get_number_of_people() + database.get_number_of_events() + database.get_number_of_families() + database.get_number_of_repositories() + database.get_number_of_places() + database.get_number_of_media_objects() + database.get_number_of_citations() + database.get_number_of_sources() + database.get_number_of_tags()) * 2 # 2 steps count = 0.0 dji = DjangoInterface() dji.clear_tables("primary", "secondary", "ref") with transaction.commit_on_success(): for step in [0, 1]: LOG.debug("Exporting Step %d..." % (step + 1)) # --------------------------------- # Person # --------------------------------- for person_handle in database.person_map.keys(): data = database.person_map[person_handle] if step == 0: dji.add_person(data) elif step == 1: djperson = dji.add_person_detail(data) djperson.probably_alive = not bool(djperson.death) djperson.save() count += 1 callback(100 * count / total) # --------------------------------- # Notes # --------------------------------- for note_handle in database.note_map.keys(): data = database.note_map[note_handle] if step == 0: dji.add_note(data) count += 1 callback(100 * count / total) # --------------------------------- # Family # --------------------------------- for family_handle in database.family_map.keys(): data = database.family_map[family_handle] if step == 0: dji.add_family(data) elif step == 1: dji.add_family_detail(data) count += 1 callback(100 * count / total) # --------------------------------- # Citation # --------------------------------- for citation_handle in database.citation_map.keys(): data = database.citation_map[citation_handle] if step == 0: dji.add_citation(data) elif step == 1: dji.add_citation_detail(data) count += 1 callback(100 * count / total) # --------------------------------- # Source # --------------------------------- for source_handle in database.source_map.keys(): data = database.source_map[source_handle] if step == 0: dji.add_source(data) elif step == 1: dji.add_source_detail(data) count += 1 callback(100 * count / total) # --------------------------------- # Event # --------------------------------- for event_handle in database.event_map.keys(): data = database.event_map[event_handle] if step == 0: dji.add_event(data) elif step == 1: dji.add_event_detail(data) count += 1 callback(100 * count / total) # --------------------------------- # Repository # --------------------------------- for repository_handle in database.repository_map.keys(): data = database.repository_map[repository_handle] if step == 0: dji.add_repository(data) elif step == 1: dji.add_repository_detail(data) count += 1 callback(100 * count / total) # --------------------------------- # Place # --------------------------------- for place_handle in database.place_map.keys(): data = database.place_map[place_handle] if step == 0: dji.add_place(data) elif step == 1: dji.add_place_detail(data) count += 1 callback(100 * count / total) # --------------------------------- # Media # --------------------------------- for media_handle in database.media_map.keys(): data = database.media_map[media_handle] if step == 0: dji.add_media(data) elif step == 1: dji.add_media_detail(data) count += 1 callback(100 * count / total) # --------------------------------- # Tags # --------------------------------- for tag_handle in database.tag_map.keys(): data = database.tag_map[tag_handle] if step == 0: dji.add_tag(data) elif step == 1: dji.add_tag_detail(data) count += 1 callback(100 * count / total) #dji.rebuild_caches(callback) # not needed anymore, caches get # saved in the add_*_detail methods total_time = time.time() - start msg = ngettext('Export Complete: %d second', 'Export Complete: %d seconds', total_time) % total_time LOG.debug(msg) return True
class DbDjango(DbGeneric): def restore(self): pass def write_version(self, directory): """Write files for a newly created DB.""" versionpath = os.path.join(directory, str(DBBACKEND)) LOG.debug("Write database backend file to 'djangodb'") with open(versionpath, "w") as version_file: version_file.write("djangodb") # Write default_settings, sqlite.db defaults = os.path.join(os.path.dirname(os.path.abspath(__file__)), "django_support", "defaults") LOG.debug("Copy defaults from: " + defaults) for filename in os.listdir(defaults): fullpath = os.path.abspath(os.path.join(defaults, filename)) if os.path.isfile(fullpath): shutil.copy2(fullpath, directory) # force load, to get all modules loaded because of reset issue self.load(directory) def initialize_backend(self, directory): pass def close_backend(self): pass def transaction_commit(self, txn): for (obj_type, trans_type) in txn.keys(): if trans_type in [TXNUPD, TXNADD]: for (handle, new_data) in txn[(obj_type, trans_type)]: if obj_type == PERSON_KEY: self.commit_person_detail(handle, new_data, trans_type, txn.batch) elif obj_type == FAMILY_KEY: self.commit_family_detail(handle, new_data, trans_type, txn.batch) elif obj_type == EVENT_KEY: self.commit_event_detail(handle, new_data, trans_type, txn.batch) elif obj_type == PLACE_KEY: self.commit_place_detail(handle, new_data, trans_type, txn.batch) elif obj_type == REPOSITORY_KEY: self.commit_repository_detail(handle, new_data, trans_type, txn.batch) elif obj_type == CITATION_KEY: self.commit_citation_detail(handle, new_data, trans_type, txn.batch) elif obj_type == SOURCE_KEY: self.commit_source_detail(handle, new_data, trans_type, txn.batch) elif obj_type == NOTE_KEY: self.commit_note_detail(handle, new_data, trans_type, txn.batch) elif obj_type == MEDIA_KEY: self.commit_media_object_detail( handle, new_data, trans_type, txn.batch) elif obj_type == TAG_KEY: self.commit_tag_detail(handle, new_data, trans_type, txn.batch) if txn.batch and self.has_changed: self.rebuild_secondary(None) def transaction_abort(self, txn): pass def get_metadata(self, setting, default=[]): metadata = self.dji.Metadata.filter(setting=setting) if metadata.count() > 0: return pickle.loads(metadata[0].value) elif default == []: return [] else: return default def set_metadata(self, setting, value): from gramps.webapp.grampsdb.models import Metadata metadata = self.dji.Metadata.filter(setting=setting) if metadata.count() > 0: metadata = metadata[0] metadata.value = pickle.dumps(value) else: metadata = Metadata(setting=setting, value=pickle.dumps(value)) metadata.save() def get_name_group_keys(self): rows = self.dji.NameGroup.all().order_by('name') return [row.name for row in rows] def get_name_group_mapping(self, key): rows = self.dji.NameGroup.filter(name=key) if rows: return row[0].name else: return key def get_person_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Person.all().order_by("handle") ] else: return [item.handle for item in self.dji.Person.all()] def get_family_handles(self): return [item.handle for item in self.dji.Family.all()] def get_event_handles(self): return [item.handle for item in self.dji.Event.all()] def get_citation_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Citation.all().order_by("handle") ] else: return [item.handle for item in self.dji.Citation.all()] def get_source_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Source.all().order_by("handle") ] else: return [item.handle for item in self.dji.Source.all()] def get_place_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Place.all().order_by("handle") ] else: return [item.handle for item in self.dji.Place.all()] def get_repository_handles(self): return [item.handle for item in self.dji.Repository.all()] def get_media_object_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Media.all().order_by("handle") ] else: return [item.handle for item in self.dji.Media.all()] def get_note_handles(self): return [item.handle for item in self.dji.Note.all()] def get_tag_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Tag.all().order_by("handle") ] else: return [item.handle for item in self.dji.Tag.all()] def get_tag_from_name(self, name): try: tag = self.dji.Tag.filter(name=name) return self._make_tag(tag[0]) except: return None def get_number_of_people(self): return self.dji.Person.count() def get_number_of_events(self): return self.dji.Event.count() def get_number_of_places(self): return self.dji.Place.count() def get_number_of_tags(self): return self.dji.Tag.count() def get_number_of_families(self): return self.dji.Family.count() def get_number_of_notes(self): return self.dji.Note.count() def get_number_of_citations(self): return self.dji.Citation.count() def get_number_of_sources(self): return self.dji.Source.count() def get_number_of_media_objects(self): return self.dji.Media.count() def get_number_of_repositories(self): return self.dji.Repository.count() def has_name_group_key(self, key): return len(self.dji.NameGroup.filter(name=key)) > 0 def set_name_group_mapping(self, name, grouping): from gramps.webapp.grampsdb.models import NameGroup if self.has_name_group_key(name): namegroup = self.dji.NameGroup.get(name=name) else: namegroup = NameGroup(name=name) namegroup.grouping = grouping namegroup.save() def commit_person(self, person, trans, change_time=None): raw = person.serialize() items = self.dji.Person.filter(handle=person.handle) count = items.count() old = None if count > 0: old = self._get_raw_person_data(person.handle) # delete and re-add items[0].delete() self.dji.add_person(raw) if count > 0: trans.add(PERSON_KEY, TXNUPD, person.handle, old, raw) else: trans.add(PERSON_KEY, TXNADD, person.handle, old, raw) # Contiued in transaction_commit... def commit_person_detail(self, handle, new_data, trans_type, batch): old_obj = self.get_person_from_handle(handle) self.dji.add_person_detail(new_data) obj = self.get_person_from_handle(handle) if trans_type == TXNUPD: if (old_obj.gender != obj.gender or old_obj.primary_name.first_name != obj.primary_name.first_name): self.genderStats.uncount_person(old_obj) self.genderStats.count_person(obj) elif trans_type == TXNADD: self.genderStats.count_person(person) person = obj # Other misc update tasks: self.individual_attributes.update([ str(attr.type) for attr in person.attribute_list if attr.type.is_custom() and str(attr.type) ]) self.event_role_names.update([ str(eref.role) for eref in person.event_ref_list if eref.role.is_custom() ]) self.name_types.update([ str(name.type) for name in ([person.primary_name] + person.alternate_names) if name.type.is_custom() ]) all_surn = [] # new list we will use for storage all_surn += person.primary_name.get_surname_list() for asurname in person.alternate_names: all_surn += asurname.get_surname_list() self.origin_types.update([ str(surn.origintype) for surn in all_surn if surn.origintype.is_custom() ]) all_surn = None self.url_types.update( [str(url.type) for url in person.urls if url.type.is_custom()]) attr_list = [] for mref in person.media_list: attr_list += [ str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type) ] self.media_attributes.update(attr_list) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("person-update", ([handle], )) elif trans_type == TXNADD: self.emit("person-add", ([handle], )) self.has_changed = True def commit_family(self, family, trans, change_time=None): raw = family.serialize() items = self.dji.Family.filter(handle=family.handle) count = items.count() old = None if count > 0: old = self._get_raw_family_data(family.handle) # delete and re-add items[0].delete() self.dji.add_family(family.serialize()) if count > 0: trans.add(FAMILY_KEY, TXNUPD, family.handle, old, raw) else: trans.add(FAMILY_KEY, TXNADD, family.handle, old, raw) # Contiued in transaction_commit... def commit_family_detail(self, handle, new_data, trans_type, batch): self.dji.add_family_detail(new_data) obj = self.get_family_from_handle(handle) family = obj # Misc updates: self.family_attributes.update([ str(attr.type) for attr in family.attribute_list if attr.type.is_custom() and str(attr.type) ]) rel_list = [] for ref in family.child_ref_list: if ref.frel.is_custom(): rel_list.append(str(ref.frel)) if ref.mrel.is_custom(): rel_list.append(str(ref.mrel)) self.child_ref_types.update(rel_list) self.event_role_names.update([ str(eref.role) for eref in family.event_ref_list if eref.role.is_custom() ]) if family.type.is_custom(): self.family_rel_types.add(str(family.type)) attr_list = [] for mref in family.media_list: attr_list += [ str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type) ] self.media_attributes.update(attr_list) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("family-update", ([handle], )) elif trans_type == TXNADD: self.emit("family-add", ([handle], )) self.has_changed = True def commit_citation(self, citation, trans, change_time=None): raw = citation.serialize() items = self.dji.Citation.filter(handle=citation.handle) count = items.count() old = None if count > 0: old = self._get_raw_citation_data(citation.handle) # delete and re-add items[0].delete() self.dji.add_citation(citation.serialize()) if count > 0: trans.add(CITATION_KEY, TXNUPD, citation.handle, old, raw) else: trans.add(CITATION_KEY, TXNADD, citation.handle, old, raw) # Contiued in transaction_commit... def commit_citation_detail(self, handle, new_data, trans_type, batch): self.dji.add_citation_detail(new_data) obj = self.get_citation_from_handle(handle) citation = obj # Misc updates: attr_list = [] for mref in citation.media_list: attr_list += [ str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type) ] self.media_attributes.update(attr_list) self.source_attributes.update([ str(attr.type) for attr in citation.attribute_list if attr.type.is_custom() and str(attr.type) ]) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("citation-update", ([handle], )) elif trans_type == TXNADD: self.emit("citation-add", ([handle], )) self.has_changed = True def commit_source(self, source, trans, change_time=None): raw = source.serialize() items = self.dji.Source.filter(handle=source.handle) count = items.count() old = None if count > 0: old = self._get_raw_source_data(source.handle) # delete and re-add items[0].delete() self.dji.add_source(source.serialize()) if count > 0: trans.add(SOURCE_KEY, TXNUPD, source.handle, old, raw) else: trans.add(SOURCE_KEY, TXNADD, source.handle, old, raw) # Contiued in transaction_commit... def commit_source_detail(self, handle, new_data, trans_type, batch): self.dji.add_source_detail(new_data) obj = self.get_source_from_handle(handle) source = obj # Misc updates: self.source_media_types.update([ str(ref.media_type) for ref in source.reporef_list if ref.media_type.is_custom() ]) attr_list = [] for mref in source.media_list: attr_list += [ str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type) ] self.media_attributes.update(attr_list) self.source_attributes.update([ str(attr.type) for attr in source.attribute_list if attr.type.is_custom() and str(attr.type) ]) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("source-update", ([handle], )) elif trans_type == TXNADD: self.emit("source-add", ([handle], )) self.has_changed = True def commit_repository(self, repository, trans, change_time=None): raw = repository.serialize() items = self.dji.Repository.filter(handle=repository.handle) count = items.count() old = None if count > 0: old = self._get_raw_repository_data(repository.handle) # delete and re-add items[0].delete() self.dji.add_repository(repository.serialize()) if count > 0: trans.add(REPOSITORY_KEY, TXNUPD, repository.handle, old, raw) else: trans.add(REPOSITORY_KEY, TXNADD, repository.handle, old, raw) # Contiued in transaction_commit... def commit_repository_detail(self, handle, new_data, trans_type, batch): self.dji.add_repository_detail(new_data) obj = self.get_repository_from_handle(handle) repository = obj # Misc updates: if repository.type.is_custom(): self.repository_types.add(str(repository.type)) self.url_types.update( [str(url.type) for url in repository.urls if url.type.is_custom()]) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("repository-update", ([handle], )) elif trans_type == TXNADD: self.emit("repository-add", ([handle], )) self.has_changed = True def commit_note(self, note, trans, change_time=None): raw = note.serialize() items = self.dji.Note.filter(handle=note.handle) count = items.count() old = None if count > 0: old = self._get_raw_note_data(note.handle) # delete and re-add items[0].delete() self.dji.add_note(note.serialize()) if count > 0: trans.add(NOTE_KEY, TXNUPD, note.handle, old, raw) else: trans.add(NOTE_KEY, TXNADD, note.handle, old, raw) # Contiued in transaction_commit... def commit_note_detail(self, handle, new_data, trans_type, batch): self.dji.add_note_detail(new_data) obj = self.get_note_from_handle(handle) note = obj # Misc updates: if note.type.is_custom(): self.note_types.add(str(note.type)) # Emit after added: if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("note-update", ([handle], )) elif trans_type == TXNADD: self.emit("note-add", ([handle], )) self.has_changed = True def commit_place(self, place, trans, change_time=None): raw = place.serialize() items = self.dji.Place.filter(handle=place.handle) count = items.count() old = None if count > 0: old = self._get_raw_place_data(place.handle) # delete and re-add items[0].delete() self.dji.add_place(place.serialize()) if count > 0: trans.add(PLACE_KEY, TXNUPD, place.handle, old, raw) else: trans.add(PLACE_KEY, TXNADD, place.handle, old, raw) # Contiued in transaction_commit... def commit_place_detail(self, handle, new_data, trans_type, batch): self.dji.add_place_detail(new_data) obj = self.get_place_from_handle(handle) place = obj # Misc updates: if place.get_type().is_custom(): self.place_types.add(str(place.get_type())) self.url_types.update( [str(url.type) for url in place.urls if url.type.is_custom()]) attr_list = [] for mref in place.media_list: attr_list += [ str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type) ] self.media_attributes.update(attr_list) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("place-update", ([handle], )) elif trans_type == TXNADD: self.emit("place-add", ([handle], )) self.has_changed = True def commit_event(self, event, trans, change_time=None): raw = event.serialize() items = self.dji.Event.filter(handle=event.handle) count = items.count() old = None if count > 0: old = self._get_raw_event_data(event.handle) # delete and re-add items[0].delete() self.dji.add_event(event.serialize()) if count > 0: trans.add(EVENT_KEY, TXNUPD, event.handle, old, raw) else: trans.add(EVENT_KEY, TXNADD, event.handle, old, raw) # Contiued in transaction_commit... def commit_event_detail(self, handle, new_data, trans_type, batch): self.dji.add_event_detail(new_data) obj = self.get_event_from_handle(handle) event = obj # Misc updates: self.event_attributes.update([ str(attr.type) for attr in event.attribute_list if attr.type.is_custom() and str(attr.type) ]) if event.type.is_custom(): self.event_names.add(str(event.type)) attr_list = [] for mref in event.media_list: attr_list += [ str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type) ] self.media_attributes.update(attr_list) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("event-update", ([handle], )) elif trans_type == TXNADD: self.emit("event-add", ([handle], )) self.has_changed = True def commit_tag(self, tag, trans, change_time=None): raw = tag.serialize() items = self.dji.Tag.filter(handle=tag.handle) count = items.count() old = None if count > 0: old = self._get_raw_tag_data(tag.handle) # delete and re-add items[0].delete() self.dji.add_tag(tag.serialize()) if count > 0: trans.add(TAG_KEY, TXNUPD, tag.handle, old, raw) else: trans.add(TAG_KEY, TXNADD, tag.handle, old, raw) # Contiued in transaction_commit... def commit_tag_detail(self, handle, new_data, trans_type, batch): self.dji.add_tag_detail(new_data) obj = self.get_tag_from_handle(handle) tag = obj if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("tag-update", ([handle], )) elif trans_type == TXNADD: self.emit("tag-add", ([handle], )) self.has_changed = True def commit_media_object(self, media, trans, change_time=None): """ Commit the specified MediaObject to the database, storing the changes as part of the transaction. """ raw = media.serialize() items = self.dji.Media.filter(handle=media.handle) count = items.count() old = None if count > 0: old = self._get_raw_media_data(media.handle) # delete and re-add items[0].delete() self.dji.add_media(media.serialize()) if count > 0: trans.add(MEDIA_KEY, TXNUPD, media.handle, old, raw) else: trans.add(MEDIA_KEY, TXNADD, media.handle, old, raw) # Contiued in transaction_commit... def commit_media_object_detail(self, handle, new_data, trans_type, batch): self.dji.add_media_detail(new_data) obj = self.get_object_from_handle(handle) media = obj # Misc updates: self.media_attributes.update([ str(attr.type) for attr in media.attribute_list if attr.type.is_custom() and str(attr.type) ]) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("media-update", ([handle], )) elif trans_type == TXNADD: self.emit("media-add", ([handle], )) self.has_changed = True def find_backlink_handles(self, handle, include_classes=None): """ Find all objects that hold a reference to the object handle. Returns an interator over a list of (class_name, handle) tuples. :param handle: handle of the object to search for. :type handle: database handle :param include_classes: list of class names to include in the results. Default: None means include all classes. :type include_classes: list of class names Note that this is a generator function, it returns a iterator for use in loops. If you want a list of the results use:: result_list = list(find_backlink_handles(handle)) """ rows = self.dji.Reference.filter(ref_handle=handle) for row in rows: if (include_classes is None) or (row.obj_class in include_classes): yield (row.obj_class, row.obj_handle) def update_backlinks(self, obj): from gramps.webapp.grampsdb.models import Reference # First, delete the current references: self.dji.Reference.filter(obj_handle=obj.handle).delete() # Now, add the current ones: references = set(obj.get_referenced_handles_recursively()) for (ref_class_name, ref_handle) in references: reference = Reference(obj_handle=obj.handle, obj_class=obj.__class__.__name__, ref_handle=ref_handle, ref_class=ref_class_name) reference.save() # Removals: def remove_person(self, handle, transaction): self.dji.Person.filter(handle=handle)[0].delete() self.emit("person-delete", ([handle], )) def _do_remove(self, handle, transaction, data_map, data_id_map, key): key2table = { PERSON_KEY: "person", FAMILY_KEY: "family", SOURCE_KEY: "source", CITATION_KEY: "citation", EVENT_KEY: "event", MEDIA_KEY: "media", PLACE_KEY: "place", REPOSITORY_KEY: "repository", NOTE_KEY: "note", TAG_KEY: "tag", } table = getattr(self.dji, key2table[key].title()) table.filter(handle=handle)[0].delete() self.emit("%s-delete" % key2table[key], ([handle], )) def find_initial_person(self): handle = self.get_default_handle() person = None if handle: person = self.get_person_from_handle(handle) if person: return person if len(self.dji.Person.all()) > 0: person = self.dji.Person.all()[0] return self.get_person_from_handle(person.handle) def iter_person_handles(self): return (person.handle for person in self.dji.Person.all()) def iter_family_handles(self): return (family.handle for family in self.dji.Family.all()) def iter_citation_handles(self): return (citation.handle for citation in self.dji.Citation.all()) def iter_event_handles(self): return (event.handle for event in self.dji.Event.all()) def iter_media_object_handles(self): return (media.handle for media in self.dji.Media.all()) def iter_note_handles(self): return (note.handle for note in self.dji.Note.all()) def iter_place_handles(self): return (place.handle for place in self.dji.Place.all()) def iter_repository_handles(self): return (repository.handle for repository in self.dji.Repository.all()) def iter_source_handles(self): return (source.handle for source in self.dji.Source.all()) def iter_tag_handles(self): return (tag.handle for tag in self.dji.Tag.all()) def reindex_reference_map(self, callback): from gramps.webapp.grampsdb.models import Reference callback(4) self.dji.Reference.all().delete() primary_table = ( (self.get_person_cursor, Person), (self.get_family_cursor, Family), (self.get_event_cursor, Event), (self.get_place_cursor, Place), (self.get_source_cursor, Source), (self.get_citation_cursor, Citation), (self.get_media_cursor, MediaObject), (self.get_repository_cursor, Repository), (self.get_note_cursor, Note), (self.get_tag_cursor, Tag), ) # Now we use the functions and classes defined above # to loop through each of the primary object tables. for cursor_func, class_func in primary_table: logging.info("Rebuilding %s reference map" % class_func.__name__) with cursor_func() as cursor: for found_handle, val in cursor: obj = class_func.create(val) references = set(obj.get_referenced_handles_recursively()) # handle addition of new references for (ref_class_name, ref_handle) in references: reference = Reference(obj_handle=obj.handle, obj_class=obj.__class__.__name__, ref_handle=ref_handle, ref_class=ref_class_name) reference.save() callback(5) def rebuild_secondary(self, update): gstats = self.rebuild_gender_stats() self.genderStats = GenderStats(gstats) def has_handle_for_person(self, key): return self.dji.Person.filter(handle=key).count() > 0 def has_handle_for_family(self, key): return self.dji.Family.filter(handle=key).count() > 0 def has_handle_for_source(self, key): return self.dji.Source.filter(handle=key).count() > 0 def has_handle_for_citation(self, key): return self.dji.Citation.filter(handle=key).count() > 0 def has_handle_for_event(self, key): return self.dji.Event.filter(handle=key).count() > 0 def has_handle_for_media(self, key): return self.dji.Media.filter(handle=key).count() > 0 def has_handle_for_place(self, key): return self.dji.Place.filter(handle=key).count() > 0 def has_handle_for_repository(self, key): return self.dji.Repository.filter(handle=key).count() > 0 def has_handle_for_note(self, key): return self.dji.Note.filter(handle=key).count() > 0 def has_handle_for_tag(self, key): return self.dji.Tag.filter(handle=key).count() > 0 def has_gramps_id_for_person(self, key): return self.dji.Person.filter(gramps_id=key).count() > 0 def has_gramps_id_for_family(self, key): return self.dji.Family.filter(gramps_id=key).count() > 0 def has_gramps_id_for_source(self, key): return self.dji.Source.filter(gramps_id=key).count() > 0 def has_gramps_id_for_citation(self, key): return self.dji.Citation.filter(gramps_id=key).count() > 0 def has_gramps_id_for_event(self, key): return self.dji.Event.filter(gramps_id=key).count() > 0 def has_gramps_id_for_media(self, key): return self.dji.Media.filter(gramps_id=key).count() > 0 def has_gramps_id_for_place(self, key): return self.dji.Place.filter(gramps_id=key).count() > 0 def has_gramps_id_for_repository(self, key): return self.dji.Repository.filter(gramps_id=key).count() > 0 def has_gramps_id_for_note(self, key): return self.dji.Note.filter(gramps_id=key).count() > 0 def get_person_gramps_ids(self): return [x.gramps_id for x in self.dji.Person.all()] def get_family_gramps_ids(self): return [x.gramps_id for x in self.dji.Family.all()] def get_source_gramps_ids(self): return [x.gramps_id for x in self.dji.Source.all()] def get_citation_gramps_ids(self): return [x.gramps_id for x in self.dji.Citation.all()] def get_event_gramps_ids(self): return [x.gramps_id for x in self.dji.Event.all()] def get_media_gramps_ids(self): return [x.gramps_id for x in self.dji.Media.all()] def get_place_gramps_ids(self): return [x.gramps_id for x in self.dji.Place.all()] def get_repository_gramps_ids(self): return [x.gramps_id for x in self.dji.Repository.all()] def get_note_gramps_ids(self): return [x.gramps_id for x in self.dji.Note.all()] def _get_raw_person_data(self, key): return self.dji.get_person(self.dji.Person.get(handle=key)) def _get_raw_person_from_id_data(self, key): return self.dji.get_person(self.dji.Person.get(gramps_id=key)) def _get_raw_family_data(self, key): return self.dji.get_family(self.dji.Family.get(handle=key)) def _get_raw_family_from_id_data(self, key): return self.dji.get_family(self.dji.Family.get(gramps_id=key)) def _get_raw_source_data(self, key): return self.dji.get_source(self.dji.Source.get(handle=key)) def _get_raw_source_from_id_data(self, key): return self.dji.get_source(self.dji.Source.get(gramps_id=key)) def _get_raw_citation_data(self, key): return self.dji.get_citation(self.dji.Citation.get(handle=key)) def _get_raw_citation_from_id_data(self, key): return self.dji.get_citation(self.dji.Citation.get(gramps_id=key)) def _get_raw_event_data(self, key): return self.dji.get_event(self.dji.Event.get(handle=key)) def _get_raw_event_from_id_data(self, key): return self.dji.get_event(self.dji.Event.get(gramps_id=key)) def _get_raw_media_data(self, key): return self.dji.get_media(self.dji.Media.get(handle=key)) def _get_raw_media_from_id_data(self, key): return self.dji.get_media(self.dji.Media.get(gramps_id=key)) def _get_raw_place_data(self, key): return self.dji.get_place(self.dji.Place.get(handle=key)) def _get_raw_place_from_id_data(self, key): return self.dji.get_place(self.dji.Place.get(gramps_id=key)) def _get_raw_repository_data(self, key): return self.dji.get_repository(self.dji.Repository.get(handle=key)) def _get_raw_repository_from_id_data(self, key): return self.dji.get_repository(self.dji.Repository.get(gramps_id=key)) def _get_raw_note_data(self, key): return self.dji.get_note(self.dji.Note.get(handle=key)) def _get_raw_note_from_id_data(self, key): return self.dji.get_note(self.dji.Note.get(gramps_id=key)) def _get_raw_tag_data(self, key): return self.dji.get_tag(self.dji.Tag.get(handle=key)) def rebuild_gender_stats(self): """ Returns a dictionary of {given_name: (male_count, female_count, unknown_count)} Not called: this is a database-efficient version """ UNKNOWN = 2 MALE = 1 FEMALE = 0 self.dji.GenderStats.all().delete() gstats = {} for person in self.dji.Person.all(): for first_name in person.name_set.all(): for name in first_name.first_name.split(): if name not in gstats: gstats[name] = [0, 0, 0] if person.gender_type.val == MALE: gstats[name][0] += 1 elif person.gender_type.val == FEMALE: gstats[name][1] += 1 else: gstats[name][2] += 1 for key in gstats: gstats[key] = tuple(gstats[key]) return gstats def save_gender_stats(self, genderStats): """ {name: (male_count, female_count, unknown_count), ...} """ from gramps.webapp.grampsdb.models import GenderStats self.dji.GenderStats.all().delete() gstats = genderStats.stats for key in gstats: data = gstats[key] stat = GenderStats(name=key, male=data[0], female=data[1], unknown=data[2]) stat.save() def get_gender_stats(self): """ Returns a dictionary of {given_name: (male_count, female_count, unknown_count)} """ rows = self.dji.GenderStats.values('name', 'male', 'female', 'unknown') gstats = {} for dict in rows: gstats[dict['name']] = (dict['male'], dict['female'], dict['unknown']) return gstats def get_surname_list(self): return [ x['surname'] for x in self.dji.Surname.values('surname').order_by( 'surname').distinct() ] def save_surname_list(self): # Nothing to do pass def build_surname_list(self): # Nothing to do pass def drop_tables(self): # Nothing to do pass def load(self, directory, callback=None, mode=None, force_schema_upgrade=False, force_bsddb_upgrade=False, force_bsddb_downgrade=False, force_python_upgrade=False): # Django-specific loads: from django.conf import settings LOG.info("Django loading...") default_settings = { "__file__": os.path.join(directory, "default_settings.py") } settings_file = os.path.join(directory, "default_settings.py") with open(settings_file) as f: code = compile(f.read(), settings_file, 'exec') exec(code, globals(), default_settings) class Module(object): def __init__(self, dictionary): self.dictionary = dictionary def __getattr__(self, item): return self.dictionary[item] LOG.info("Django loading defaults from: " + directory) try: settings.configure(Module(default_settings)) except RuntimeError: LOG.info("Django already configured error! Shouldn't happen!") # already configured; ignore import django django.setup() from gramps.webapp.libdjango import DjangoInterface self.dji = DjangoInterface() super().load(directory, callback, mode, force_schema_upgrade, force_bsddb_upgrade, force_bsddb_downgrade, force_python_upgrade) def _make_repository(self, repository): if self.use_db_cache and repository.cache: data = repository.from_cache() else: data = self.dji.get_repository(repository) return Repository.create(data) def _make_citation(self, citation): if self.use_db_cache and citation.cache: data = citation.from_cache() else: data = self.dji.get_citation(citation) return Citation.create(data) def _make_source(self, source): if self.use_db_cache and source.cache: data = source.from_cache() else: data = self.dji.get_source(source) return Source.create(data) def _make_family(self, family): if self.use_db_cache and family.cache: data = family.from_cache() else: data = self.dji.get_family(family) return Family.create(data) def _make_person(self, person): if self.use_db_cache and person.cache: data = person.from_cache() else: data = self.dji.get_person(person) return Person.create(data) def _make_event(self, event): if self.use_db_cache and event.cache: data = event.from_cache() else: data = self.dji.get_event(event) return Event.create(data) def _make_note(self, note): if self.use_db_cache and note.cache: data = note.from_cache() else: data = self.dji.get_note(note) return Note.create(data) def _make_tag(self, tag): data = self.dji.get_tag(tag) return Tag.create(data) def _make_place(self, place): if self.use_db_cache and place.cache: data = place.from_cache() else: data = self.dji.get_place(place) return Place.create(data) def _make_media(self, media): if self.use_db_cache and media.cache: data = media.from_cache() else: data = self.dji.get_media(media) return MediaObject.create(data) def request_rebuild(self): # override # caches are ok, but let's compute public's self.dji.update_publics() super().request_rebuild()
def export_all(database, filename, error_dialog, option_box=None, callback=None): if not callable(callback): callback = lambda percent: None # dummy start = time.time() total = (database.get_number_of_notes() + database.get_number_of_people() + database.get_number_of_events() + database.get_number_of_families() + database.get_number_of_repositories() + database.get_number_of_places() + database.get_number_of_media_objects() + database.get_number_of_citations() + database.get_number_of_sources() + database.get_number_of_tags()) * 2 # 2 steps count = 0.0 dji = DjangoInterface() dji.clear_tables("primary", "secondary", "ref") with transaction.commit_on_success(): for step in [0, 1]: LOG.debug("Exporting Step %d..." % (step + 1)) # --------------------------------- # Person # --------------------------------- for person_handle in database.person_map.keys(): data = database.person_map[person_handle] if step == 0: dji.add_person(data) elif step == 1: djperson = dji.add_person_detail(data) djperson.probably_alive = not bool(djperson.death) djperson.save() count += 1 callback(100 * count/total) # --------------------------------- # Notes # --------------------------------- for note_handle in database.note_map.keys(): data = database.note_map[note_handle] if step == 0: dji.add_note(data) count += 1 callback(100 * count/total) # --------------------------------- # Family # --------------------------------- for family_handle in database.family_map.keys(): data = database.family_map[family_handle] if step == 0: dji.add_family(data) elif step == 1: dji.add_family_detail(data) count += 1 callback(100 * count/total) # --------------------------------- # Citation # --------------------------------- for citation_handle in database.citation_map.keys(): data = database.citation_map[citation_handle] if step == 0: dji.add_citation(data) elif step == 1: dji.add_citation_detail(data) count += 1 callback(100 * count/total) # --------------------------------- # Source # --------------------------------- for source_handle in database.source_map.keys(): data = database.source_map[source_handle] if step == 0: dji.add_source(data) elif step == 1: dji.add_source_detail(data) count += 1 callback(100 * count/total) # --------------------------------- # Event # --------------------------------- for event_handle in database.event_map.keys(): data = database.event_map[event_handle] if step == 0: dji.add_event(data) elif step == 1: dji.add_event_detail(data) count += 1 callback(100 * count/total) # --------------------------------- # Repository # --------------------------------- for repository_handle in database.repository_map.keys(): data = database.repository_map[repository_handle] if step == 0: dji.add_repository(data) elif step == 1: dji.add_repository_detail(data) count += 1 callback(100 * count/total) # --------------------------------- # Place # --------------------------------- for place_handle in database.place_map.keys(): data = database.place_map[place_handle] if step == 0: dji.add_place(data) elif step == 1: dji.add_place_detail(data) count += 1 callback(100 * count/total) # --------------------------------- # Media # --------------------------------- for media_handle in database.media_map.keys(): data = database.media_map[media_handle] if step == 0: dji.add_media(data) elif step == 1: dji.add_media_detail(data) count += 1 callback(100 * count/total) # --------------------------------- # Tags # --------------------------------- for tag_handle in database.tag_map.keys(): data = database.tag_map[tag_handle] if step == 0: dji.add_tag(data) elif step == 1: dji.add_tag_detail(data) count += 1 callback(100 * count/total) #dji.rebuild_caches(callback) # not needed anymore, caches get # saved in the add_*_detail methods total_time = time.time() - start msg = ngettext('Export Complete: %d second','Export Complete: %d seconds', total_time ) % total_time LOG.debug(msg) return True
def __init__(self): DbReadBase.__init__(self) DbWriteBase.__init__(self) self._tables = { 'Person': { "handle_func": self.get_person_from_handle, "gramps_id_func": self.get_person_from_gramps_id, "class_func": gramps.gen.lib.Person, "cursor_func": self.get_person_cursor, "handles_func": self.get_person_handles, "iter_func": self.iter_people, }, 'Family': { "handle_func": self.get_family_from_handle, "gramps_id_func": self.get_family_from_gramps_id, "class_func": gramps.gen.lib.Family, "cursor_func": self.get_family_cursor, "handles_func": self.get_family_handles, "iter_func": self.iter_families, }, 'Source': { "handle_func": self.get_source_from_handle, "gramps_id_func": self.get_source_from_gramps_id, "class_func": gramps.gen.lib.Source, "cursor_func": self.get_source_cursor, "handles_func": self.get_source_handles, "iter_func": self.iter_sources, }, 'Citation': { "handle_func": self.get_citation_from_handle, "gramps_id_func": self.get_citation_from_gramps_id, "class_func": gramps.gen.lib.Citation, "cursor_func": self.get_citation_cursor, "handles_func": self.get_citation_handles, "iter_func": self.iter_citations, }, 'Event': { "handle_func": self.get_event_from_handle, "gramps_id_func": self.get_event_from_gramps_id, "class_func": gramps.gen.lib.Event, "cursor_func": self.get_event_cursor, "handles_func": self.get_event_handles, "iter_func": self.iter_events, }, 'Media': { "handle_func": self.get_object_from_handle, "gramps_id_func": self.get_object_from_gramps_id, "class_func": gramps.gen.lib.MediaObject, "cursor_func": self.get_media_cursor, "handles_func": self.get_media_object_handles, "iter_func": self.iter_media_objects, }, 'Place': { "handle_func": self.get_place_from_handle, "gramps_id_func": self.get_place_from_gramps_id, "class_func": gramps.gen.lib.Place, "cursor_func": self.get_place_cursor, "handles_func": self.get_place_handles, "iter_func": self.iter_places, }, 'Repository': { "handle_func": self.get_repository_from_handle, "gramps_id_func": self.get_repository_from_gramps_id, "class_func": gramps.gen.lib.Repository, "cursor_func": self.get_repository_cursor, "handles_func": self.get_repository_handles, "iter_func": self.iter_repositories, }, 'Note': { "handle_func": self.get_note_from_handle, "gramps_id_func": self.get_note_from_gramps_id, "class_func": gramps.gen.lib.Note, "cursor_func": self.get_note_cursor, "handles_func": self.get_note_handles, "iter_func": self.iter_notes, }, 'Tag': { "handle_func": self.get_tag_from_handle, "gramps_id_func": None, "class_func": gramps.gen.lib.Tag, "cursor_func": self.get_tag_cursor, "handles_func": self.get_tag_handles, "iter_func": self.iter_tags, }, } # skip GEDCOM cross-ref check for now: self.set_feature("skip-check-xref", True) self.dji = DjangoInterface() self.readonly = False self.db_is_open = True self.name_formats = [] self.bookmarks = Bookmarks() self.family_bookmarks = Bookmarks() self.event_bookmarks = Bookmarks() self.place_bookmarks = Bookmarks() self.citation_bookmarks = Bookmarks() self.source_bookmarks = Bookmarks() self.repo_bookmarks = Bookmarks() self.media_bookmarks = Bookmarks() self.note_bookmarks = Bookmarks() self.set_person_id_prefix('I%04d') self.set_object_id_prefix('O%04d') self.set_family_id_prefix('F%04d') self.set_citation_id_prefix('C%04d') self.set_source_id_prefix('S%04d') self.set_place_id_prefix('P%04d') self.set_event_id_prefix('E%04d') self.set_repository_id_prefix('R%04d') self.set_note_id_prefix('N%04d') # ---------------------------------- self.id_trans = DjangoTxn("ID Transaction", self, self.dji.Person) self.fid_trans = DjangoTxn("FID Transaction", self, self.dji.Family) self.pid_trans = DjangoTxn("PID Transaction", self, self.dji.Place) self.cid_trans = DjangoTxn("CID Transaction", self, self.dji.Citation) self.sid_trans = DjangoTxn("SID Transaction", self, self.dji.Source) self.oid_trans = DjangoTxn("OID Transaction", self, self.dji.Media) self.rid_trans = DjangoTxn("RID Transaction", self, self.dji.Repository) self.nid_trans = DjangoTxn("NID Transaction", self, self.dji.Note) self.eid_trans = DjangoTxn("EID Transaction", self, self.dji.Event) self.cmap_index = 0 self.smap_index = 0 self.emap_index = 0 self.pmap_index = 0 self.fmap_index = 0 self.lmap_index = 0 self.omap_index = 0 self.rmap_index = 0 self.nmap_index = 0 self.env = None self.person_map = {} self.family_map = {} self.place_map = {} self.citation_map = {} self.source_map = {} self.repository_map = {} self.note_map = {} self.media_map = {} self.event_map = {} self.metadata = {} self.name_group = {} self.undo_callback = None self.redo_callback = None self.undo_history_callback = None self.modified = 0 self.txn = DjangoTxn("DbDjango Transaction", self) self.transaction = None # Import cache for gedcom import, uses transactions, and # two step adding of objects. self.import_cache = {} self.use_import_cache = False self.use_db_cache = True
class DbDjango(DbWriteBase, DbReadBase): """ A Gramps Database Backend. This replicates the grampsdb functions. """ def __init__(self): DbReadBase.__init__(self) DbWriteBase.__init__(self) self._tables = { 'Person': { "handle_func": self.get_person_from_handle, "gramps_id_func": self.get_person_from_gramps_id, "class_func": gramps.gen.lib.Person, "cursor_func": self.get_person_cursor, "handles_func": self.get_person_handles, "iter_func": self.iter_people, }, 'Family': { "handle_func": self.get_family_from_handle, "gramps_id_func": self.get_family_from_gramps_id, "class_func": gramps.gen.lib.Family, "cursor_func": self.get_family_cursor, "handles_func": self.get_family_handles, "iter_func": self.iter_families, }, 'Source': { "handle_func": self.get_source_from_handle, "gramps_id_func": self.get_source_from_gramps_id, "class_func": gramps.gen.lib.Source, "cursor_func": self.get_source_cursor, "handles_func": self.get_source_handles, "iter_func": self.iter_sources, }, 'Citation': { "handle_func": self.get_citation_from_handle, "gramps_id_func": self.get_citation_from_gramps_id, "class_func": gramps.gen.lib.Citation, "cursor_func": self.get_citation_cursor, "handles_func": self.get_citation_handles, "iter_func": self.iter_citations, }, 'Event': { "handle_func": self.get_event_from_handle, "gramps_id_func": self.get_event_from_gramps_id, "class_func": gramps.gen.lib.Event, "cursor_func": self.get_event_cursor, "handles_func": self.get_event_handles, "iter_func": self.iter_events, }, 'Media': { "handle_func": self.get_object_from_handle, "gramps_id_func": self.get_object_from_gramps_id, "class_func": gramps.gen.lib.MediaObject, "cursor_func": self.get_media_cursor, "handles_func": self.get_media_object_handles, "iter_func": self.iter_media_objects, }, 'Place': { "handle_func": self.get_place_from_handle, "gramps_id_func": self.get_place_from_gramps_id, "class_func": gramps.gen.lib.Place, "cursor_func": self.get_place_cursor, "handles_func": self.get_place_handles, "iter_func": self.iter_places, }, 'Repository': { "handle_func": self.get_repository_from_handle, "gramps_id_func": self.get_repository_from_gramps_id, "class_func": gramps.gen.lib.Repository, "cursor_func": self.get_repository_cursor, "handles_func": self.get_repository_handles, "iter_func": self.iter_repositories, }, 'Note': { "handle_func": self.get_note_from_handle, "gramps_id_func": self.get_note_from_gramps_id, "class_func": gramps.gen.lib.Note, "cursor_func": self.get_note_cursor, "handles_func": self.get_note_handles, "iter_func": self.iter_notes, }, 'Tag': { "handle_func": self.get_tag_from_handle, "gramps_id_func": None, "class_func": gramps.gen.lib.Tag, "cursor_func": self.get_tag_cursor, "handles_func": self.get_tag_handles, "iter_func": self.iter_tags, }, } # skip GEDCOM cross-ref check for now: self.set_feature("skip-check-xref", True) self.dji = DjangoInterface() self.readonly = False self.db_is_open = True self.name_formats = [] self.bookmarks = Bookmarks() self.family_bookmarks = Bookmarks() self.event_bookmarks = Bookmarks() self.place_bookmarks = Bookmarks() self.citation_bookmarks = Bookmarks() self.source_bookmarks = Bookmarks() self.repo_bookmarks = Bookmarks() self.media_bookmarks = Bookmarks() self.note_bookmarks = Bookmarks() self.set_person_id_prefix('I%04d') self.set_object_id_prefix('O%04d') self.set_family_id_prefix('F%04d') self.set_citation_id_prefix('C%04d') self.set_source_id_prefix('S%04d') self.set_place_id_prefix('P%04d') self.set_event_id_prefix('E%04d') self.set_repository_id_prefix('R%04d') self.set_note_id_prefix('N%04d') # ---------------------------------- self.id_trans = DjangoTxn("ID Transaction", self, self.dji.Person) self.fid_trans = DjangoTxn("FID Transaction", self, self.dji.Family) self.pid_trans = DjangoTxn("PID Transaction", self, self.dji.Place) self.cid_trans = DjangoTxn("CID Transaction", self, self.dji.Citation) self.sid_trans = DjangoTxn("SID Transaction", self, self.dji.Source) self.oid_trans = DjangoTxn("OID Transaction", self, self.dji.Media) self.rid_trans = DjangoTxn("RID Transaction", self, self.dji.Repository) self.nid_trans = DjangoTxn("NID Transaction", self, self.dji.Note) self.eid_trans = DjangoTxn("EID Transaction", self, self.dji.Event) self.cmap_index = 0 self.smap_index = 0 self.emap_index = 0 self.pmap_index = 0 self.fmap_index = 0 self.lmap_index = 0 self.omap_index = 0 self.rmap_index = 0 self.nmap_index = 0 self.env = None self.person_map = {} self.family_map = {} self.place_map = {} self.citation_map = {} self.source_map = {} self.repository_map = {} self.note_map = {} self.media_map = {} self.event_map = {} self.metadata = {} self.name_group = {} self.undo_callback = None self.redo_callback = None self.undo_history_callback = None self.modified = 0 self.txn = DjangoTxn("DbDjango Transaction", self) self.transaction = None # Import cache for gedcom import, uses transactions, and # two step adding of objects. self.import_cache = {} self.use_import_cache = False self.use_db_cache = True def prepare_import(self): """ DbDjango does not commit data on gedcom import, but saves them for later commit. """ self.use_import_cache = True self.import_cache = {} @transaction.commit_on_success def commit_import(self): """ Commits the items that were queued up during the last gedcom import for two step adding. """ # First we add the primary objects: for key in list(self.import_cache.keys()): obj = self.import_cache[key] if isinstance(obj, Person): self.dji.add_person(obj.serialize()) elif isinstance(obj, Family): self.dji.add_family(obj.serialize()) elif isinstance(obj, Event): self.dji.add_event(obj.serialize()) elif isinstance(obj, Place): self.dji.add_place(obj.serialize()) elif isinstance(obj, Repository): self.dji.add_repository(obj.serialize()) elif isinstance(obj, Citation): self.dji.add_citation(obj.serialize()) elif isinstance(obj, Source): self.dji.add_source(obj.serialize()) elif isinstance(obj, Note): self.dji.add_note(obj.serialize()) elif isinstance(obj, MediaObject): self.dji.add_media(obj.serialize()) elif isinstance(obj, Tag): self.dji.add_tag(obj.serialize()) # Next we add the links: for key in list(self.import_cache.keys()): obj = self.import_cache[key] if isinstance(obj, Person): self.dji.add_person_detail(obj.serialize()) elif isinstance(obj, Family): self.dji.add_family_detail(obj.serialize()) elif isinstance(obj, Event): self.dji.add_event_detail(obj.serialize()) elif isinstance(obj, Place): self.dji.add_place_detail(obj.serialize()) elif isinstance(obj, Repository): self.dji.add_repository_detail(obj.serialize()) elif isinstance(obj, Citation): self.dji.add_citation_detail(obj.serialize()) elif isinstance(obj, Source): self.dji.add_source_detail(obj.serialize()) elif isinstance(obj, Note): self.dji.add_note_detail(obj.serialize()) elif isinstance(obj, MediaObject): self.dji.add_media_detail(obj.serialize()) elif isinstance(obj, Tag): self.dji.add_tag_detail(obj.serialize()) self.use_import_cache = False self.import_cache = {} self.dji.update_publics() def transaction_commit(self, txn): pass def enable_signals(self): pass def request_rebuild(self): # caches are ok, but let's compute public's self.dji.update_publics() def get_undodb(self): return None def transaction_abort(self, txn): pass @staticmethod def _validated_id_prefix(val, default): if isinstance(val, str) and val: try: str_ = val % 1 except TypeError: # missing conversion specifier prefix_var = val + "%d" except ValueError: # incomplete format prefix_var = default + "%04d" else: prefix_var = val # OK as given else: prefix_var = default + "%04d" # not a string or empty string return prefix_var @staticmethod def __id2user_format(id_pattern): """ Return a method that accepts a Gramps ID and adjusts it to the users format. """ pattern_match = re.match(r"(.*)%[0 ](\d+)[diu]$", id_pattern) if pattern_match: str_prefix = pattern_match.group(1) nr_width = pattern_match.group(2) def closure_func(gramps_id): if gramps_id and gramps_id.startswith(str_prefix): id_number = gramps_id[len(str_prefix):] if id_number.isdigit(): id_value = int(id_number, 10) #if len(str(id_value)) > nr_width: # # The ID to be imported is too large to fit in the # # users format. For now just create a new ID, # # because that is also what happens with IDs that # # are identical to IDs already in the database. If # # the problem of colliding import and already # # present IDs is solved the code here also needs # # some solution. # gramps_id = id_pattern % 1 #else: gramps_id = id_pattern % id_value return gramps_id else: def closure_func(gramps_id): return gramps_id return closure_func def set_person_id_prefix(self, val): """ Set the naming template for GRAMPS Person ID values. The string is expected to be in the form of a simple text string, or in a format that contains a C/Python style format string using %d, such as I%d or I%04d. """ self.person_prefix = self._validated_id_prefix(val, "I") self.id2user_format = self.__id2user_format(self.person_prefix) def set_citation_id_prefix(self, val): """ Set the naming template for GRAMPS Citation ID values. The string is expected to be in the form of a simple text string, or in a format that contains a C/Python style format string using %d, such as C%d or C%04d. """ self.citation_prefix = self._validated_id_prefix(val, "C") self.cid2user_format = self.__id2user_format(self.citation_prefix) def set_source_id_prefix(self, val): """ Set the naming template for GRAMPS Source ID values. The string is expected to be in the form of a simple text string, or in a format that contains a C/Python style format string using %d, such as S%d or S%04d. """ self.source_prefix = self._validated_id_prefix(val, "S") self.sid2user_format = self.__id2user_format(self.source_prefix) def set_object_id_prefix(self, val): """ Set the naming template for GRAMPS MediaObject ID values. The string is expected to be in the form of a simple text string, or in a format that contains a C/Python style format string using %d, such as O%d or O%04d. """ self.mediaobject_prefix = self._validated_id_prefix(val, "O") self.oid2user_format = self.__id2user_format(self.mediaobject_prefix) def set_place_id_prefix(self, val): """ Set the naming template for GRAMPS Place ID values. The string is expected to be in the form of a simple text string, or in a format that contains a C/Python style format string using %d, such as P%d or P%04d. """ self.place_prefix = self._validated_id_prefix(val, "P") self.pid2user_format = self.__id2user_format(self.place_prefix) def set_family_id_prefix(self, val): """ Set the naming template for GRAMPS Family ID values. The string is expected to be in the form of a simple text string, or in a format that contains a C/Python style format string using %d, such as F%d or F%04d. """ self.family_prefix = self._validated_id_prefix(val, "F") self.fid2user_format = self.__id2user_format(self.family_prefix) def set_event_id_prefix(self, val): """ Set the naming template for GRAMPS Event ID values. The string is expected to be in the form of a simple text string, or in a format that contains a C/Python style format string using %d, such as E%d or E%04d. """ self.event_prefix = self._validated_id_prefix(val, "E") self.eid2user_format = self.__id2user_format(self.event_prefix) def set_repository_id_prefix(self, val): """ Set the naming template for GRAMPS Repository ID values. The string is expected to be in the form of a simple text string, or in a format that contains a C/Python style format string using %d, such as R%d or R%04d. """ self.repository_prefix = self._validated_id_prefix(val, "R") self.rid2user_format = self.__id2user_format(self.repository_prefix) def set_note_id_prefix(self, val): """ Set the naming template for GRAMPS Note ID values. The string is expected to be in the form of a simple text string, or in a format that contains a C/Python style format string using %d, such as N%d or N%04d. """ self.note_prefix = self._validated_id_prefix(val, "N") self.nid2user_format = self.__id2user_format(self.note_prefix) def __find_next_gramps_id(self, prefix, map_index, trans): """ Helper function for find_next_<object>_gramps_id methods """ index = prefix % map_index while trans.get(str(index), txn=self.txn) is not None: map_index += 1 index = prefix % map_index map_index += 1 return (map_index, index) def find_next_person_gramps_id(self): """ Return the next available GRAMPS' ID for a Person object based off the person ID prefix. """ self.pmap_index, gid = self.__find_next_gramps_id( self.person_prefix, self.pmap_index, self.id_trans) return gid def find_next_place_gramps_id(self): """ Return the next available GRAMPS' ID for a Place object based off the place ID prefix. """ self.lmap_index, gid = self.__find_next_gramps_id( self.place_prefix, self.lmap_index, self.pid_trans) return gid def find_next_event_gramps_id(self): """ Return the next available GRAMPS' ID for a Event object based off the event ID prefix. """ self.emap_index, gid = self.__find_next_gramps_id( self.event_prefix, self.emap_index, self.eid_trans) return gid def find_next_object_gramps_id(self): """ Return the next available GRAMPS' ID for a MediaObject object based off the media object ID prefix. """ self.omap_index, gid = self.__find_next_gramps_id( self.mediaobject_prefix, self.omap_index, self.oid_trans) return gid def find_next_citation_gramps_id(self): """ Return the next available GRAMPS' ID for a Citation object based off the citation ID prefix. """ self.cmap_index, gid = self.__find_next_gramps_id( self.citation_prefix, self.cmap_index, self.cid_trans) return gid def find_next_source_gramps_id(self): """ Return the next available GRAMPS' ID for a Source object based off the source ID prefix. """ self.smap_index, gid = self.__find_next_gramps_id( self.source_prefix, self.smap_index, self.sid_trans) return gid def find_next_family_gramps_id(self): """ Return the next available GRAMPS' ID for a Family object based off the family ID prefix. """ self.fmap_index, gid = self.__find_next_gramps_id( self.family_prefix, self.fmap_index, self.fid_trans) return gid def find_next_repository_gramps_id(self): """ Return the next available GRAMPS' ID for a Respository object based off the repository ID prefix. """ self.rmap_index, gid = self.__find_next_gramps_id( self.repository_prefix, self.rmap_index, self.rid_trans) return gid def find_next_note_gramps_id(self): """ Return the next available GRAMPS' ID for a Note object based off the note ID prefix. """ self.nmap_index, gid = self.__find_next_gramps_id( self.note_prefix, self.nmap_index, self.nid_trans) return gid def get_mediapath(self): return None def get_name_group_keys(self): return [] def get_name_group_mapping(self, key): return None def get_researcher(self): obj = Researcher() return obj def get_tag_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Tag.all().order_by("handle") ] else: return [item.handle for item in self.dji.Tag.all()] def get_person_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Person.all().order_by("handle") ] else: return [item.handle for item in self.dji.Person.all()] def get_family_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Family.all().order_by("handle") ] else: return [item.handle for item in self.dji.Family.all()] def get_event_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Event.all().order_by("handle") ] else: return [item.handle for item in self.dji.Event.all()] def get_citation_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Citation.all().order_by("handle") ] else: return [item.handle for item in self.dji.Citation.all()] def get_source_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Source.all().order_by("handle") ] else: return [item.handle for item in self.dji.Source.all()] def get_place_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Place.all().order_by("handle") ] else: return [item.handle for item in self.dji.Place.all()] def get_repository_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Repository.all().order_by("handle") ] else: return [item.handle for item in self.dji.Repository.all()] def get_media_object_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Media.all().order_by("handle") ] else: return [item.handle for item in self.dji.Media.all()] def get_note_handles(self, sort_handles=False): if sort_handles: return [ item.handle for item in self.dji.Note.all().order_by("handle") ] else: return [item.handle for item in self.dji.Note.all()] def get_media_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: media = self.dji.Media.get(handle=handle) except: return None return self.make_media(media) def get_event_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: event = self.dji.Event.get(handle=handle) except: return None return self.make_event(event) def get_family_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: family = self.dji.Family.get(handle=handle) except: return None return self.make_family(family) def get_family_from_gramps_id(self, gramps_id): if self.import_cache: for handle in self.import_cache: if self.import_cache[handle].gramps_id == gramps_id: return self.import_cache[handle] try: family = self.dji.Family.get(gramps_id=gramps_id) except: return None return self.make_family(family) def get_repository_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: repository = self.dji.Repository.get(handle=handle) except: return None return self.make_repository(repository) def get_person_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: person = self.dji.Person.get(handle=handle) except: return None return self.make_person(person) def get_tag_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: tag = self.dji.Tag.get(handle=handle) except: return None return self.make_tag(tag) def make_repository(self, repository): if self.use_db_cache and repository.cache: data = repository.from_cache() else: data = self.dji.get_repository(repository) return Repository.create(data) def make_citation(self, citation): if self.use_db_cache and citation.cache: data = citation.from_cache() else: data = self.dji.get_citation(citation) return Citation.create(data) def make_source(self, source): if self.use_db_cache and source.cache: data = source.from_cache() else: data = self.dji.get_source(source) return Source.create(data) def make_family(self, family): if self.use_db_cache and family.cache: data = family.from_cache() else: data = self.dji.get_family(family) return Family.create(data) def make_person(self, person): if self.use_db_cache and person.cache: data = person.from_cache() else: data = self.dji.get_person(person) return Person.create(data) def make_event(self, event): if self.use_db_cache and event.cache: data = event.from_cache() else: data = self.dji.get_event(event) return Event.create(data) def make_note(self, note): if self.use_db_cache and note.cache: data = note.from_cache() else: data = self.dji.get_note(note) return Note.create(data) def make_tag(self, tag): data = self.dji.get_tag(tag) return Tag.create(data) def make_place(self, place): if self.use_db_cache and place.cache: data = place.from_cache() else: data = self.dji.get_place(place) return Place.create(data) def make_media(self, media): if self.use_db_cache and media.cache: data = media.from_cache() else: data = self.dji.get_media(media) return MediaObject.create(data) def get_place_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: place = self.dji.Place.get(handle=handle) except: return None return self.make_place(place) def get_citation_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: citation = self.dji.Citation.get(handle=handle) except: return None return self.make_citation(citation) def get_source_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: source = self.dji.Source.get(handle=handle) except: return None return self.make_source(source) def get_note_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: note = self.dji.Note.get(handle=handle) except: return None return self.make_note(note) def get_object_from_handle(self, handle): if handle in self.import_cache: return self.import_cache[handle] try: media = self.dji.Media.get(handle=handle) except: return None return self.make_media(media) def get_default_person(self): return None def iter_people(self): return (self.get_person_from_handle(person.handle) for person in self.dji.Person.all()) def iter_person_handles(self): return (person.handle for person in self.dji.Person.all()) def iter_families(self): return (self.get_family_from_handle(family.handle) for family in self.dji.Family.all()) def iter_family_handles(self): return (family.handle for family in self.dji.Family.all()) def iter_notes(self): return (self.get_note_from_handle(note.handle) for note in self.dji.Note.all()) def iter_note_handles(self): return (note.handle for note in self.dji.Note.all()) def iter_events(self): return (self.get_event_from_handle(event.handle) for event in self.dji.Event.all()) def iter_event_handles(self): return (event.handle for event in self.dji.Event.all()) def iter_places(self): return (self.get_place_from_handle(place.handle) for place in self.dji.Place.all()) def iter_place_handles(self): return (place.handle for place in self.dji.Place.all()) def iter_repositories(self): return (self.get_repository_from_handle(repository.handle) for repository in self.dji.Repository.all()) def iter_repository_handles(self): return (repository.handle for repository in self.dji.Repository.all()) def iter_sources(self): return (self.get_source_from_handle(source.handle) for source in self.dji.Source.all()) def iter_source_handles(self): return (source.handle for source in self.dji.Source.all()) def iter_citations(self): return (self.get_citation_from_handle(citation.handle) for citation in self.dji.Citation.all()) def iter_citation_handles(self): return (citation.handle for citation in self.dji.Citation.all()) def iter_tags(self): return (self.get_tag_from_handle(tag.handle) for tag in self.dji.Tag.all()) def iter_tag_handles(self): return (tag.handle for tag in self.dji.Tag.all()) def iter_media_objects(self): return (self.get_media_from_handle(media.handle) for media in self.dji.Media.all()) def get_tag_from_name(self, name): try: tag = self.dji.Tag.filter(name=name) return self.make_tag(tag[0]) except: return None def get_person_from_gramps_id(self, gramps_id): if self.import_cache: for handle in self.import_cache: if self.import_cache[handle].gramps_id == gramps_id: return self.import_cache[handle] match_list = self.dji.Person.filter(gramps_id=gramps_id) if match_list.count() > 0: return self.make_person(match_list[0]) else: return None def get_number_of_people(self): return self.dji.Person.count() def get_number_of_events(self): return self.dji.Event.count() def get_number_of_places(self): return self.dji.Place.count() def get_number_of_tags(self): return self.dji.Tag.count() def get_number_of_families(self): return self.dji.Family.count() def get_number_of_notes(self): return self.dji.Note.count() def get_number_of_citations(self): return self.dji.Citation.count() def get_number_of_sources(self): return self.dji.Source.count() def get_number_of_media_objects(self): return self.dji.Media.count() def get_number_of_repositories(self): return self.dji.Repository.count() def get_place_cursor(self): return Cursor(self.dji.Place, self.get_raw_place_data).iter() def get_person_cursor(self): return Cursor(self.dji.Person, self.get_raw_person_data).iter() def get_family_cursor(self): return Cursor(self.dji.Family, self.get_raw_family_data).iter() def get_event_cursor(self): return Cursor(self.dji.Event, self.get_raw_event_data).iter() def get_citation_cursor(self): return Cursor(self.dji.Citation, self.get_raw_citation_data).iter() def get_source_cursor(self): return Cursor(self.dji.Source, self.get_raw_source_data).iter() def get_note_cursor(self): return Cursor(self.dji.Note, self.get_raw_note_data).iter() def get_tag_cursor(self): return Cursor(self.dji.Tag, self.get_raw_tag_data).iter() def get_repository_cursor(self): return Cursor(self.dji.Repository, self.get_raw_repository_data).iter() def get_media_cursor(self): return Cursor(self.dji.Media, self.get_raw_object_data).iter() def has_gramps_id(self, obj_key, gramps_id): key2table = { PERSON_KEY: self.dji.Person, FAMILY_KEY: self.dji.Family, SOURCE_KEY: self.dji.Source, CITATION_KEY: self.dji.Citation, EVENT_KEY: self.dji.Event, MEDIA_KEY: self.dji.Media, PLACE_KEY: self.dji.Place, REPOSITORY_KEY: self.dji.Repository, NOTE_KEY: self.dji.Note, } table = key2table[obj_key] return table.filter(gramps_id=gramps_id).count() > 0 def has_person_handle(self, handle): if handle in self.import_cache: return True return self.dji.Person.filter(handle=handle).count() == 1 def has_family_handle(self, handle): if handle in self.import_cache: return True return self.dji.Family.filter(handle=handle).count() == 1 def has_citation_handle(self, handle): if handle in self.import_cache: return True return self.dji.Citation.filter(handle=handle).count() == 1 def has_source_handle(self, handle): if handle in self.import_cache: return True return self.dji.Source.filter(handle=handle).count() == 1 def has_repository_handle(self, handle): if handle in self.import_cache: return True return self.dji.Repository.filter(handle=handle).count() == 1 def has_note_handle(self, handle): if handle in self.import_cache: return True return self.dji.Note.filter(handle=handle).count() == 1 def has_place_handle(self, handle): if handle in self.import_cache: return True return self.dji.Place.filter(handle=handle).count() == 1 def has_event_handle(self, handle): if handle in self.import_cache: return True return self.dji.Event.filter(handle=handle).count() == 1 def has_tag_handle(self, handle): if handle in self.import_cache: return True return self.dji.Tag.filter(handle=handle).count() == 1 def has_object_handle(self, handle): if handle in self.import_cache: return True return self.dji.Media.filter(handle=handle).count() == 1 def has_name_group_key(self, key): # FIXME: return False def set_name_group_mapping(self, key, value): # FIXME: pass def set_default_person_handle(self, handle): pass def set_mediapath(self, mediapath): pass def get_raw_person_data(self, handle): try: return self.dji.get_person(self.dji.Person.get(handle=handle)) except: if handle in self.import_cache: return self.import_cache[handle].serialize() else: return None def get_raw_family_data(self, handle): try: return self.dji.get_family(self.dji.Family.get(handle=handle)) except: if handle in self.import_cache: return self.import_cache[handle].serialize() else: return None def get_raw_citation_data(self, handle): try: return self.dji.get_citation(self.dji.Citation.get(handle=handle)) except: if handle in self.import_cache: return self.import_cache[handle].serialize() else: return None def get_raw_source_data(self, handle): try: return self.dji.get_source(self.dji.Source.get(handle=handle)) except: if handle in self.import_cache: return self.import_cache[handle].serialize() else: return None def get_raw_repository_data(self, handle): try: return self.dji.get_repository( self.dji.Repository.get(handle=handle)) except: if handle in self.import_cache: return self.import_cache[handle].serialize() else: return None def get_raw_note_data(self, handle): try: return self.dji.get_note(self.dji.Note.get(handle=handle)) except: if handle in self.import_cache: return self.import_cache[handle].serialize() else: return None def get_raw_place_data(self, handle): try: return self.dji.get_place(self.dji.Place.get(handle=handle)) except: if handle in self.import_cache: return self.import_cache[handle].serialize() else: return None def get_raw_object_data(self, handle): try: return self.dji.get_media(self.dji.Media.get(handle=handle)) except: if handle in self.import_cache: return self.import_cache[handle].serialize() else: return None def get_raw_tag_data(self, handle): try: return self.dji.get_tag(self.dji.Tag.get(handle=handle)) except: if handle in self.import_cache: return self.import_cache[handle].serialize() else: return None def get_raw_event_data(self, handle): try: return self.dji.get_event(self.dji.Event.get(handle=handle)) except: if handle in self.import_cache: return self.import_cache[handle].serialize() else: return None def add_person(self, person, trans, set_gid=True): if not person.handle: person.handle = create_id() if not person.gramps_id and set_gid: person.gramps_id = self.find_next_person_gramps_id() self.commit_person(person, trans) return person.handle def add_family(self, family, trans, set_gid=True): if not family.handle: family.handle = create_id() if not family.gramps_id and set_gid: family.gramps_id = self.find_next_family_gramps_id() self.commit_family(family, trans) return family.handle def add_citation(self, citation, trans, set_gid=True): if not citation.handle: citation.handle = create_id() if not citation.gramps_id and set_gid: citation.gramps_id = self.find_next_citation_gramps_id() self.commit_citation(citation, trans) return citation.handle def add_source(self, source, trans, set_gid=True): if not source.handle: source.handle = create_id() if not source.gramps_id and set_gid: source.gramps_id = self.find_next_source_gramps_id() self.commit_source(source, trans) return source.handle def add_repository(self, repository, trans, set_gid=True): if not repository.handle: repository.handle = create_id() if not repository.gramps_id and set_gid: repository.gramps_id = self.find_next_repository_gramps_id() self.commit_repository(repository, trans) return repository.handle def add_note(self, note, trans, set_gid=True): if not note.handle: note.handle = create_id() if not note.gramps_id and set_gid: note.gramps_id = self.find_next_note_gramps_id() self.commit_note(note, trans) return note.handle def add_place(self, place, trans, set_gid=True): if not place.handle: place.handle = create_id() if not place.gramps_id and set_gid: place.gramps_id = self.find_next_place_gramps_id() self.commit_place(place, trans) return place.handle def add_event(self, event, trans, set_gid=True): if not event.handle: event.handle = create_id() if not event.gramps_id and set_gid: event.gramps_id = self.find_next_event_gramps_id() self.commit_event(event, trans) return event.handle def add_tag(self, tag, trans): if not tag.handle: tag.handle = create_id() self.commit_event(tag, trans) return tag.handle def add_object(self, obj, transaction, set_gid=True): """ Add a MediaObject to the database, assigning internal IDs if they have not already been defined. If not set_gid, then gramps_id is not set. """ if not obj.handle: obj.handle = create_id() if not obj.gramps_id and set_gid: obj.gramps_id = self.find_next_object_gramps_id() self.commit_media_object(obj, transaction) return obj.handle def commit_person(self, person, trans, change_time=None): self.import_cache[person.handle] = person def commit_family(self, family, trans, change_time=None): self.import_cache[family.handle] = family def commit_citation(self, citation, trans, change_time=None): self.import_cache[citation.handle] = citation def commit_source(self, source, trans, change_time=None): self.import_cache[source.handle] = source def commit_repository(self, repository, trans, change_time=None): self.import_cache[repository.handle] = repository def commit_note(self, note, trans, change_time=None): self.import_cache[note.handle] = note def commit_place(self, place, trans, change_time=None): self.import_cache[place.handle] = place def commit_event(self, event, trans, change_time=None): self.import_cache[event.handle] = event def commit_tag(self, tag, trans, change_time=None): self.import_cache[tag.handle] = tag def commit_media_object(self, obj, transaction, change_time=None): """ Commit the specified MediaObject to the database, storing the changes as part of the transaction. """ self.import_cache[obj.handle] = obj def get_gramps_ids(self, obj_key): key2table = { PERSON_KEY: self.id_trans, FAMILY_KEY: self.fid_trans, CITATION_KEY: self.cid_trans, SOURCE_KEY: self.sid_trans, EVENT_KEY: self.eid_trans, MEDIA_KEY: self.oid_trans, PLACE_KEY: self.pid_trans, REPOSITORY_KEY: self.rid_trans, NOTE_KEY: self.nid_trans, } table = key2table[obj_key] return list(table.keys()) def transaction_begin(self, transaction): return def disable_signals(self): pass def set_researcher(self, owner): pass def copy_from_db(self, db): """ A (possibily) implementation-specific method to get data from db into this database. """ # First we add the primary objects: for key in db._tables.keys(): cursor = db._tables[key]["cursor_func"] for (handle, data) in cursor(): if key == "Person": self.dji.add_person(data) elif key == "Family": self.dji.add_family(data) elif key == "Event": self.dji.add_event(data) elif key == "Place": self.dji.add_place(data) elif key == "Repository": self.dji.add_repository(data) elif key == "Citation": self.dji.add_citation(data) elif key == "Source": self.dji.add_source(data) elif key == "Note": self.dji.add_note(data) elif key == "Media": self.dji.add_media(data) elif key == "Tag": self.dji.add_tag(data) for key in db._tables.keys(): cursor = db._tables[key]["cursor_func"] for (handle, data) in cursor(): if key == "Person": self.dji.add_person_detail(data) elif key == "Family": self.dji.add_family_detail(data) elif key == "Event": self.dji.add_event_detail(data) elif key == "Place": self.dji.add_place_detail(data) elif key == "Repository": self.dji.add_repository_detail(data) elif key == "Citation": self.dji.add_citation_detail(data) elif key == "Source": self.dji.add_source_detail(data) elif key == "Note": self.dji.add_note_detail(data) elif key == "Media": self.dji.add_media_detail(data) elif key == "Tag": self.dji.add_tag_detail(data) # Next we add the links: self.dji.update_publics() def get_from_name_and_handle(self, table_name, handle): """ Returns a gen.lib object (or None) given table_name and handle. Examples: >>> self.get_from_name_and_handle("Person", "a7ad62365bc652387008") >>> self.get_from_name_and_handle("Media", "c3434653675bcd736f23") """ if table_name in self._tables: return self._tables[table_name]["handle_func"](handle) return None
def make_cache(self): from gramps.webapp.libdjango import DjangoInterface if self.dji is None: self.dji = DjangoInterface() raw = self.dji.get_tag(self) return str(base64.encodebytes(pickle.dumps(raw)), "utf-8")
class PrimaryObject(models.Model): """ Common attribute of all primary objects with key on the handle """ class Meta: abstract = True ## Fields: id = models.AutoField(primary_key=True) handle = models.CharField(max_length=19, unique=True) gramps_id = models.CharField('ID', max_length=25, blank=True) last_saved = models.DateTimeField('last changed', auto_now=True) last_changed = models.DateTimeField('last changed', null=True, blank=True) # user edits last_changed_by = models.TextField(blank=True, null=True) private = models.BooleanField('private', default=True) public = models.BooleanField('public', default=True) #attributes = models.ManyToManyField("Attribute", blank=True, null=True) cache = models.TextField(blank=True, null=True) tags = models.ManyToManyField('Tag', blank=True, null=True) dji = None save_cache_q = False def __str__(self): return "%s: %s" % (self.__class__.__name__, self.gramps_id) def get_url(self): return "/%s/%s" % (self.__class__.__name__.lower(), self.handle) def get_tag_list(self): return [tag.handle for tag in self.tags.all()] def make_cache(self): from gramps.webapp.libdjango import DjangoInterface if self.dji is None: self.dji = DjangoInterface() if isinstance(self, Person): raw = self.dji.get_person(self) elif isinstance(self, Family): raw = self.dji.get_family(self) elif isinstance(self, Place): raw = self.dji.get_place(self) elif isinstance(self, Media): raw = self.dji.get_media(self) elif isinstance(self, Source): raw = self.dji.get_source(self) elif isinstance(self, Citation): raw = self.dji.get_citation(self) elif isinstance(self, Repository): raw = self.dji.get_repository(self) elif isinstance(self, Note): raw = self.dji.get_note(self) elif isinstance(self, Event): raw = self.dji.get_event(self) elif isinstance(self, Tag): raw = self.dji.get_tag(self) else: raise Exception("Don't know how to get raw '%s'" % type(item)) return str(base64.encodebytes(pickle.dumps(raw)), "utf-8") def from_cache(self): return pickle.loads(base64.decodebytes(bytes(self.cache, "utf-8"))) def save_cache(self): cache = self.make_cache() if cache != self.cache: self.cache = cache models.Model.save(self) def save(self, *args, **kwargs): if "save_cache" in kwargs: self.save_cache_q = kwargs.pop("save_cache") if self.save_cache_q: self.cache = self.make_cache() models.Model.save(self, *args, **kwargs) # save to db
class DjangoReader(object): def __init__(self, db, filename, callback): if not callable(callback): callback = lambda percent: None # dummy self.db = db self.dji = DjangoInterface() self.filename = filename self.callback = callback self.debug = 0 def process(self): sql = None total = (self.dji.Note.count() + self.dji.Person.count() + self.dji.Event.count() + self.dji.Family.count() + self.dji.Repository.count() + self.dji.Place.count() + self.dji.Media.count() + self.dji.Citation.count() + self.dji.Source.count() + self.dji.Tag.count()) #self.trans = self.db.transaction_begin("",batch=True) self.db.disable_signals() count = 0.0 self.t = time.time() # --------------------------------- # Process note # --------------------------------- notes = self.dji.Note.all() for note in notes: data = self.dji.get_note(note) self.db.note_map[str(note.handle)] = data count += 1 self.callback(100 * count/total) # --------------------------------- # Process event # --------------------------------- events = self.dji.Event.all() for event in events: data = self.dji.get_event(event) self.db.event_map[str(event.handle)] = data count += 1 self.callback(100 * count/total) # --------------------------------- # Process person # --------------------------------- ## Do this after Events to get the birth/death data people = self.dji.Person.all() for person in people: data = self.dji.get_person(person) self.db.person_map[str(person.handle)] = data count += 1 self.callback(100 * count/total) # --------------------------------- # Process family # --------------------------------- families = self.dji.Family.all() for family in families: data = self.dji.get_family(family) self.db.family_map[str(family.handle)] = data count += 1 self.callback(100 * count/total) # --------------------------------- # Process repository # --------------------------------- repositories = self.dji.Repository.all() for repo in repositories: data = self.dji.get_repository(repo) self.db.repository_map[str(repo.handle)] = data count += 1 self.callback(100 * count/total) # --------------------------------- # Process place # --------------------------------- places = self.dji.Place.all() for place in places: data = self.dji.get_place(place) self.db.place_map[str(place.handle)] = data count += 1 self.callback(100 * count/total) # --------------------------------- # Process citation # --------------------------------- citations = self.dji.Citation.all() for citation in citations: data = self.dji.get_citation(citation) self.db.citation_map[str(citation.handle)] = data count += 1 self.callback(100 * count/total) # --------------------------------- # Process source # --------------------------------- sources = self.dji.Source.all() for source in sources: data = self.dji.get_source(source) self.db.source_map[str(source.handle)] = data count += 1 self.callback(100 * count/total) # --------------------------------- # Process media # --------------------------------- media = self.dji.Media.all() for med in media: data = self.dji.get_media(med) self.db.media_map[str(med.handle)] = data count += 1 self.callback(100 * count/total) # --------------------------------- # Process tag # --------------------------------- tags = self.dji.Tag.all() for tag in tags: data = self.dji.get_tag(tag) self.db.tag_map[str(tag.handle)] = data count += 1 self.callback(100 * count/total) return None def cleanup(self): self.t = time.time() - self.t msg = ngettext('Import Complete: %d second','Import Complete: %d seconds', self.t ) % self.t #self.db.transaction_commit(self.trans, _("Django import")) self.db.enable_signals() self.db.request_rebuild() LOG.debug(msg)
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # """ Views for Person, Name, and Surname """ ## Gramps Modules from gramps.webapp.utils import _, boolean, update_last_changed, build_search from gramps.webapp.grampsdb.models import Source from gramps.webapp.grampsdb.forms import * from gramps.webapp.libdjango import DjangoInterface ## Django Modules from django.shortcuts import get_object_or_404, render_to_response, redirect from django.template import Context, RequestContext ## Globals dji = DjangoInterface() def process_source(request, context, handle, act, add_to=None): # view, edit, save """ Process act on person. Can return a redirect. """ context["tview"] = _("Source") context["tviews"] = _("Sources") context["action"] = "view" view_template = "view_source_detail.html"
class DbDjango(DbGeneric): def restore(self): pass def write_version(self, directory): """Write files for a newly created DB.""" versionpath = os.path.join(directory, str(DBBACKEND)) LOG.debug("Write database backend file to 'djangodb'") with open(versionpath, "w") as version_file: version_file.write("djangodb") # Write default_settings, sqlite.db defaults = os.path.join(os.path.dirname(os.path.abspath(__file__)), "django_support", "defaults") LOG.debug("Copy defaults from: " + defaults) for filename in os.listdir(defaults): fullpath = os.path.abspath(os.path.join(defaults, filename)) if os.path.isfile(fullpath): shutil.copy2(fullpath, directory) # force load, to get all modules loaded because of reset issue self.load(directory) def initialize_backend(self, directory): pass def close_backend(self): pass def transaction_commit(self, txn): for (obj_type, trans_type) in txn.keys(): if trans_type in [TXNUPD, TXNADD]: for (handle, new_data) in txn[(obj_type, trans_type)]: if obj_type == PERSON_KEY: self.commit_person_detail(handle, new_data, trans_type, txn.batch) elif obj_type == FAMILY_KEY: self.commit_family_detail(handle, new_data, trans_type, txn.batch) elif obj_type == EVENT_KEY: self.commit_event_detail(handle, new_data, trans_type, txn.batch) elif obj_type == PLACE_KEY: self.commit_place_detail(handle, new_data, trans_type, txn.batch) elif obj_type == REPOSITORY_KEY: self.commit_repository_detail(handle, new_data, trans_type, txn.batch) elif obj_type == CITATION_KEY: self.commit_citation_detail(handle, new_data, trans_type, txn.batch) elif obj_type == SOURCE_KEY: self.commit_source_detail(handle, new_data, trans_type, txn.batch) elif obj_type == NOTE_KEY: self.commit_note_detail(handle, new_data, trans_type, txn.batch) elif obj_type == MEDIA_KEY: self.commit_media_object_detail(handle, new_data, trans_type, txn.batch) elif obj_type == TAG_KEY: self.commit_tag_detail(handle, new_data, trans_type, txn.batch) if txn.batch and self.has_changed: self.rebuild_secondary(None) def transaction_abort(self, txn): pass def get_metadata(self, setting, default=[]): metadata = self.dji.Metadata.filter(setting=setting) if metadata.count() > 0: return pickle.loads(metadata[0].value) elif default == []: return [] else: return default def set_metadata(self, setting, value): from gramps.webapp.grampsdb.models import Metadata metadata = self.dji.Metadata.filter(setting=setting) if metadata.count() > 0: metadata = metadata[0] metadata.value = pickle.dumps(value) else: metadata = Metadata(setting=setting, value=pickle.dumps(value)) metadata.save() def get_name_group_keys(self): rows = self.dji.NameGroup.all().order_by('name') return [row.name for row in rows] def get_name_group_mapping(self, key): rows = self.dji.NameGroup.filter(name=key) if rows: return row[0].name else: return key def get_person_handles(self, sort_handles=False): if sort_handles: return [item.handle for item in self.dji.Person.all().order_by("handle")] else: return [item.handle for item in self.dji.Person.all()] def get_family_handles(self): return [item.handle for item in self.dji.Family.all()] def get_event_handles(self): return [item.handle for item in self.dji.Event.all()] def get_citation_handles(self, sort_handles=False): if sort_handles: return [item.handle for item in self.dji.Citation.all().order_by("handle")] else: return [item.handle for item in self.dji.Citation.all()] def get_source_handles(self, sort_handles=False): if sort_handles: return [item.handle for item in self.dji.Source.all().order_by("handle")] else: return [item.handle for item in self.dji.Source.all()] def get_place_handles(self, sort_handles=False): if sort_handles: return [item.handle for item in self.dji.Place.all().order_by("handle")] else: return [item.handle for item in self.dji.Place.all()] def get_repository_handles(self): return [item.handle for item in self.dji.Repository.all()] def get_media_object_handles(self, sort_handles=False): if sort_handles: return [item.handle for item in self.dji.Media.all().order_by("handle")] else: return [item.handle for item in self.dji.Media.all()] def get_note_handles(self): return [item.handle for item in self.dji.Note.all()] def get_tag_handles(self, sort_handles=False): if sort_handles: return [item.handle for item in self.dji.Tag.all().order_by("handle")] else: return [item.handle for item in self.dji.Tag.all()] def get_tag_from_name(self, name): try: tag = self.dji.Tag.filter(name=name) return self._make_tag(tag[0]) except: return None def get_number_of_people(self): return self.dji.Person.count() def get_number_of_events(self): return self.dji.Event.count() def get_number_of_places(self): return self.dji.Place.count() def get_number_of_tags(self): return self.dji.Tag.count() def get_number_of_families(self): return self.dji.Family.count() def get_number_of_notes(self): return self.dji.Note.count() def get_number_of_citations(self): return self.dji.Citation.count() def get_number_of_sources(self): return self.dji.Source.count() def get_number_of_media_objects(self): return self.dji.Media.count() def get_number_of_repositories(self): return self.dji.Repository.count() def has_name_group_key(self, key): return len(self.dji.NameGroup.filter(name=key)) > 0 def set_name_group_mapping(self, name, grouping): from gramps.webapp.grampsdb.models import NameGroup if self.has_name_group_key(name): namegroup = self.dji.NameGroup.get(name=name) else: namegroup = NameGroup(name=name) namegroup.grouping = grouping namegroup.save() def commit_person(self, person, trans, change_time=None): raw = person.serialize() items = self.dji.Person.filter(handle=person.handle) count = items.count() old = None if count > 0: old = self._get_raw_person_data(person.handle) # delete and re-add items[0].delete() self.dji.add_person(raw) if count > 0: trans.add(PERSON_KEY, TXNUPD, person.handle, old, raw) else: trans.add(PERSON_KEY, TXNADD, person.handle, old, raw) # Contiued in transaction_commit... def commit_person_detail(self, handle, new_data, trans_type, batch): old_obj = self.get_person_from_handle(handle) self.dji.add_person_detail(new_data) obj = self.get_person_from_handle(handle) if trans_type == TXNUPD: if (old_obj.gender != obj.gender or old_obj.primary_name.first_name != obj.primary_name.first_name): self.genderStats.uncount_person(old_obj) self.genderStats.count_person(obj) elif trans_type == TXNADD: self.genderStats.count_person(person) person = obj # Other misc update tasks: self.individual_attributes.update( [str(attr.type) for attr in person.attribute_list if attr.type.is_custom() and str(attr.type)]) self.event_role_names.update([str(eref.role) for eref in person.event_ref_list if eref.role.is_custom()]) self.name_types.update([str(name.type) for name in ([person.primary_name] + person.alternate_names) if name.type.is_custom()]) all_surn = [] # new list we will use for storage all_surn += person.primary_name.get_surname_list() for asurname in person.alternate_names: all_surn += asurname.get_surname_list() self.origin_types.update([str(surn.origintype) for surn in all_surn if surn.origintype.is_custom()]) all_surn = None self.url_types.update([str(url.type) for url in person.urls if url.type.is_custom()]) attr_list = [] for mref in person.media_list: attr_list += [str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type)] self.media_attributes.update(attr_list) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("person-update", ([handle],)) elif trans_type == TXNADD: self.emit("person-add", ([handle],)) self.has_changed = True def commit_family(self, family, trans, change_time=None): raw = family.serialize() items = self.dji.Family.filter(handle=family.handle) count = items.count() old = None if count > 0: old = self._get_raw_family_data(family.handle) # delete and re-add items[0].delete() self.dji.add_family(family.serialize()) if count > 0: trans.add(FAMILY_KEY, TXNUPD, family.handle, old, raw) else: trans.add(FAMILY_KEY, TXNADD, family.handle, old, raw) # Contiued in transaction_commit... def commit_family_detail(self, handle, new_data, trans_type, batch): self.dji.add_family_detail(new_data) obj = self.get_family_from_handle(handle) family = obj # Misc updates: self.family_attributes.update( [str(attr.type) for attr in family.attribute_list if attr.type.is_custom() and str(attr.type)]) rel_list = [] for ref in family.child_ref_list: if ref.frel.is_custom(): rel_list.append(str(ref.frel)) if ref.mrel.is_custom(): rel_list.append(str(ref.mrel)) self.child_ref_types.update(rel_list) self.event_role_names.update( [str(eref.role) for eref in family.event_ref_list if eref.role.is_custom()]) if family.type.is_custom(): self.family_rel_types.add(str(family.type)) attr_list = [] for mref in family.media_list: attr_list += [str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type)] self.media_attributes.update(attr_list) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("family-update", ([handle],)) elif trans_type == TXNADD: self.emit("family-add", ([handle],)) self.has_changed = True def commit_citation(self, citation, trans, change_time=None): raw = citation.serialize() items = self.dji.Citation.filter(handle=citation.handle) count = items.count() old = None if count > 0: old = self._get_raw_citation_data(citation.handle) # delete and re-add items[0].delete() self.dji.add_citation(citation.serialize()) if count > 0: trans.add(CITATION_KEY, TXNUPD, citation.handle, old, raw) else: trans.add(CITATION_KEY, TXNADD, citation.handle, old, raw) # Contiued in transaction_commit... def commit_citation_detail(self, handle, new_data, trans_type, batch): self.dji.add_citation_detail(new_data) obj = self.get_citation_from_handle(handle) citation = obj # Misc updates: attr_list = [] for mref in citation.media_list: attr_list += [str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type)] self.media_attributes.update(attr_list) self.source_attributes.update( [str(attr.type) for attr in citation.attribute_list if attr.type.is_custom() and str(attr.type)]) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("citation-update", ([handle],)) elif trans_type == TXNADD: self.emit("citation-add", ([handle],)) self.has_changed = True def commit_source(self, source, trans, change_time=None): raw = source.serialize() items = self.dji.Source.filter(handle=source.handle) count = items.count() old = None if count > 0: old = self._get_raw_source_data(source.handle) # delete and re-add items[0].delete() self.dji.add_source(source.serialize()) if count > 0: trans.add(SOURCE_KEY, TXNUPD, source.handle, old, raw) else: trans.add(SOURCE_KEY, TXNADD, source.handle, old, raw) # Contiued in transaction_commit... def commit_source_detail(self, handle, new_data, trans_type, batch): self.dji.add_source_detail(new_data) obj = self.get_source_from_handle(handle) source = obj # Misc updates: self.source_media_types.update( [str(ref.media_type) for ref in source.reporef_list if ref.media_type.is_custom()]) attr_list = [] for mref in source.media_list: attr_list += [str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type)] self.media_attributes.update(attr_list) self.source_attributes.update( [str(attr.type) for attr in source.attribute_list if attr.type.is_custom() and str(attr.type)]) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("source-update", ([handle],)) elif trans_type == TXNADD: self.emit("source-add", ([handle],)) self.has_changed = True def commit_repository(self, repository, trans, change_time=None): raw = repository.serialize() items = self.dji.Repository.filter(handle=repository.handle) count = items.count() old = None if count > 0: old = self._get_raw_repository_data(repository.handle) # delete and re-add items[0].delete() self.dji.add_repository(repository.serialize()) if count > 0: trans.add(REPOSITORY_KEY, TXNUPD, repository.handle, old, raw) else: trans.add(REPOSITORY_KEY, TXNADD, repository.handle, old, raw) # Contiued in transaction_commit... def commit_repository_detail(self, handle, new_data, trans_type, batch): self.dji.add_repository_detail(new_data) obj = self.get_repository_from_handle(handle) repository = obj # Misc updates: if repository.type.is_custom(): self.repository_types.add(str(repository.type)) self.url_types.update([str(url.type) for url in repository.urls if url.type.is_custom()]) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("repository-update", ([handle],)) elif trans_type == TXNADD: self.emit("repository-add", ([handle],)) self.has_changed = True def commit_note(self, note, trans, change_time=None): raw = note.serialize() items = self.dji.Note.filter(handle=note.handle) count = items.count() old = None if count > 0: old = self._get_raw_note_data(note.handle) # delete and re-add items[0].delete() self.dji.add_note(note.serialize()) if count > 0: trans.add(NOTE_KEY, TXNUPD, note.handle, old, raw) else: trans.add(NOTE_KEY, TXNADD, note.handle, old, raw) # Contiued in transaction_commit... def commit_note_detail(self, handle, new_data, trans_type, batch): self.dji.add_note_detail(new_data) obj = self.get_note_from_handle(handle) note = obj # Misc updates: if note.type.is_custom(): self.note_types.add(str(note.type)) # Emit after added: if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("note-update", ([handle],)) elif trans_type == TXNADD: self.emit("note-add", ([handle],)) self.has_changed = True def commit_place(self, place, trans, change_time=None): raw = place.serialize() items = self.dji.Place.filter(handle=place.handle) count = items.count() old = None if count > 0: old = self._get_raw_place_data(place.handle) # delete and re-add items[0].delete() self.dji.add_place(place.serialize()) if count > 0: trans.add(PLACE_KEY, TXNUPD, place.handle, old, raw) else: trans.add(PLACE_KEY, TXNADD, place.handle, old, raw) # Contiued in transaction_commit... def commit_place_detail(self, handle, new_data, trans_type, batch): self.dji.add_place_detail(new_data) obj = self.get_place_from_handle(handle) place = obj # Misc updates: if place.get_type().is_custom(): self.place_types.add(str(place.get_type())) self.url_types.update([str(url.type) for url in place.urls if url.type.is_custom()]) attr_list = [] for mref in place.media_list: attr_list += [str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type)] self.media_attributes.update(attr_list) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("place-update", ([handle],)) elif trans_type == TXNADD: self.emit("place-add", ([handle],)) self.has_changed = True def commit_event(self, event, trans, change_time=None): raw = event.serialize() items = self.dji.Event.filter(handle=event.handle) count = items.count() old = None if count > 0: old = self._get_raw_event_data(event.handle) # delete and re-add items[0].delete() self.dji.add_event(event.serialize()) if count > 0: trans.add(EVENT_KEY, TXNUPD, event.handle, old, raw) else: trans.add(EVENT_KEY, TXNADD, event.handle, old, raw) # Contiued in transaction_commit... def commit_event_detail(self, handle, new_data, trans_type, batch): self.dji.add_event_detail(new_data) obj = self.get_event_from_handle(handle) event = obj # Misc updates: self.event_attributes.update( [str(attr.type) for attr in event.attribute_list if attr.type.is_custom() and str(attr.type)]) if event.type.is_custom(): self.event_names.add(str(event.type)) attr_list = [] for mref in event.media_list: attr_list += [str(attr.type) for attr in mref.attribute_list if attr.type.is_custom() and str(attr.type)] self.media_attributes.update(attr_list) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("event-update", ([handle],)) elif trans_type == TXNADD: self.emit("event-add", ([handle],)) self.has_changed = True def commit_tag(self, tag, trans, change_time=None): raw = tag.serialize() items = self.dji.Tag.filter(handle=tag.handle) count = items.count() old = None if count > 0: old = self._get_raw_tag_data(tag.handle) # delete and re-add items[0].delete() self.dji.add_tag(tag.serialize()) if count > 0: trans.add(TAG_KEY, TXNUPD, tag.handle, old, raw) else: trans.add(TAG_KEY, TXNADD, tag.handle, old, raw) # Contiued in transaction_commit... def commit_tag_detail(self, handle, new_data, trans_type, batch): self.dji.add_tag_detail(new_data) obj = self.get_tag_from_handle(handle) tag = obj if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("tag-update", ([handle],)) elif trans_type == TXNADD: self.emit("tag-add", ([handle],)) self.has_changed = True def commit_media_object(self, media, trans, change_time=None): """ Commit the specified MediaObject to the database, storing the changes as part of the transaction. """ raw = media.serialize() items = self.dji.Media.filter(handle=media.handle) count = items.count() old = None if count > 0: old = self._get_raw_media_data(media.handle) # delete and re-add items[0].delete() self.dji.add_media(media.serialize()) if count > 0: trans.add(MEDIA_KEY, TXNUPD, media.handle, old, raw) else: trans.add(MEDIA_KEY, TXNADD, media.handle, old, raw) # Contiued in transaction_commit... def commit_media_object_detail(self, handle, new_data, trans_type, batch): self.dji.add_media_detail(new_data) obj = self.get_object_from_handle(handle) media = obj # Misc updates: self.media_attributes.update( [str(attr.type) for attr in media.attribute_list if attr.type.is_custom() and str(attr.type)]) if not batch: self.update_backlinks(obj) if trans_type == TXNUPD: self.emit("media-update", ([handle],)) elif trans_type == TXNADD: self.emit("media-add", ([handle],)) self.has_changed = True def find_backlink_handles(self, handle, include_classes=None): """ Find all objects that hold a reference to the object handle. Returns an interator over a list of (class_name, handle) tuples. :param handle: handle of the object to search for. :type handle: database handle :param include_classes: list of class names to include in the results. Default: None means include all classes. :type include_classes: list of class names Note that this is a generator function, it returns a iterator for use in loops. If you want a list of the results use:: result_list = list(find_backlink_handles(handle)) """ rows = self.dji.Reference.filter(ref_handle=handle) for row in rows: if (include_classes is None) or (row.obj_class in include_classes): yield (row.obj_class, row.obj_handle) def update_backlinks(self, obj): from gramps.webapp.grampsdb.models import Reference # First, delete the current references: self.dji.Reference.filter(obj_handle=obj.handle).delete() # Now, add the current ones: references = set(obj.get_referenced_handles_recursively()) for (ref_class_name, ref_handle) in references: reference = Reference(obj_handle=obj.handle, obj_class=obj.__class__.__name__, ref_handle=ref_handle, ref_class=ref_class_name) reference.save() # Removals: def remove_person(self, handle, transaction): self.dji.Person.filter(handle=handle)[0].delete() self.emit("person-delete", ([handle],)) def _do_remove(self, handle, transaction, data_map, data_id_map, key): key2table = { PERSON_KEY: "person", FAMILY_KEY: "family", SOURCE_KEY: "source", CITATION_KEY: "citation", EVENT_KEY: "event", MEDIA_KEY: "media", PLACE_KEY: "place", REPOSITORY_KEY: "repository", NOTE_KEY: "note", TAG_KEY: "tag", } table = getattr(self.dji, key2table[key].title()) table.filter(handle=handle)[0].delete() self.emit("%s-delete" % key2table[key], ([handle],)) def find_initial_person(self): handle = self.get_default_handle() person = None if handle: person = self.get_person_from_handle(handle) if person: return person if len(self.dji.Person.all()) > 0: person = self.dji.Person.all()[0] return self.get_person_from_handle(person.handle) def iter_person_handles(self): return (person.handle for person in self.dji.Person.all()) def iter_family_handles(self): return (family.handle for family in self.dji.Family.all()) def iter_citation_handles(self): return (citation.handle for citation in self.dji.Citation.all()) def iter_event_handles(self): return (event.handle for event in self.dji.Event.all()) def iter_media_object_handles(self): return (media.handle for media in self.dji.Media.all()) def iter_note_handles(self): return (note.handle for note in self.dji.Note.all()) def iter_place_handles(self): return (place.handle for place in self.dji.Place.all()) def iter_repository_handles(self): return (repository.handle for repository in self.dji.Repository.all()) def iter_source_handles(self): return (source.handle for source in self.dji.Source.all()) def iter_tag_handles(self): return (tag.handle for tag in self.dji.Tag.all()) def reindex_reference_map(self, callback): from gramps.webapp.grampsdb.models import Reference callback(4) self.dji.Reference.all().delete() primary_table = ( (self.get_person_cursor, Person), (self.get_family_cursor, Family), (self.get_event_cursor, Event), (self.get_place_cursor, Place), (self.get_source_cursor, Source), (self.get_citation_cursor, Citation), (self.get_media_cursor, MediaObject), (self.get_repository_cursor, Repository), (self.get_note_cursor, Note), (self.get_tag_cursor, Tag), ) # Now we use the functions and classes defined above # to loop through each of the primary object tables. for cursor_func, class_func in primary_table: logging.info("Rebuilding %s reference map" % class_func.__name__) with cursor_func() as cursor: for found_handle, val in cursor: obj = class_func.create(val) references = set(obj.get_referenced_handles_recursively()) # handle addition of new references for (ref_class_name, ref_handle) in references: reference = Reference(obj_handle=obj.handle, obj_class=obj.__class__.__name__, ref_handle=ref_handle, ref_class=ref_class_name) reference.save() callback(5) def rebuild_secondary(self, update): gstats = self.rebuild_gender_stats() self.genderStats = GenderStats(gstats) def has_handle_for_person(self, key): return self.dji.Person.filter(handle=key).count() > 0 def has_handle_for_family(self, key): return self.dji.Family.filter(handle=key).count() > 0 def has_handle_for_source(self, key): return self.dji.Source.filter(handle=key).count() > 0 def has_handle_for_citation(self, key): return self.dji.Citation.filter(handle=key).count() > 0 def has_handle_for_event(self, key): return self.dji.Event.filter(handle=key).count() > 0 def has_handle_for_media(self, key): return self.dji.Media.filter(handle=key).count() > 0 def has_handle_for_place(self, key): return self.dji.Place.filter(handle=key).count() > 0 def has_handle_for_repository(self, key): return self.dji.Repository.filter(handle=key).count() > 0 def has_handle_for_note(self, key): return self.dji.Note.filter(handle=key).count() > 0 def has_handle_for_tag(self, key): return self.dji.Tag.filter(handle=key).count() > 0 def has_gramps_id_for_person(self, key): return self.dji.Person.filter(gramps_id=key).count() > 0 def has_gramps_id_for_family(self, key): return self.dji.Family.filter(gramps_id=key).count() > 0 def has_gramps_id_for_source(self, key): return self.dji.Source.filter(gramps_id=key).count() > 0 def has_gramps_id_for_citation(self, key): return self.dji.Citation.filter(gramps_id=key).count() > 0 def has_gramps_id_for_event(self, key): return self.dji.Event.filter(gramps_id=key).count() > 0 def has_gramps_id_for_media(self, key): return self.dji.Media.filter(gramps_id=key).count() > 0 def has_gramps_id_for_place(self, key): return self.dji.Place.filter(gramps_id=key).count() > 0 def has_gramps_id_for_repository(self, key): return self.dji.Repository.filter(gramps_id=key).count() > 0 def has_gramps_id_for_note(self, key): return self.dji.Note.filter(gramps_id=key).count() > 0 def get_person_gramps_ids(self): return [x.gramps_id for x in self.dji.Person.all()] def get_family_gramps_ids(self): return [x.gramps_id for x in self.dji.Family.all()] def get_source_gramps_ids(self): return [x.gramps_id for x in self.dji.Source.all()] def get_citation_gramps_ids(self): return [x.gramps_id for x in self.dji.Citation.all()] def get_event_gramps_ids(self): return [x.gramps_id for x in self.dji.Event.all()] def get_media_gramps_ids(self): return [x.gramps_id for x in self.dji.Media.all()] def get_place_gramps_ids(self): return [x.gramps_id for x in self.dji.Place.all()] def get_repository_gramps_ids(self): return [x.gramps_id for x in self.dji.Repository.all()] def get_note_gramps_ids(self): return [x.gramps_id for x in self.dji.Note.all()] def _get_raw_person_data(self, key): return self.dji.get_person(self.dji.Person.get(handle=key)) def _get_raw_person_from_id_data(self, key): return self.dji.get_person(self.dji.Person.get(gramps_id=key)) def _get_raw_family_data(self, key): return self.dji.get_family(self.dji.Family.get(handle=key)) def _get_raw_family_from_id_data(self, key): return self.dji.get_family(self.dji.Family.get(gramps_id=key)) def _get_raw_source_data(self, key): return self.dji.get_source(self.dji.Source.get(handle=key)) def _get_raw_source_from_id_data(self, key): return self.dji.get_source(self.dji.Source.get(gramps_id=key)) def _get_raw_citation_data(self, key): return self.dji.get_citation(self.dji.Citation.get(handle=key)) def _get_raw_citation_from_id_data(self, key): return self.dji.get_citation(self.dji.Citation.get(gramps_id=key)) def _get_raw_event_data(self, key): return self.dji.get_event(self.dji.Event.get(handle=key)) def _get_raw_event_from_id_data(self, key): return self.dji.get_event(self.dji.Event.get(gramps_id=key)) def _get_raw_media_data(self, key): return self.dji.get_media(self.dji.Media.get(handle=key)) def _get_raw_media_from_id_data(self, key): return self.dji.get_media(self.dji.Media.get(gramps_id=key)) def _get_raw_place_data(self, key): return self.dji.get_place(self.dji.Place.get(handle=key)) def _get_raw_place_from_id_data(self, key): return self.dji.get_place(self.dji.Place.get(gramps_id=key)) def _get_raw_repository_data(self, key): return self.dji.get_repository(self.dji.Repository.get(handle=key)) def _get_raw_repository_from_id_data(self, key): return self.dji.get_repository(self.dji.Repository.get(gramps_id=key)) def _get_raw_note_data(self, key): return self.dji.get_note(self.dji.Note.get(handle=key)) def _get_raw_note_from_id_data(self, key): return self.dji.get_note(self.dji.Note.get(gramps_id=key)) def _get_raw_tag_data(self, key): return self.dji.get_tag(self.dji.Tag.get(handle=key)) def rebuild_gender_stats(self): """ Returns a dictionary of {given_name: (male_count, female_count, unknown_count)} Not called: this is a database-efficient version """ UNKNOWN = 2 MALE = 1 FEMALE = 0 self.dji.GenderStats.all().delete() gstats = {} for person in self.dji.Person.all(): for first_name in person.name_set.all(): for name in first_name.first_name.split(): if name not in gstats: gstats[name] = [0, 0, 0] if person.gender_type.val == MALE: gstats[name][0] += 1 elif person.gender_type.val == FEMALE: gstats[name][1] += 1 else: gstats[name][2] += 1 for key in gstats: gstats[key] = tuple(gstats[key]) return gstats def save_gender_stats(self, genderStats): """ {name: (male_count, female_count, unknown_count), ...} """ from gramps.webapp.grampsdb.models import GenderStats self.dji.GenderStats.all().delete() gstats = genderStats.stats for key in gstats: data = gstats[key] stat = GenderStats(name=key, male=data[0], female=data[1], unknown=data[2]) stat.save() def get_gender_stats(self): """ Returns a dictionary of {given_name: (male_count, female_count, unknown_count)} """ rows = self.dji.GenderStats.values('name', 'male', 'female', 'unknown') gstats = {} for dict in rows: gstats[dict['name']] = (dict['male'], dict['female'], dict['unknown']) return gstats def get_surname_list(self): return [x['surname'] for x in self.dji.Surname.values('surname').order_by('surname').distinct()] def save_surname_list(self): # Nothing to do pass def build_surname_list(self): # Nothing to do pass def drop_tables(self): # Nothing to do pass def load(self, directory, callback=None, mode=None, force_schema_upgrade=False, force_bsddb_upgrade=False, force_bsddb_downgrade=False, force_python_upgrade=False): # Django-specific loads: from django.conf import settings LOG.info("Django loading...") default_settings = {"__file__": os.path.join(directory, "default_settings.py")} settings_file = os.path.join(directory, "default_settings.py") with open(settings_file) as f: code = compile(f.read(), settings_file, 'exec') exec(code, globals(), default_settings) class Module(object): def __init__(self, dictionary): self.dictionary = dictionary def __getattr__(self, item): return self.dictionary[item] LOG.info("Django loading defaults from: " + directory) try: settings.configure(Module(default_settings)) except RuntimeError: LOG.info("Django already configured error! Shouldn't happen!") # already configured; ignore import django django.setup() from gramps.webapp.libdjango import DjangoInterface self.dji = DjangoInterface() super().load(directory, callback, mode, force_schema_upgrade, force_bsddb_upgrade, force_bsddb_downgrade, force_python_upgrade) def _make_repository(self, repository): if self.use_db_cache and repository.cache: data = repository.from_cache() else: data = self.dji.get_repository(repository) return Repository.create(data) def _make_citation(self, citation): if self.use_db_cache and citation.cache: data = citation.from_cache() else: data = self.dji.get_citation(citation) return Citation.create(data) def _make_source(self, source): if self.use_db_cache and source.cache: data = source.from_cache() else: data = self.dji.get_source(source) return Source.create(data) def _make_family(self, family): if self.use_db_cache and family.cache: data = family.from_cache() else: data = self.dji.get_family(family) return Family.create(data) def _make_person(self, person): if self.use_db_cache and person.cache: data = person.from_cache() else: data = self.dji.get_person(person) return Person.create(data) def _make_event(self, event): if self.use_db_cache and event.cache: data = event.from_cache() else: data = self.dji.get_event(event) return Event.create(data) def _make_note(self, note): if self.use_db_cache and note.cache: data = note.from_cache() else: data = self.dji.get_note(note) return Note.create(data) def _make_tag(self, tag): data = self.dji.get_tag(tag) return Tag.create(data) def _make_place(self, place): if self.use_db_cache and place.cache: data = place.from_cache() else: data = self.dji.get_place(place) return Place.create(data) def _make_media(self, media): if self.use_db_cache and media.cache: data = media.from_cache() else: data = self.dji.get_media(media) return MediaObject.create(data) def request_rebuild(self): # override # caches are ok, but let's compute public's self.dji.update_publics() super().request_rebuild()