3502: Extend CSV import to include additional fields; added source title to export

svn: r15671
This commit is contained in:
Doug Blank 2010-07-31 15:18:41 +00:00
parent e4bb905303
commit 59dca4e3ea
2 changed files with 178 additions and 18 deletions

View File

@ -50,6 +50,7 @@ LOG = logging.getLogger(".ExportCSV")
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
import gen.lib import gen.lib
from Filters import GenericFilter, Rules, build_filter_model from Filters import GenericFilter, Rules, build_filter_model
from gen.lib.eventroletype import EventRoleType
from ExportOptions import WriterOptionBox from ExportOptions import WriterOptionBox
import Utils import Utils
import gen.proxy import gen.proxy
@ -80,6 +81,25 @@ def sortable_string_representation(text):
alpha += s alpha += s
return alpha + (("0" * 10) + numeric)[-10:] return alpha + (("0" * 10) + numeric)[-10:]
def get_primary_event_ref_from_type(db, person, event_name):
"""
>>> get_primary_event_ref_from_type(db, Person(), "Baptism"):
"""
for ref in person.event_ref_list:
if ref.get_role() == EventRoleType.PRIMARY:
event = db.get_event_from_handle(ref.ref)
if event and event.type.is_type(event_name):
return ref
return None
def get_primary_source_title(db, obj):
import pdb; pdb.set_trace()
for ref in obj.get_source_references():
source = db.get_source_from_handle(ref.ref)
if source:
return source.get_title()
return ""
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Encoding support for CSV, from http://docs.python.org/lib/csv-examples.html # Encoding support for CSV, from http://docs.python.org/lib/csv-examples.html
@ -310,19 +330,25 @@ class CSVWriter(object):
########################### ###########################
if self.include_individuals: if self.include_individuals:
if self.translate_headers: if self.translate_headers:
self.write_csv(_("Person"), _("Surname"), _("Given"), self.write_csv(
_("Call"), _("Suffix"), _("Prefix"), _("Person"), _("Surname"), _("Given"),
_("Person|Title"), _("Gender"), _("Birth date"), _("Call"), _("Suffix"), _("Prefix"),
_("Birth place"), _("Birth source"), _("Person|Title"), _("Gender"),
_("Death date"), _("Death place"), _("Birth date"), _("Birth place"), _("Birth source"),
_("Death source"), _("Note")) _("Baptism date"), _("Baptism place"), _("Baptism source"),
_("Death date"), _("Death place"), _("Death source"),
_("Burial date"), _("Burial place"), _("Burial source"),
_("Note"))
else: else:
self.write_csv("Person", "Surname", "Given", self.write_csv(
"Call", "Suffix", "Prefix", "Person", "Surname", "Given",
"Title", "Gender", "Birth date", "Call", "Suffix", "Prefix",
"Birth place", "Birth source", "Title", "Gender",
"Death date", "Death place", "Birth date", "Birth place", "Birth source",
"Death source", "Note") "Baptism date", "Baptism place", "Baptism source",
"Death date", "Death place", "Death source",
"Burial date", "Burial place", "Burial source",
"Note")
for key in plist: for key in plist:
person = self.db.get_person_from_handle(key) person = self.db.get_person_from_handle(key)
if person: if person:
@ -336,7 +362,7 @@ class CSVWriter(object):
grampsid_ref = "" grampsid_ref = ""
if grampsid != "": if grampsid != "":
grampsid_ref = "[" + grampsid + "]" grampsid_ref = "[" + grampsid + "]"
note = '' # don't export notes or source note = '' # don't export notes
callname = primary_name.get_call_name() callname = primary_name.get_call_name()
gender = person.get_gender() gender = person.get_gender()
if gender == gen.lib.Person.MALE: if gender == gen.lib.Person.MALE:
@ -348,6 +374,7 @@ class CSVWriter(object):
# Birth: # Birth:
birthdate = "" birthdate = ""
birthplace = "" birthplace = ""
birthsource = ""
birth_ref = person.get_birth_ref() birth_ref = person.get_birth_ref()
if birth_ref: if birth_ref:
birth = self.db.get_event_from_handle(birth_ref.ref) birth = self.db.get_event_from_handle(birth_ref.ref)
@ -357,9 +384,26 @@ class CSVWriter(object):
if place_handle: if place_handle:
place = self.db.get_place_from_handle(place_handle) place = self.db.get_place_from_handle(place_handle)
birthplace = place.get_title() birthplace = place.get_title()
birthsource = get_primary_source_title(self.db, birth)
# Baptism:
baptismdate = ""
baptismplace = ""
baptismsource = ""
baptism_ref = get_primary_event_ref_from_type(
self.db, person, "Baptism")
if baptism_ref:
baptism = self.db.get_event_from_handle(baptism_ref.ref)
if baptism:
baptismdate = self.format_date( baptism)
place_handle = baptism.get_place_handle()
if place_handle:
place = self.db.get_place_from_handle(place_handle)
baptismplace = place.get_title()
baptismsource = get_primary_source_title(self.db, baptism)
# Death: # Death:
deathdate = "" deathdate = ""
deathplace = "" deathplace = ""
deathsource = ""
death_ref = person.get_death_ref() death_ref = person.get_death_ref()
if death_ref: if death_ref:
death = self.db.get_event_from_handle(death_ref.ref) death = self.db.get_event_from_handle(death_ref.ref)
@ -369,10 +413,29 @@ class CSVWriter(object):
if place_handle: if place_handle:
place = self.db.get_place_from_handle(place_handle) place = self.db.get_place_from_handle(place_handle)
deathplace = place.get_title() deathplace = place.get_title()
deathsource = get_primary_source_title(self.db, death)
# Burial:
burialdate = ""
burialplace = ""
burialsource = ""
burial_ref = get_primary_event_ref_from_type(
self.db, person, "Burial")
if burial_ref:
burial = self.db.get_event_from_handle(burial_ref.ref)
if burial:
burialdate = self.format_date( burial)
place_handle = burial.get_place_handle()
if place_handle:
place = self.db.get_place_from_handle(place_handle)
burialplace = place.get_title()
burialsource = get_primary_source_title(self.db, burial)
# Write it out:
self.write_csv(grampsid_ref, surname, first_name, callname, self.write_csv(grampsid_ref, surname, first_name, callname,
suffix, prefix, title, gender, suffix, prefix, title, gender,
birthdate, birthplace, "", birthdate, birthplace, birthsource,
deathdate, deathplace, "", baptismdate, baptismplace, baptismsource,
deathdate, deathplace, deathsource,
burialdate, burialplace, burialsource,
note) note)
self.update() self.update()
self.writeln() self.writeln()
@ -426,8 +489,8 @@ class CSVWriter(object):
if place_handle: if place_handle:
place = self.db.get_place_from_handle(place_handle) place = self.db.get_place_from_handle(place_handle)
mplace = place.get_title() mplace = place.get_title()
# m_source = self.get_primary_source( event.get_source_references()) source = get_primary_source_title(self.db, event)
source, note = '', '' note = ''
self.write_csv(marriage_id, father_id, mother_id, mdate, self.write_csv(marriage_id, father_id, mother_id, mdate,
mplace, source, note) mplace, source, note)
self.update() self.update()

