From f47691367c1382e4e02ff2f749408d20b85512e6 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Wed, 12 Oct 2011 01:17:29 +0000 Subject: [PATCH] Added rebuild_cache; use base64 string to encode/decode svn: r18288 --- src/web/dbdjango.py | 5 +-- src/web/libdjango.py | 77 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/web/dbdjango.py b/src/web/dbdjango.py index 9a9c8c258..48d67ef58 100644 --- a/src/web/dbdjango.py +++ b/src/web/dbdjango.py @@ -27,6 +27,7 @@ # #------------------------------------------------------------------------ import cPickle +import base64 import web import gen from gen.db import DbReadBase, DbWriteBase @@ -187,7 +188,7 @@ class DbDjango(DbWriteBase, DbReadBase): def make_family(self, family): if family.cache: - data = cPickle.loads(str(family.cache)) + data = cPickle.loads(base64.decodestring(family.cache)) obj = gen.lib.Family.create(data) else: data = self.dji.get_family(family) @@ -196,7 +197,7 @@ class DbDjango(DbWriteBase, DbReadBase): def make_person(self, person): if person.cache: - data = cPickle.loads(str(person.cache)) + data = cPickle.loads(base64.decodestring(person.cache)) obj = gen.lib.Person.create(data) else: data = self.dji.get_person(person) diff --git a/src/web/libdjango.py b/src/web/libdjango.py index b5dc21530..d5c3767c0 100644 --- a/src/web/libdjango.py +++ b/src/web/libdjango.py @@ -28,6 +28,8 @@ #------------------------------------------------------------------------ import time import sys +import cPickle +import base64 #------------------------------------------------------------------------ # @@ -1396,3 +1398,78 @@ class DjangoInterface(object): self.add_media_ref_list(event, media_list) self.add_source_ref_list(event, source_list) + def rebuild_caches(self, callback=None): + """ + Call this to rebuild the caches for all primary models. + """ + if not callable(callback): + callback = lambda (percent): None # dummy + + callback(0) + count = 0.0 + total = (self.Note.all().count() + + self.Person.all().count() + + self.Event.all().count() + + self.Family.all().count() + + self.Repository.all().count() + + self.Place.all().count() + + self.Media.all().count() + + self.Source.all().count()) + + for item in self.Person.all(): + raw = self.get_person(item) + item.cache = base64.encodestring(cPickle.dumps(raw)) + item.save() + callback(100 * count/total) + count += 1 + + for item in self.Family.all(): + raw = self.get_family(item) + item.cache = base64.encodestring(cPickle.dumps(raw)) + item.save() + callback(100 * count/total) + count += 1 + + for item in self.Source.all(): + raw = self.get_source(item) + item.cache = base64.encodestring(cPickle.dumps(raw)) + item.save() + callback(100 * count/total) + count += 1 + + for item in self.Event.all(): + raw = self.get_event(item) + item.cache = base64.encodestring(cPickle.dumps(raw)) + item.save() + callback(100 * count/total) + count += 1 + + for item in self.Repository.all(): + raw = self.get_repository(item) + item.cache = base64.encodestring(cPickle.dumps(raw)) + item.save() + callback(100 * count/total) + count += 1 + + for item in self.Place.all(): + raw = self.get_place(item) + item.cache = base64.encodestring(cPickle.dumps(raw)) + item.save() + callback(100 * count/total) + count += 1 + + for item in self.Media.all(): + raw = self.get_media(item) + item.cache = base64.encodestring(cPickle.dumps(raw)) + item.save() + callback(100 * count/total) + count += 1 + + for item in self.Note.all(): + raw = self.get_note(item) + item.cache = base64.encodestring(cPickle.dumps(raw)) + item.save() + callback(100 * count/total) + count += 1 + + callback(100)