9744: Tidy up get_*_from_handle methods

This commit is contained in:
Nick Hall 2017-03-03 22:00:16 +00:00
parent 0813ba25db
commit 349bdf85b7

View File

@ -1286,135 +1286,48 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback):
# #
################################################################ ################################################################
def get_event_from_handle(self, handle): def _get_from_handle(self, obj_key, obj_class, handle):
if isinstance(handle, bytes): if isinstance(handle, bytes):
handle = str(handle, "utf-8") handle = str(handle, "utf-8")
if handle is None: if handle is None:
raise HandleError('Handle is None') raise HandleError('Handle is None')
if not handle: if not handle:
raise HandleError('Handle is empty') raise HandleError('Handle is empty')
data = self.get_raw_event_data(handle) data = self.get_raw_data(obj_key, handle)
if data: if data:
return Event.create(data) return obj_class.create(data)
else: else:
raise HandleError('Handle %s not found' % handle) raise HandleError('Handle %s not found' % handle)
def get_event_from_handle(self, handle):
return self._get_from_handle(EVENT_KEY, Event, handle)
def get_family_from_handle(self, handle): def get_family_from_handle(self, handle):
if isinstance(handle, bytes): return self._get_from_handle(FAMILY_KEY, Family, handle)
handle = str(handle, "utf-8")
if handle is None:
raise HandleError('Handle is None')
if not handle:
raise HandleError('Handle is empty')
data = self.get_raw_family_data(handle)
if data:
return Family.create(data)
else:
raise HandleError('Handle %s not found' % handle)
def get_repository_from_handle(self, handle): def get_repository_from_handle(self, handle):
if isinstance(handle, bytes): return self._get_from_handle(REPOSITORY_KEY, Repository, handle)
handle = str(handle, "utf-8")
if handle is None:
raise HandleError('Handle is None')
if not handle:
raise HandleError('Handle is empty')
data = self.get_raw_repository_data(handle)
if data:
return Repository.create(data)
else:
raise HandleError('Handle %s not found' % handle)
def get_person_from_handle(self, handle): def get_person_from_handle(self, handle):
if isinstance(handle, bytes): return self._get_from_handle(PERSON_KEY, Person, handle)
handle = str(handle, "utf-8")
if handle is None:
raise HandleError('Handle is None')
if not handle:
raise HandleError('Handle is empty')
data = self.get_raw_person_data(handle)
if data:
return Person.create(data)
else:
raise HandleError('Handle %s not found' % handle)
def get_place_from_handle(self, handle): def get_place_from_handle(self, handle):
if isinstance(handle, bytes): return self._get_from_handle(PLACE_KEY, Place, handle)
handle = str(handle, "utf-8")
if handle is None:
raise HandleError('Handle is None')
if not handle:
raise HandleError('Handle is empty')
data = self.get_raw_place_data(handle)
if data:
return Place.create(data)
else:
raise HandleError('Handle %s not found' % handle)
def get_citation_from_handle(self, handle): def get_citation_from_handle(self, handle):
if isinstance(handle, bytes): return self._get_from_handle(CITATION_KEY, Citation, handle)
handle = str(handle, "utf-8")
if handle is None:
raise HandleError('Handle is None')
if not handle:
raise HandleError('Handle is empty')
data = self.get_raw_citation_data(handle)
if data:
return Citation.create(data)
else:
raise HandleError('Handle %s not found' % handle)
def get_source_from_handle(self, handle): def get_source_from_handle(self, handle):
if isinstance(handle, bytes): return self._get_from_handle(SOURCE_KEY, Source, handle)
handle = str(handle, "utf-8")
if handle is None:
raise HandleError('Handle is None')
if not handle:
raise HandleError('Handle is empty')
data = self.get_raw_source_data(handle)
if data:
return Source.create(data)
else:
raise HandleError('Handle %s not found' % handle)
def get_note_from_handle(self, handle): def get_note_from_handle(self, handle):
if isinstance(handle, bytes): return self._get_from_handle(NOTE_KEY, Note, handle)
handle = str(handle, "utf-8")
if handle is None:
raise HandleError('Handle is None')
if not handle:
raise HandleError('Handle is empty')
data = self.get_raw_note_data(handle)
if data:
return Note.create(data)
else:
raise HandleError('Handle %s not found' % handle)
def get_media_from_handle(self, handle): def get_media_from_handle(self, handle):
if isinstance(handle, bytes): return self._get_from_handle(MEDIA_KEY, Media, handle)
handle = str(handle, "utf-8")
if handle is None:
raise HandleError('Handle is None')
if not handle:
raise HandleError('Handle is empty')
data = self.get_raw_media_data(handle)
if data:
return Media.create(data)
else:
raise HandleError('Handle %s not found' % handle)
def get_tag_from_handle(self, handle): def get_tag_from_handle(self, handle):
if isinstance(handle, bytes): return self._get_from_handle(TAG_KEY, Tag, handle)
handle = str(handle, "utf-8")
if handle is None:
raise HandleError('Handle is None')
if not handle:
raise HandleError('Handle is empty')
data = self.get_raw_tag_data(handle)
if data:
return Tag.create(data)
else:
raise HandleError('Handle %s not found' % handle)
################################################################ ################################################################
# #