I tried to clear and change the code in the answer here for my needs, where I want to remove only from Model Reservations for data records until the date expressed in get, like yy,mm,dd .
If I correctly anticipate the action cleanTable/2012/10/5 against routing ('/cleanTable/([\d]+)/([\d]+)/([\d]+)', CleanTable) , then my code will delete no more than 50 (10 * nlimit) data records.
Btw, the author of the source code (which is most likely no longer subscribing to SO), claimed that his main trick for executing this code was to "enable redirection in html instead of using self.redirect".
I am not familiar with raise Exception and the like, but my instinct should be to add a raise Exception or raise StopIteration loop to a for loop after it turns into a while loop. But it is not clear to me whether it is worth raising the StopIteration exception, the iteration stops or more is required. Also, I don't know how to rethink, so the html ends smoothly on early exit.
class CleanTable(BaseHandler): def get(self, yy,mm,dd): nlimit=5 iyy=int(yy) imm=int(mm) idd=int(dd) param=date(iyy,imm,idd) q=Reservations.all(keys_only=True) q.filter("date < ", dt(iyy,imm,idd)) results = q.fetch(nlimit) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write(""" <html> <meta HTTP-EQUIV="REFRESH" content="url=http://yourapp.appspot.com/cleanTable"> <body>""") try: for i in range(10): db.delete(results) results = q.fetch(nlimit, len(results)) for r in results: logging.info("r.name: %s" % r.name) self.response.out.write("<p> "+str(nlimit)+" removed</p>") self.response.out.write(""" </body> </html>""") except Exception, inst: logging.info("inst: %s" % inst) self.response.out.write(str(inst))
source share