Person.save() now also computes probably_alive (but only when computing cache, too)

This commit is contained in:
Doug Blank 2013-12-20 00:21:05 -05:00
parent e649e5514d
commit 4051984031
5 changed files with 34 additions and 5 deletions

View File

@ -612,6 +612,20 @@ class Person(PrimaryObject):
def get_selection_string(self): def get_selection_string(self):
return self.name_set.get(preferred=True).get_selection_string() return self.name_set.get(preferred=True).get_selection_string()
def save(self, *args, **kwargs):
from gramps.webapp.utils import probably_alive
if "save_cache" in kwargs:
compute_probably_alive = kwargs["save_cache"]
else:
compute_probably_alive = True
PrimaryObject.save(self, *args, **kwargs)
# expensive! only do this if also saving cache
if compute_probably_alive:
pa = probably_alive(self.handle)
if self.probably_alive != pa:
self.probably_alive = pa
PrimaryObject.save(self, *args, **kwargs)
class Family(PrimaryObject): class Family(PrimaryObject):
father = models.ForeignKey('Person', related_name="father_ref", father = models.ForeignKey('Person', related_name="father_ref",
null=True, blank=True) null=True, blank=True)

View File

@ -117,7 +117,6 @@ def recheck_birth_death_refs(person):
role_type=get_type_from_name(EventRoleType, "Primary"), role_type=get_type_from_name(EventRoleType, "Primary"),
ref_object__event_type__val=EventType.DEATH).order_by("order") ref_object__event_type__val=EventType.DEATH).order_by("order")
if events: if events:
person.probably_alive = False
person.death = events[0].ref_object person.death = events[0].ref_object
new_index = lookup_role_index(EventType.DEATH, all_events) new_index = lookup_role_index(EventType.DEATH, all_events)
if person.death_ref_index != new_index: if person.death_ref_index != new_index:
@ -125,7 +124,6 @@ def recheck_birth_death_refs(person):
else: else:
person.death = None person.death = None
person.death_ref_index = -1 person.death_ref_index = -1
person.probably_alive = True
def process_event(request, context, handle, act, add_to=None): # view, edit, save def process_event(request, context, handle, act, add_to=None): # view, edit, save
""" """

View File

@ -417,9 +417,8 @@ def process_person(request, context, handle, act, add_to=None): # view, edit, sa
# check if valid: # check if valid:
if nf.is_valid() and pf.is_valid() and sf.is_valid() and logform.is_valid(): if nf.is_valid() and pf.is_valid() and sf.is_valid() and logform.is_valid():
# name.preferred and surname.primary get set False in the above is_valid() # name.preferred and surname.primary get set False in the above is_valid()
person.probably_alive = not bool(person.death)
update_last_changed(person, request.user.username) update_last_changed(person, request.user.username)
person = pf.save() person = pf.save(save_cache=False)
# Process data: # Process data:
name.person = person name.person = person
name = nf.save(commit=False) name = nf.save(commit=False)

View File

@ -1940,6 +1940,7 @@ class DjangoInterface(object):
referenced events should be marked not public (public = False). referenced events should be marked not public (public = False).
""" """
from gramps.webapp.utils import probably_alive
if obj.__class__.__name__ == "Event": if obj.__class__.__name__ == "Event":
objref = self.EventRef objref = self.EventRef
elif obj.__class__.__name__ == "Person": elif obj.__class__.__name__ == "Person":
@ -2040,3 +2041,21 @@ class DjangoInterface(object):
count += 1 count += 1
callback(100 * (count/total if total else 0)) callback(100 * (count/total if total else 0))
def update_probably_alive(self, callback=None):
"""
Call this to update primary_alive for people.
"""
from gramps.webapp.utils import probably_alive
if not isinstance(callback, collections.Callable):
callback = lambda percent: None # dummy
callback(0)
count = 0.0
total = self.Person.all().count()
for item in self.Person.all():
pa = probably_alive(item.handle)
if pa != item.probably_alive:
print("Updating probably_alive")
item.probably_alive = pa
item.save()
count += 1
callback(100 * (count/total if total else 0))

View File

@ -152,7 +152,6 @@ def get_person_from_handle(db, handle):
return None return None
def probably_alive(handle): def probably_alive(handle):
return False
person = db.get_person_from_handle(handle) person = db.get_person_from_handle(handle)
return alive(person, db) return alive(person, db)