def clean_results(idx, results, query=None): schema = idx.schema ret, default_document = list(), get_default_schema(schema) metadata = DotDict() if hasattr(results, 'runtime'): # regular search.search(..) method call runtime = results.runtime else: if hasattr(results, 'results'): # search.search_page(..) hides the underlying # `results` object as a new attribute in a `ResultsPage` # wrapper. runtime = results.results.runtime else: # if we can't get the runtime, suggest a negative # number...hopefully this doesn't happen and might # be considered a bug if the runtime returns as -1 runtime = -1 # round the runtime to the nearest 4th decimal place metadata.search_time = round(runtime, 4) metadata.count = len(results) metadata.query = str(query) for doc in results: # create a copy of the default structure # then replace the fields that exist in our # returned document; this will also add any # fields/columns that didn't exist in the original schema. res = dict(default_document) res.update(doc.items()) # convert the pmeta string (json) into a dict object try: m = '{}' if doc['pmeta'] is None else doc['pmeta'] m = json.loads(m) except Exception as e: m = {} finally: res['pmeta'] = m # ensure we have a created date if res['created'] is None: res['created'] = res['modified'] ret.append(res) metadata.results = ret return metadata
def make_app(**settings): cwd = os.getcwd() TORNADO_CONFIG.update(settings) app_settings = DotDict(APP_CONFIG) # setup the logging configuration logging.config.dictConfigClass(app_settings.logging).configure() logger.info('cwd, ' + cwd) logger.info('creating application') # determine where our flat assets are stored static_path = os.path.join(cwd, 'static') template_path = os.path.join(cwd, 'templates') TORNADO_CONFIG['static_path'] = settings.get('static_path', static_path) TORNADO_CONFIG['template_path'] = settings.get('template_path', template_path) # set up our routing configuration app = Application([ # hard routes url(r'/feed.atom', AtomFeedHandler), # soft routes url(r'/login', LoginHandler), url(r'/admin/create', AdminCreateHandler), url(r'/search', DynamicSearchHandler), url(r'/search/([a-z0-9_]+)/([a-zA-Z0-9\+\-_\%]+)(\.atom)?', StaticSearchHandler), url(r'/blog/([a-zA-Z0-9\-_]+)\.html', OldBlogHandler), url(r'/blog/([a-z]+)/(\d+)/(\d+)/(.*)/(\d+)', BlogHandler), # catch-all routes url(r'/static/(.*)', StaticFileHandler, {'path': static_path}), url(r'/media/(.*)', StaticFileHandler, {'path': app_settings.media}), url(r'/(.*\.(txt|ico))', StaticFileHandler, {'path': static_path}), url(r'/', IndexHandler), url(r'/(.*)', ErrorHandler) ], **TORNADO_CONFIG) logger.info('finished setting up HTTP routes') # storage location for singletons and factory methods app.meta = DotDict() app.meta.seo = SEO_VALUES # ~~ config Search app.meta.search_index = obtain_index( WHOOSH.get('location'), SowingSchema, WHOOSH.get('index_name')) logger.info('finished binding to Whoosh index') # ~~ private settings private_settings = app_settings.pop('private_settings', {}) # bootstrap the private settings into the meta-object accessible # in the BaseHandler webRequest class app.meta.private = private_settings # ~~ authentication auth = private_settings.get('auth', {}) username, password = auth.get('username', None), auth.get('password', None) app.meta.auth_token = auth.get('token', None) app.meta.username_combo = (username, password,) logger.info('finished configuring authentication') # callback settings for launching the server app.meta.settings = app_settings return app, app_settings