当前位置: 首页>>代码示例>>Python>>正文


Python Search.query方法代码示例

本文整理汇总了Python中search.Search.query方法的典型用法代码示例。如果您正苦于以下问题:Python Search.query方法的具体用法?Python Search.query怎么用?Python Search.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在search.Search的用法示例。


在下文中一共展示了Search.query方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: route_search

# 需要导入模块: from search import Search [as 别名]
# 或者: from search.Search import query [as 别名]
def route_search():

	# Get query
	text_query = flask.request.args.get('q', None)
	page_num = flask.request.args.get('page', 1);
	show_syntax = flask.request.args.get('show_syntax', 0)
	has_more_items = False
	expires = 0

	# Fail if bad parameters provided
	try:
		page_num = int(page_num)
		show_syntax = bool(int(show_syntax))
	except ValueError:
		flask.abort(400)

	# Search page
	if text_query is not None:
		query = Search.query(text_query,
							 page_num,
							 offset=(page_num-1)*app.config['ITEMS_PER_PAGE'],
							 count=app.config['ITEMS_PER_PAGE']+1
							 )
		items = query.all()
		if len(items) == app.config['ITEMS_PER_PAGE']+1:
			items = items[:-1]
			has_more_items = True
		query_title = (text_query if len(text_query) > 0 else 'HN Firehose')
		meta_og_title = u'hnapp search: "%s"' % query_title
		title = u'%s – hnapp' % query_title

	# Front page
	else:
		query = None
		items = None
		title = u'hnapp – Hacker News Search With RSS & JSON Feeds'
		meta_og_title = u'hnapp – Hacker News RSS'


	# Meta SEO tags
	meta_keywords = u'Hacker News,RSS,hnapp'
	meta_description = u'Get only the stories and comments you want. Follow users, keywords, jobs, mentions of your product, etc.'


	# Get format
	if flask.request.path == '/':
		output_format = None
		html_template = 'search.html'
	else:
		if text_query is None:
			flask.abort(400)
		output_format = flask.request.path[1:]
		if output_format == 'bare':
			html_template = 'parts/items.html'


	# Those users who created their filters on alpha version new.hnapp.com
	# don't expect comments, so we fix it for them
	if flask.request.args.get('legacy', 0) == '1':
		# Not a search – redirect to home page
		if text_query is None:
			return flask.redirect(query_url(None, output_format=None), code=302)
		else:
			return flask.redirect(query_url('type:story '+text_query, output_format=output_format), code=302)


	# Web page or bare HTML
	if output_format in (None, 'bare'):
		page_data = {
			'is_app': True,
			'title': title,
			'meta_og_title': meta_og_title,
			'meta_keywords': meta_keywords,
			'meta_description': meta_description,
			'show_syntax': show_syntax,
			'query': text_query,
			'items': items,
			'has_more_items': int(has_more_items),
			'page_expires_at': int(time.time()) + 60*5, # Cache pages for this many seconds
			'this_url': flask.request.url,
			'prev_url': query_url(text_query, page_num=page_num-1) if query is not None else None,
			'next_url': query_url(text_query, page_num=page_num+1) if query is not None else None,
			'rss_url': query_url(text_query, output_format='rss') if query is not None else None,
			'json_url': query_url(text_query, output_format='json') if query is not None else None,
			'page_num': page_num,
			'ga_id': app.config['GA_ID'],
			'HOST_NAME': app.config['HOST_NAME']
			}
		return flask.render_template(html_template, **page_data)


	# RSS
	elif output_format == 'rss':
		feed = AtomFeed(title=title.encode('ascii', 'xmlcharrefreplace'),
						title_type='html',
						feed_url=query_url(text_query, output_format='rss'),
						author='via hnapp',
						url=query_url(text_query),
						generator=('hnapp', app.config['HOST_NAME'], None)
						)
#.........这里部分代码省略.........
开发者ID:raquo,项目名称:hnapp,代码行数:103,代码来源:hnapp.py


注:本文中的search.Search.query方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。