added cache to objects; added birth/death_ref_index

svn: r18287
This commit is contained in:
Doug Blank 2011-10-11 23:42:33 +00:00
parent bdc643366d
commit 17f1796f1c
3 changed files with 55 additions and 21 deletions

View File

@ -26,6 +26,7 @@
# Gramps Modules # Gramps Modules
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import cPickle
import web import web
import gen import gen
from gen.db import DbReadBase, DbWriteBase from gen.db import DbReadBase, DbWriteBase
@ -161,27 +162,45 @@ class DbDjango(DbWriteBase, DbReadBase):
obj.unserialize(self.dji.get_event(self.dji.Event.get(handle=handle))) obj.unserialize(self.dji.get_event(self.dji.Event.get(handle=handle)))
return obj return obj
def get_family_from_handle(self, handle): def get_family_from_handle(self, handle):
obj = gen.lib.Family() #print "get_family_from_handle", handle
obj.unserialize(self.dji.get_family(self.dji.Family.get(handle=handle))) family = self.dji.Family.get(handle=handle)
obj = self.make_family(family)
return obj return obj
def get_family_from_gramps_id(self, gramps_id): def get_family_from_gramps_id(self, gramps_id):
obj = gen.lib.Family() #print "get_family_from_id", gramps_id
try: try:
family = self.dji.Family.get(gramps_id=gramps_id) family = self.dji.Family.get(gramps_id=gramps_id)
except: except:
return None return None
obj.unserialize(self.dji.get_family(family)) obj = self.make_family(family)
return obj return obj
def get_person_from_handle(self, handle): def get_person_from_handle(self, handle):
#print "get_person_from_handle", handle
try: try:
person = self.dji.Person.select_related().get(handle=handle) person = self.dji.Person.select_related().get(handle=handle)
except: except:
return None return None
data = self.dji.get_person(person) return self.make_person(person)
obj = gen.lib.Person().unserialize(data)
def make_family(self, family):
if family.cache:
data = cPickle.loads(str(family.cache))
obj = gen.lib.Family.create(data)
else:
data = self.dji.get_family(family)
obj = gen.lib.Family.create(data)
return obj
def make_person(self, person):
if person.cache:
data = cPickle.loads(str(person.cache))
obj = gen.lib.Person.create(data)
else:
data = self.dji.get_person(person)
obj = gen.lib.Person.create(data)
return obj return obj
def get_place_from_handle(self, handle): def get_place_from_handle(self, handle):
@ -239,14 +258,10 @@ class DbDjango(DbWriteBase, DbReadBase):
return (family.handle for family in self.dji.Family.all()) return (family.handle for family in self.dji.Family.all())
def get_person_from_gramps_id(self, gramps_id): def get_person_from_gramps_id(self, gramps_id):
obj = gen.lib.Person() #print "get_person_from_gramps_id", gramps_id
match_list = self.dji.Person.filter(gramps_id=gramps_id) match_list = self.dji.Person.filter(gramps_id=gramps_id)
if match_list.count() > 0: if match_list.count() > 0:
data = self.dji.get_person( return self.make_person(match_list[0])
match_list[0]
)
obj.unserialize(data)
return obj
else: else:
return None return None

View File

