-
Notifications
You must be signed in to change notification settings - Fork 4
Maintainance functions in list modules
Sometimes it is necessary to write simple maintainance functions for data manipulation or to simply delete entries.
This can be done easily by copying and modifying the following function stub. This stub simply runs data modification within the request, and dumps a tiny HTML-site containing a link showing the amount of records modfied, and continues with the next batch.
@exposed
def clearAll(self, cursor = None, *args, **kwargs):
q = self.viewSkel().all()
q = self.listFilter(q)
if not q:
raise errors.Unauthorized()
q.cursor(cursor)
count = 0
for skel in q.fetch():
skel.delete() # This simply deletes the record.
# You could also do some other, task-related things like modifying data or spawning processes.
count += 1
if count:
return """
<html><a href="{0}?cursor={1}">{2}</a></html>
""".format(request.current.get().request.url, q.getCursor().urlsafe(), count)
else:
return "<html>0</html>"
Alternatively, when running on huge record sets, the task may be run deferred. This is the method used under these circumstances. Watch out for the different access control mechanism used here. Above, the listFilter() is used, in this case, no listFilter() is used but the user has to be logged in and the "root"-flag is required to kick-off the request.
@exposed
def clearAll(self, *args, **kwargs):
cuser = utils.getCurrentUser()
if not (cuser and "root" in cuser["access"]):
raise errors.Unauthorized()
self.doClearAll()
@callDeferred
def doClearAll(self, cursor = None, *args, **kwargs):
q = self.viewSkel().all()
q.cursor(cursor)
count = 0
for skel in q.fetch():
skel.delete() # This simply deletes the record.
# You could also do some other, task-related things like modifying data or spawning processes.
count += 1
if count:
self.doClearAll(q.getCursor().urlsafe())
It is useful to provide debugging output when running deferred tasks to get to know about what's going on in the background ;-) ...