From 30a4c5b846742f52fd034d82720235f6defce60b Mon Sep 17 00:00:00 2001 From: rose1988c Date: Wed, 18 Sep 2013 15:43:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=90=9C=E7=B4=A2=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/search.py | 41 +++++++++++++++++++++++++++++++ templates/base.html | 6 +++++ templates/search.html | 57 +++++++++++++++++++++++++++++++++++++++++++ urls.py | 6 +++-- 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 apps/search.py create mode 100644 templates/search.html diff --git a/apps/search.py b/apps/search.py new file mode 100644 index 0000000..72f886c --- /dev/null +++ b/apps/search.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + search 搜索 +""" + +import tornado.web +import tornado.database +from settings import db, NAVNUM +from libs import markdown +from tornado.escape import xhtml_escape + +md = markdown.Markdown(safe_mode=True) + +class BaseHandler(tornado.web.RequestHandler): + + @property + def db(self): + blogdb = tornado.database.Connection( + host=db["host"] + ":" + db["port"], database=db["db"], + user=db["user"], password=db["password"]) + return blogdb + + def get_current_user(self): + user_id = self.get_secure_cookie("user") + if not user_id: + return None + return self.db.get("SELECT * FROM users WHERE id = %s", int(user_id)) + +class CodeHandler(BaseHandler): + + def get(self): + + q = xhtml_escape(self.get_argument("q")) + + entries = self.db.query("SELECT * FROM entries where title like '%%" + q + "%%' LIMIT 30") + + count = len(entries) + pages = (count - 1) / NAVNUM + 1 + self.render("search.html", entries=entries, pages=pages, counts=count) diff --git a/templates/base.html b/templates/base.html index a408d7f..3dad0c5 100644 --- a/templates/base.html +++ b/templates/base.html @@ -16,6 +16,12 @@
+
+
+ + +
+

{{ escape(handler.settings["sitename"]) }}

diff --git a/templates/search.html b/templates/search.html new file mode 100644 index 0000000..307bf2a --- /dev/null +++ b/templates/search.html @@ -0,0 +1,57 @@ +{% extends "base.html" %} + +{% block body %} + + + + + + + + + + {% for i,entry in enumerate(entries) %} + + + + + + {% end %} + +
序号标题介绍
#{{counts-i}}{{entry.title}}{{entry.info[ :100]}}
+ +{% end %} diff --git a/urls.py b/urls.py index 8d362f5..60a9ab2 100644 --- a/urls.py +++ b/urls.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from apps import code, admin +from apps import code, admin, search urls = [(r"/", code.HomeHandler), (r"/(\d+)", code.EntryHandler), @@ -10,8 +10,10 @@ (r"/update/(\d+)", code.UpdateHandler), (r"/delete", code.DeleteHandler), (r"/page/(\d+)", code.PageHandler), + (r"/search/", search.CodeHandler), (r"/auth/login", admin.LoginHandler), (r"/auth/logout", admin.LogoutHandler), (r"/admin/start", admin.SiteStartHandler), (r"/admin/delete/(\d+)", admin.DeleteHandler), - (r"/feed", code.FeedHandler), ] + (r"/feed", code.FeedHandler), + ]