db.table._archive_records()
This commit is contained in:
1 parent
66edba7f05
commit
1edbe9e340
3 files changed
+40
-30
No files matched your search
@@ -1 +1 @@
|
|||||||
Version 1.99.7 (2012-04-05 19:09:40) dev
|
Version 1.99.7 (2012-04-05 20:19:10) dev
|
||||||
@@ -6947,6 +6947,30 @@ class Table(dict):
|
|||||||
def update(self,*args,**kwargs):
|
def update(self,*args,**kwargs):
|
||||||
raise RuntimeError, "Syntax Not Supported"
|
raise RuntimeError, "Syntax Not Supported"
|
||||||
|
|
||||||
|
def _archive_records(self,
|
||||||
|
archive_db=None,
|
||||||
|
archive_name = '%(tablename)_archive',
|
||||||
|
current_record = 'current_record'):
|
||||||
|
archive_db = archive_db or self._db
|
||||||
|
fieldnames = self.fields()
|
||||||
|
archive_name = archive_name % dict(tablename=self._tablename)
|
||||||
|
if 'modified_by' in fieldnames and 'modified_on' in fieldnames:
|
||||||
|
archive_table = archive_db.define_table(
|
||||||
|
archive_name,
|
||||||
|
Field(current_record,self),
|
||||||
|
self)
|
||||||
|
self._before_update.append(
|
||||||
|
lambda qset,fs,at=archive_table,cn=current_record:
|
||||||
|
archive_record(qset,fs,at,cn))
|
||||||
|
if 'is_active' in fieldnames:
|
||||||
|
self._before_delete.append(
|
||||||
|
lambda qset: qset.update(is_active=False))
|
||||||
|
newquery = lambda query, t=self: t.is_active == True
|
||||||
|
query = self._common_filter
|
||||||
|
if query:
|
||||||
|
newquery = query & newquery
|
||||||
|
self._common_filter = newquery
|
||||||
|
|
||||||
def _validate(self,**vars):
|
def _validate(self,**vars):
|
||||||
errors = Row()
|
errors = Row()
|
||||||
for key,value in vars.items():
|
for key,value in vars.items():
|
||||||
@@ -7281,6 +7305,16 @@ class Table(dict):
|
|||||||
def on(self, query):
|
def on(self, query):
|
||||||
return Expression(self._db,self._db._adapter.ON,self,query)
|
return Expression(self._db,self._db._adapter.ON,self,query)
|
||||||
|
|
||||||
|
def archive_record(qset,fs,archive_table,current_record):
|
||||||
|
tablenames = qset.db._adapter.tables(qset.query)
|
||||||
|
if len(tablenames)!=1: raise RuntimeError, "cannot update join"
|
||||||
|
table = qset.db[tablenames[0]]
|
||||||
|
for row in qset.select():
|
||||||
|
fields = archive_table._filter_fields(row)
|
||||||
|
fields[current_record] = row.id
|
||||||
|
archive_table.insert(**fields)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Expression(object):
|
class Expression(object):
|
||||||
|
|||||||
+5
-29
@@ -1260,7 +1260,7 @@ class Auth(object):
|
|||||||
def enable_record_versioning(self,
|
def enable_record_versioning(self,
|
||||||
tables,
|
tables,
|
||||||
archive_db = None,
|
archive_db = None,
|
||||||
archive_names='%s_archive',
|
archive_names='%(tablename)s_archive',
|
||||||
current_record='current_record'):
|
current_record='current_record'):
|
||||||
"""
|
"""
|
||||||
to enable full record vernionioning (including auth tables):
|
to enable full record vernionioning (including auth tables):
|
||||||
@@ -1288,36 +1288,12 @@ class Auth(object):
|
|||||||
does automatically.
|
does automatically.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def archive(qset,fs,archive_table,current_name):
|
|
||||||
tablenames = qset.db._adapter.tables(qset.query)
|
|
||||||
if len(tablenames)!=1: raise RuntimeError, "cannot update join"
|
|
||||||
table = qset.db[tablenames[0]]
|
|
||||||
for row in qset.select():
|
|
||||||
fields = archive_table._filter_fields(row)
|
|
||||||
fields[current_record] = row.id
|
|
||||||
archive_table.insert(**fields)
|
|
||||||
|
|
||||||
archive_db = archive_db or self.db
|
|
||||||
tables = [table for table in tables]
|
tables = [table for table in tables]
|
||||||
for table in tables:
|
for table in tables:
|
||||||
fieldnames = table.fields()
|
table._archive_records(
|
||||||
if 'modified_by' in fieldnames and 'modified_on' in fieldnames:
|
archive_db = archive_db,
|
||||||
archive_name = archive_names % table._tablename
|
archive_name = archive_names,
|
||||||
archive_table = archive_db.define_table(
|
current_record = current_record)
|
||||||
archive_name,
|
|
||||||
Field(current_record,table),
|
|
||||||
table)
|
|
||||||
table._before_update.append(
|
|
||||||
lambda qset,fs,at=archive_table,cn=current_record:
|
|
||||||
archive(qset,fs,at,cn))
|
|
||||||
if 'is_active' in fieldnames:
|
|
||||||
table._before_delete.append(
|
|
||||||
lambda qset: qset.update(is_active=False))
|
|
||||||
newquery = lambda query, t=table: t.is_active == True
|
|
||||||
query = table._common_filter
|
|
||||||
if query:
|
|
||||||
newquery = query & newquery
|
|
||||||
table._common_filter = newquery
|
|
||||||
|
|
||||||
def define_tables(self, username=False, signature=None,
|
def define_tables(self, username=False, signature=None,
|
||||||
migrate=True, fake_migrate=False):
|
migrate=True, fake_migrate=False):
|
||||||
|
|||||||
Reference in new issue
Block a user