--- /dev/null
+<div class="row">
+ <div class="col-lg-6 col-md-6 col-lg-offset-3 ac">
+ <form class="form-inline" action="/search" method="GET">
+ <div class="form-group">
+ <label class="sr-only" for="searchBox">{{ _("Search") }}</label>
+ <input type="text" class="form-control" name="q" id="searchBox"
+ placeholder="{{ _("Search") }}" autocomplete="off" {% if query %}value="{{ query }}"{% end %}>
+ <button type="submit" class="btn btn-default">
+ <i class="glyphicon glyphicon-search"></i>
+ </button>
+
+ <div class="btn-group">
+ <a class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="#">
+ {{ _("All posts from") }}
+ <span class="caret"></span>
+ </a>
+ <ul class="dropdown-menu">
+ {% for y in years %}
+ <li><a href="/year/{{ y }}">{{ y }}</a></li>
+ {% end %}
+ </ul>
+ </div>
+ </div>
+ </form>
+ </div>
+</div>
</p>
<br>
- <div class="row">
- <div class="col-lg-6 col-md-6 col-lg-offset-3 ac">
- <form class="form-inline" action="/search" method="GET">
- <div class="form-group">
- <label class="sr-only" for="searchBox">{{ _("Search") }}</label>
- <input type="text" class="form-control planet-search-autocomplete" name="q" id="searchBox"
- placeholder="{{ _("Search") }}" autocomplete="off">
- <button type="submit" class="btn btn-default">
- <i class="glyphicon glyphicon-search"></i>
- </button>
-
- <div class="btn-group">
- <a class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="#">
- {{ _("All posts from") }}
- <span class="caret"></span>
- </a>
- <ul class="dropdown-menu">
- {% for y in years %}
- <li><a href="/year/{{ y }}">{{ y }}</a></li>
- {% end %}
- </ul>
- </div>
- </div>
- </form>
- </div>
- </div>
+ {% module PlanetSearchBox() %}
<hr class="separator">
{% end block %}
{% block body %}
+ {% module PlanetSearchBox(query=query) %}
+
+ <hr class="separator">
+
{% if entries %}
{% for entry in entries %}
{% module PlanetEntry(entry) %}
"NewsTable" : NewsTableModule,
"NewsYearNavigation" : NewsYearNavigationModule,
"PlanetEntry" : PlanetEntryModule,
+ "PlanetSearchBox" : PlanetSearchBoxModule,
"ReleaseItem" : ReleaseItemModule,
"SidebarBanner" : SidebarBannerModule,
"SidebarRelease" : SidebarReleaseModule,
(r"/search", PlanetSearchHandler),
(r"/year/(\d+)", PlanetYearHandler),
- # API
- (r"/api/planet/search/autocomplete", PlanetAPISearchAutocomplete),
-
# RSS
(r"/rss", RSSPlanetAllHandler),
(r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
(r"/downloads", AdminDownloadsHandler),
(r"/downloads/mirrors", AdminDownloadsMirrorsHandler),
# API
- (r"/api/planet/search/autocomplete", PlanetAPISearchAutocomplete),
(r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
] + static_handlers)
return id
def search(self, what):
- # Split tags.
- tags = what.split()
-
- query = "SELECT planet.* FROM planet INNER JOIN ( \
- SELECT post_id FROM planet_tags \
- INNER JOIN planet ON planet_tags.post_id = planet.id \
- WHERE %s GROUP BY post_id HAVING COUNT(post_id) = %%s \
- ) pt ON planet.id = pt.post_id ORDER BY published DESC"
-
- args = (tags, len(tags))
-
- clauses, args = [], tags
- for tag in tags:
- clauses.append("planet_tags.tag = %s")
- args.append(len(tags))
-
- entries = self.db.query(query % " OR ".join(clauses), *args)
- return [PlanetEntry(self.backend, e) for e in entries]
-
- def search_autocomplete(self, what):
- tags = what.split()
- last_tag = tags.pop()
-
- res = self.db.query("SELECT tag, COUNT(tag) AS count FROM planet_tags \
- WHERE tag LIKE %s GROUP BY tag ORDER BY count DESC", "%s%%" % last_tag)
-
- if tags:
- return ["%s %s" % (" ".join(tags), row.tag) for row in res]
-
- return [row.tag for row in res]
+ res = self.db.query("WITH \
+ q AS (SELECT plainto_tsquery(%s, %s) AS query), \
+ ranked AS (SELECT id, query, ts_rank_cd(to_tsvector(%s, markdown), query) AS rank \
+ FROM planet, q WHERE markdown @@ query ORDER BY rank DESC) \
+ SELECT *, ts_headline(markup, ranked.query, 'MinWords=100, MaxWords=110') AS markup FROM planet \
+ JOIN ranked ON planet.id = ranked.id \
+ WHERE status = %s AND published IS NOT NULL AND published <= NOW() \
+ ORDER BY ranked DESC LIMIT 10",
+ "english", what, "english", "published")
+
+ return [PlanetEntry(self.backend, e) for e in res]
limit = int(self.get_argument("limit", 4))
entries = self.planet.get_entries(offset=offset, limit=limit)
- years = self.planet.get_years()
- self.render("planet/index.html", entries=entries, years=years,
- offset=offset + limit, limit=limit)
+ self.render("planet/index.html", entries=entries, offset=offset + limit, limit=limit)
class PlanetUserHandler(PlanetBaseHandler):
months.sort(reverse=True)
self.render("planet/year.html", months=months, year=year)
-
-
-class PlanetAPISearchAutocomplete(PlanetBaseHandler):
- def get(self):
- query = self.get_argument("q")
- if not query:
- raise tornado.web.HTTPError(400)
-
- results = self.planet.search_autocomplete(query)
-
- res = {
- "query" : query,
- "results" : results,
- }
- self.write(res)
def news(self):
return self.handler.news
+ @property
+ def planet(self):
+ return self.handler.planet
+
class AdvertisementModule(UIModule):
def render(self, where):
active=active, years=self.news.years)
+class PlanetSearchBoxModule(UIModule):
+ def render(self, query=None):
+ years = self.planet.get_years()
+
+ return self.render_string("modules/planet/search-box.html",
+ query=query, years=years)
+
+
class SidebarItemModule(UIModule):
def render(self):
return self.render_string("modules/sidebar-item.html")