Added rebuild_cache; use base64 string to encode/decode

svn: r18288
This commit is contained in:
Doug Blank 2011-10-12 01:17:29 +00:00
parent 17f1796f1c
commit f47691367c
2 changed files with 80 additions and 2 deletions

View File

@ -27,6 +27,7 @@
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import cPickle import cPickle
import base64
import web import web
import gen import gen
from gen.db import DbReadBase, DbWriteBase from gen.db import DbReadBase, DbWriteBase
@ -187,7 +188,7 @@ class DbDjango(DbWriteBase, DbReadBase):
def make_family(self, family): def make_family(self, family):
if family.cache: if family.cache:
data = cPickle.loads(str(family.cache)) data = cPickle.loads(base64.decodestring(family.cache))
obj = gen.lib.Family.create(data) obj = gen.lib.Family.create(data)
else: else:
data = self.dji.get_family(family) data = self.dji.get_family(family)
@ -196,7 +197,7 @@ class DbDjango(DbWriteBase, DbReadBase):
def make_person(self, person): def make_person(self, person):
if person.cache: if person.cache:
data = cPickle.loads(str(person.cache)) data = cPickle.loads(base64.decodestring(person.cache))
obj = gen.lib.Person.create(data) obj = gen.lib.Person.create(data)
else: else:
data = self.dji.get_person(person) data = self.dji.get_person(person)

View File

@ -28,6 +28,8 @@
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import time import time
import sys import sys
import cPickle
import base64
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #
@ -1396,3 +1398,78 @@ class DjangoInterface(object):
self.add_media_ref_list(event, media_list) self.add_media_ref_list(event, media_list)
self.add_source_ref_list(event, source_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)