View File

@ -55,6 +55,23 @@ from DateHandler import parser as _dp
from Utils import gender as gender_map from Utils import gender as gender_map
from Utils import create_id from Utils import create_id
from gui.utils import ProgressMeter from gui.utils import ProgressMeter
from gen.lib.eventroletype import EventRoleType
#-------------------------------------------------------------------------
#
# Support Functions
#
#-------------------------------------------------------------------------
def get_primary_event_ref_from_type(db, person, event_name):
"""
>>> get_primary_event_ref_from_type(db, Person(), "Baptism"):
"""
for ref in person.event_ref_list:
if ref.get_role() == EventRoleType.PRIMARY:
event = db.get_event_from_handle(ref.ref)
if event and event.type.is_type(event_name):
return ref
return None
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -184,6 +201,24 @@ def cleanup_column_name(column):
elif retval in ["Birthsource", elif retval in ["Birthsource",
"Birth source", _("Birth source")]: "Birth source", _("Birth source")]:
return "birthsource" return "birthsource"
elif retval in ["Baptismplace",
"Baptism place", _("Baptism place")]:
return "baptismplace"
elif retval in ["Baptismdate",
"Baptism date", _("Baptism date")]:
return "baptismdate"
elif retval in ["Baptismsource",
"Baptism source", _("Baptism source")]:
return "baptismsource"
elif retval in ["Burialplace",
"Burial place", _("Burial place")]:
return "burialplace"
elif retval in ["Burialdate",
"Burial date", _("Burial date")]:
return "burialdate"
elif retval in ["Burialsource",
"Burial source", _("Burial source")]:
return "burialsource"
elif retval in ["Deathplace", elif retval in ["Deathplace",
"Death place", _("Death place")]: "Death place", _("Death place")]:
return "deathplace" return "deathplace"
@ -256,6 +291,24 @@ def cleanup_column_name(column):
elif retval in ["birthsource", "birth_source", elif retval in ["birthsource", "birth_source",
"birth source", _("birth source")]: "birth source", _("birth source")]:
return "birthsource" return "birthsource"
elif retval in ["baptismplace",
"baptism place", _("baptism place")]:
return "baptismplace"
elif retval in ["baptismdate",
"baptism date", _("baptism date")]:
return "baptismdate"
elif retval in ["baptismsource",
"baptism source", _("baptism source")]:
return "baptismsource"
elif retval in ["burialplace",
"burial place", _("burial place")]:
return "burialplace"
elif retval in ["burialdate",
"burial date", _("burial date")]:
return "burialdate"
elif retval in ["burialsource",
"burial source", _("burial source")]:
return "burialsource"
elif retval in ["deathplace", "death_place", elif retval in ["deathplace", "death_place",
"death place", _("death place")]: "death place", _("death place")]:
return "deathplace" return "deathplace"
@ -574,6 +627,12 @@ class CSVParser(object):
birthplace = rd(line_number, row, col, "birthplace") birthplace = rd(line_number, row, col, "birthplace")
birthdate = rd(line_number, row, col, "birthdate") birthdate = rd(line_number, row, col, "birthdate")
birthsource = rd(line_number, row, col, "birthsource") birthsource = rd(line_number, row, col, "birthsource")
baptismplace = rd(line_number, row, col, "baptismplace")
baptismdate = rd(line_number, row, col, "baptismdate")
baptismsource = rd(line_number, row, col, "baptismsource")
burialplace = rd(line_number, row, col, "burialplace")
burialdate = rd(line_number, row, col, "burialdate")
burialsource = rd(line_number, row, col, "burialsource")
deathplace = rd(line_number, row, col, "deathplace") deathplace = rd(line_number, row, col, "deathplace")
deathdate = rd(line_number, row, col, "deathdate") deathdate = rd(line_number, row, col, "deathdate")
deathsource = rd(line_number, row, col, "deathsource") deathsource = rd(line_number, row, col, "deathsource")
@ -648,6 +707,7 @@ class CSVParser(object):
person.set_gender(gender) person.set_gender(gender)
######################################################### #########################################################
# add if new, replace if different # add if new, replace if different
# Birth:
if birthdate is not None: if birthdate is not None:
birthdate = _dp.parse(birthdate) birthdate = _dp.parse(birthdate)
if birthplace is not None: if birthplace is not None:
@ -655,13 +715,33 @@ class CSVParser(object):
if birthsource is not None: if birthsource is not None:
new, birthsource = self.get_or_create_source(birthsource) new, birthsource = self.get_or_create_source(birthsource)
if birthdate or birthplace or birthsource: if birthdate or birthplace or birthsource:
new, birth = self.get_or_create_event(person, gen.lib.EventType.BIRTH, birthdate, birthplace, birthsource) new, birth = self.get_or_create_event(person,
gen.lib.EventType.BIRTH, birthdate,
birthplace, birthsource)
birth_ref = person.get_birth_ref() birth_ref = person.get_birth_ref()
if birth_ref is None: if birth_ref is None:
# new # new
birth_ref = gen.lib.EventRef() birth_ref = gen.lib.EventRef()
birth_ref.set_reference_handle( birth.get_handle()) birth_ref.set_reference_handle( birth.get_handle())
person.set_birth_ref( birth_ref) person.set_birth_ref( birth_ref)
# Baptism:
if baptismdate is not None:
baptismdate = _dp.parse(baptismdate)
if baptismplace is not None:
new, baptismplace = self.get_or_create_place(baptismplace)
if baptismsource is not None:
new, baptismsource = self.get_or_create_source(baptismsource)
if baptismdate or baptismplace or baptismsource:
new, baptism = self.get_or_create_event(person,
gen.lib.EventType.BAPTISM, baptismdate,
baptismplace, baptismsource)
baptism_ref = get_primary_event_ref_from_type(self.db, person, "Baptism")
if baptism_ref is None:
# new
baptism_ref = gen.lib.EventRef()
baptism_ref.set_reference_handle( baptism.get_handle())
person.add_event_ref( baptism_ref)
# Death:
if deathdate is not None: if deathdate is not None:
deathdate = _dp.parse(deathdate) deathdate = _dp.parse(deathdate)
if deathplace is not None: if deathplace is not None:
@ -679,6 +759,23 @@ class CSVParser(object):
death_ref = gen.lib.EventRef() death_ref = gen.lib.EventRef()
death_ref.set_reference_handle(death.get_handle()) death_ref.set_reference_handle(death.get_handle())
person.set_death_ref(death_ref) person.set_death_ref(death_ref)
# Burial:
if burialdate is not None:
burialdate = _dp.parse(burialdate)
if burialplace is not None:
new, burialplace = self.get_or_create_place(burialplace)
if burialsource is not None:
new, burialsource = self.get_or_create_source(burialsource)
if burialdate or burialplace or burialsource:
new, burial = self.get_or_create_event(person,
gen.lib.EventType.BURIAL, burialdate,
burialplace, burialsource)
burial_ref = get_primary_event_ref_from_type(self.db, person, "Burial")
if burial_ref is None:
# new
burial_ref = gen.lib.EventRef()
burial_ref.set_reference_handle( burial.get_handle())
person.add_event_ref( burial_ref)
if source: if source:
# add, if new # add, if new
new, source = self.get_or_create_source(source) new, source = self.get_or_create_source(source)