<s> import datetime <EOL> import webapp2 <EOL> from google . appengine . ext import db <EOL> from google . appengine . api import search <EOL> from application import config <EOL> from application . models . datastore . application_model import * <EOL> class ClearDocumentsHandler ( webapp2 . RequestHandler ) : <EOL> def get ( self ) : <EOL> date_tag = datetime . datetime . now ( ) - datetime . timedelta ( days = config . document_expiration ) <EOL> options = search . QueryOptions ( returned_fields = [ '<STR_LIT>' ] ) <EOL> query = search . Query ( query_string = '<STR_LIT>' % date_tag . strftime ( '<STR_LIT>' ) , options = options ) <EOL> for application in ApplicationModel . all ( ) : <EOL> self . __delete_text_search ( '<STR_LIT>' , str ( application . key ( ) . id ( ) ) , query ) <EOL> self . __delete_text_search ( '<STR_LIT>' , str ( application . key ( ) . id ( ) ) , query ) <EOL> self . __delete_text_search ( '<STR_LIT>' , str ( application . key ( ) . id ( ) ) , query ) <EOL> self . __delete_data_store ( '<STR_LIT>' , date_tag ) <EOL> self . __delete_data_store ( '<STR_LIT>' , date_tag ) <EOL> self . __delete_data_store ( '<STR_LIT>' , date_tag ) <EOL> def __delete_text_search ( self , namespace , name , query ) : <EOL> """<STR_LIT>""" <EOL> index = search . Index ( namespace = namespace , name = name ) <EOL> try : <EOL> document_ids = [ x . doc_id for x in index . search ( query ) ] <EOL> index . delete ( document_ids ) <EOL> except Exception : <EOL> pass <EOL> def __delete_data_store ( self , model_name , date_tag ) : <EOL> query = db . GqlQuery ( '<STR_LIT>' % model_name , date_tag ) <EOL> models = query . fetch ( <NUM_LIT:1000> ) <EOL> db . delete ( models ) <EOL> app = webapp2 . WSGIApplication ( [ <EOL> ( '<STR_LIT>' , ClearDocumentsHandler ) <EOL> ] ) </s>
