Commits

Ivan Melnikov committed e8c5456 Draft

Extract search to separate module

Comments (0)

Files changed (6)

   script: user.app
   login: required
 
+- url: /\.search.*
+  script: search.app
+  login: required
+
 - url: /.*
   script: page.app
   login: required
 from creole.html_emitter import HtmlEmitter
 
 from user import User, get_user, auth
-
+from search import add_page
 
 ##################
 ###   Models   ###
 
 
 
-##################
-###   Search   ###
-##################
-
-from google.appengine.api import search
-
-_INDEX_NAME="page"
-
-def add_page(page_id):
-	page = Page.get_by_id(page_id)
-	# add to search index
-	doc = search.Document(doc_id=page.path, 
-						fields=[search.HtmlField(name='html', value=page.html),])
-	try:
-	    search.Index(name=_INDEX_NAME).put(doc)
-	except search.Error:
-	    logging.exception('Put failed')
-
-def search_page(text):
-	docs = []
-	if not text:
-		return docs
-	try:
-	    results = search.Index(name=_INDEX_NAME).search(text)
-	    for doc in results:
-	    	docs.append(doc.doc_id)
-	except search.Error:
-	    logging.exception('Search failed')
-	return docs
-
-
-class SearchPage(webapp2.RequestHandler):
-	@auth
-	def get(self):
-		text = self.request.get('text')
-		docs = search_page(text)
-		
-		template = jinja_environment.get_template('search.html')
-		self.response.out.write(template.render({'docs': docs, 'text': text}))
-
-
-
 debug = os.environ.get('SERVER_SOFTWARE', '').startswith('Dev')
 
-app = webapp2.WSGIApplication([('/.search', SearchPage),
-
-							   (URL_MOVE_PAGE, MovePageCluster),
+app = webapp2.WSGIApplication([(URL_MOVE_PAGE, MovePageCluster),
 
 							   ('/\.preview', PreviewPage),
 							   ('(.*)/\.edit', EditPage),
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import webapp2
+import logging
+import jinja2
+import os
+
+from google.appengine.api import search
+
+from user import auth
+
+
+_INDEX_NAME="page"
+
+def add_page(page_id):
+	from page import Page
+	page = Page.get_by_id(page_id)
+	# add to search index
+	doc = search.Document(doc_id=page.path, 
+						fields=[search.HtmlField(name='html', value=page.html),])
+	try:
+	    search.Index(name=_INDEX_NAME).put(doc)
+	except search.Error:
+	    logging.exception('Put failed')
+
+def search_page(text):
+	docs = []
+	if not text:
+		return docs
+	try:
+	    results = search.Index(name=_INDEX_NAME).search(text)
+	    for doc in results:
+	    	docs.append(doc.doc_id)
+	except search.Error:
+	    logging.exception('Search failed')
+	return docs
+
+
+
+jinja_environment = jinja2.Environment(
+    loader=jinja2.FileSystemLoader(os.path.dirname(__file__) + '/template/search'))
+
+class SearchPage(webapp2.RequestHandler):
+	@auth
+	def get(self):
+		text = self.request.get('text')
+		docs = search_page(text)
+		
+		template = jinja_environment.get_template('search.html')
+		self.response.out.write(template.render({'docs': docs, 'text': text}))
+
+
+
+debug = os.environ.get('SERVER_SOFTWARE', '').startswith('Dev')
+
+app = webapp2.WSGIApplication([('/.search', SearchPage),
+
+								], debug=debug)

template/page/search.html

-{% extends "index.html" %}
-
-{% block content %}
-
-<form>
-<input name="text" size="50"  value="{{ text }}" placeholder="Поиск"/>
-<input type="submit" value="Найти"/>
-</form>
-
-{% for link in docs %}
-<div>
-	<a href="{{ link }}">{{ link }}</a>
-</div>
-{% endfor %}
-
-{% endblock %}

template/search/search.html

+<!DOCTYPE html>
+<html>
+<head>
+	<link rel="icon" type="image/png" href="/_static/img/favicon.png">
+	<link rel="stylesheet" type="text/css" href="/_static/css/page.css">
+	<link rel="stylesheet" type="text/css" href="/_static/css/search.css">
+	<title>Поиск</title>
+</head>
+<body>
+
+<div class="page-content">
+
+<div style="padding-bottom: 45px;">
+
+	<form class="search-form">
+		<input name="text" size="50"  value="{{ text }}" placeholder="Поиск"/>
+		<input type="submit" value="Найти"/>
+	</form>
+
+	{# search results #}
+	{% for link in docs %}
+		<div class="search-item">
+			{{ loop.index }}
+			<a href="{{ link }}">{{ link }}</a>
+		</div>
+	{% endfor %}
+
+</div>
+
+<div class="page-footer">
+	<span>
+		<a href="/_docs/syntax.html">Синтаксис</a>
+	</span>
+	<span>
+		<a href="http://bitbucket.org/imelnikov/gae-wiki/issues/new">Нашли ошибку?</a>
+	</span>
+	<span>
+		<a href="mailto:melnikov.ivan@gmail.com">Вопросы</a>
+	</span>
+	<span>
+		2012-2013 &copy; <a href="http://bitbucket.org/imelnikov/gae-wiki">gae-wiki</a>
+	</span>
+</div>
+
+</div>
+</body>

template/static/css/search.css

+.search-form {
+	background-color: #F9E39F;
+	padding: 7px 30px;
+}
+
+.search-item {
+	padding: 20px;
+	border-bottom: 1px dotted #aaa;
+}
+