@ -412,6 +412,7 @@ class PrimaryObject(models.Model):
blank=True) # user edits blank=True) # user edits
private = models.BooleanField('private') private = models.BooleanField('private')
#attributes = models.ManyToManyField("Attribute", blank=True, null=True) #attributes = models.ManyToManyField("Attribute", blank=True, null=True)
cache = models.TextField(blank=True, null=True)
def __unicode__(self): return "%s: %s" % (self.__class__.__name__, def __unicode__(self): return "%s: %s" % (self.__class__.__name__,
self.gramps_id) self.gramps_id)
@ -433,6 +434,9 @@ class Person(PrimaryObject):
birth = models.ForeignKey("Event", related_name="birth", blank=True, null=True) birth = models.ForeignKey("Event", related_name="birth", blank=True, null=True)
death = models.ForeignKey("Event", related_name="death", blank=True, null=True) death = models.ForeignKey("Event", related_name="death", blank=True, null=True)
birth_ref_index = models.IntegerField("Birth Reference Index", default=-1)
death_ref_index = models.IntegerField("Death Reference Index", default=-1)
tags = models.ManyToManyField('Tag', blank=True, null=True) tags = models.ManyToManyField('Tag', blank=True, null=True)
# Others keys here: # Others keys here:
@ -454,7 +458,7 @@ class Person(PrimaryObject):
return str(self.get_primary_name()) return str(self.get_primary_name())
def make_tag_list(self): def make_tag_list(self):
return [] return tuple()
class Family(PrimaryObject): class Family(PrimaryObject):
father = models.ForeignKey('Person', related_name="father_ref", father = models.ForeignKey('Person', related_name="father_ref",
@ -465,7 +469,7 @@ class Family(PrimaryObject):
tags = models.ManyToManyField('Tag', blank=True, null=True) tags = models.ManyToManyField('Tag', blank=True, null=True)
def make_tag_list(self): def make_tag_list(self):
return [] return tuple()
#lds_list = models.ManyToManyField('Lds', null=True, blank=True) #lds_list = models.ManyToManyField('Lds', null=True, blank=True)
@ -526,7 +530,7 @@ class Media(DateObject, PrimaryObject):
tags = models.ManyToManyField('Tag', blank=True, null=True) tags = models.ManyToManyField('Tag', blank=True, null=True)
def make_tag_list(self): def make_tag_list(self):
return [] return tuple()
class Note(PrimaryObject): class Note(PrimaryObject):
note_type = models.ForeignKey('NoteType') note_type = models.ForeignKey('NoteType')
@ -538,7 +542,7 @@ class Note(PrimaryObject):
tags = models.ManyToManyField('Tag', blank=True, null=True) tags = models.ManyToManyField('Tag', blank=True, null=True)
def make_tag_list(self): def make_tag_list(self):
return [] return tuple()
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
# #

View File

@ -162,6 +162,17 @@ class DjangoInterface(object):
object_type=obj_type) object_type=obj_type)
return map(self.pack_attribute, attribute_list) return map(self.pack_attribute, attribute_list)
def get_primary_name(self, person):
names = person.name_set.filter(preferred=True).order_by("order")
if len(names) > 0:
return gen.lib.Name.create(self.pack_name(names[0]))
else:
return gen.lib.Name()
def get_alternate_names(self, person):
names = person.name_set.filter(preferred=False).order_by("order")
return [gen.lib.Name.create(self.pack_name(n)) for n in names]
def get_names(self, person, preferred): def get_names(self, person, preferred):
names = person.name_set.filter(preferred=preferred).order_by("order") names = person.name_set.filter(preferred=preferred).order_by("order")
if preferred: if preferred:
@ -374,10 +385,9 @@ class DjangoInterface(object):
pnote_list = self.get_note_list(person) pnote_list = self.get_note_list(person)
person_ref_list = self.get_person_ref_list(person) person_ref_list = self.get_person_ref_list(person)
# This looks up the events for the first EventType given: # This looks up the events for the first EventType given:
death_ref_index = lookup_role_index(models.EventType.DEATH, death_ref_index = person.death_ref_index
event_ref_list) birth_ref_index = person.birth_ref_index
birth_ref_index = lookup_role_index(models.EventType.BIRTH,
event_ref_list)
return (str(person.handle), return (str(person.handle),
person.gramps_id, person.gramps_id,
tuple(person.gender_type)[0], tuple(person.gender_type)[0],
@ -1093,14 +1103,19 @@ class DjangoInterface(object):
object_id=person.id, object_id=person.id,
object_type=obj_type, object_type=obj_type,
ref_object__event_type__val=models.EventType.BIRTH) ref_object__event_type__val=models.EventType.BIRTH)
all_events = self.get_event_ref_list(person)
if events: if events:
person.birth = events[0].ref_object person.birth = events[0].ref_object
person.birth_ref_index = lookup_role_index(models.EventType.BIRTH, all_events)
events = models.EventRef.objects.filter( events = models.EventRef.objects.filter(
object_id=person.id, object_id=person.id,
object_type=obj_type, object_type=obj_type,
ref_object__event_type__val=models.EventType.DEATH) ref_object__event_type__val=models.EventType.DEATH)
if events: if events:
person.death = events[0].ref_object person.death = events[0].ref_object
person.death_ref_index = lookup_role_index(models.EventType.DEATH, all_events)
person.save() person.save()
return person return person