From 4051984031cc8579ff947e8e69825534f2c54484 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Fri, 20 Dec 2013 00:21:05 -0500 Subject: [PATCH] Person.save() now also computes probably_alive (but only when computing cache, too) --- gramps/webapp/grampsdb/models.py | 14 ++++++++++++++ gramps/webapp/grampsdb/view/event.py | 2 -- gramps/webapp/grampsdb/view/person.py | 3 +-- gramps/webapp/libdjango.py | 19 +++++++++++++++++++ gramps/webapp/utils.py | 1 - 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/gramps/webapp/grampsdb/models.py b/gramps/webapp/grampsdb/models.py index 2e75f63bb..16cbb7b57 100644 --- a/gramps/webapp/grampsdb/models.py +++ b/gramps/webapp/grampsdb/models.py @@ -612,6 +612,20 @@ class Person(PrimaryObject): def get_selection_string(self): 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): father = models.ForeignKey('Person', related_name="father_ref", null=True, blank=True) diff --git a/gramps/webapp/grampsdb/view/event.py b/gramps/webapp/grampsdb/view/event.py index 4f50ea2e5..5e3387b35 100644 --- a/gramps/webapp/grampsdb/view/event.py +++ b/gramps/webapp/grampsdb/view/event.py @@ -117,7 +117,6 @@ def recheck_birth_death_refs(person): role_type=get_type_from_name(EventRoleType, "Primary"), ref_object__event_type__val=EventType.DEATH).order_by("order") if events: - person.probably_alive = False person.death = events[0].ref_object new_index = lookup_role_index(EventType.DEATH, all_events) if person.death_ref_index != new_index: @@ -125,7 +124,6 @@ def recheck_birth_death_refs(person): else: person.death = None person.death_ref_index = -1 - person.probably_alive = True def process_event(request, context, handle, act, add_to=None): # view, edit, save """ diff --git a/gramps/webapp/grampsdb/view/person.py b/gramps/webapp/grampsdb/view/person.py index 49c82af6d..a11458785 100644 --- a/gramps/webapp/grampsdb/view/person.py +++ b/gramps/webapp/grampsdb/view/person.py @@ -417,9 +417,8 @@ def process_person(request, context, handle, act, add_to=None): # view, edit, sa # check if 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() - person.probably_alive = not bool(person.death) update_last_changed(person, request.user.username) - person = pf.save() + person = pf.save(save_cache=False) # Process data: name.person = person name = nf.save(commit=False) diff --git a/gramps/webapp/libdjango.py b/gramps/webapp/libdjango.py index ec0f012c8..d260d39e5 100644 --- a/gramps/webapp/libdjango.py +++ b/gramps/webapp/libdjango.py @@ -1940,6 +1940,7 @@ class DjangoInterface(object): referenced events should be marked not public (public = False). """ + from gramps.webapp.utils import probably_alive if obj.__class__.__name__ == "Event": objref = self.EventRef elif obj.__class__.__name__ == "Person": @@ -2040,3 +2041,21 @@ class DjangoInterface(object): count += 1 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)) diff --git a/gramps/webapp/utils.py b/gramps/webapp/utils.py index 6b588c275..564a9ef41 100644 --- a/gramps/webapp/utils.py +++ b/gramps/webapp/utils.py @@ -152,7 +152,6 @@ def get_person_from_handle(db, handle): return None def probably_alive(handle): - return False person = db.get_person_from_handle(handle) return alive(person, db)