]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/blob - search-tags.py
Remove generated html/redirect.py
[phdru.name/cgi-bin/blog-ru/search-tags.git] / search-tags.py
1 #! /usr/bin/env python
2 # coding: koi8-r
3 """Search tags CGI"""
4
5 __author__ = "Oleg Broytman <phd@phdru.name>"
6 __copyright__ = "Copyright (C) 2014-2016 PhiloSoft Design"
7 __license__ = "GNU GPL"
8
9 import cgi, sys
10 from grako.exceptions import FailedParse
11
12 from html.response import redirect, response
13
14 form = cgi.FieldStorage()
15 if not form.has_key('q'):
16     status = "400 Bad request"
17     title = "Error!"
18     body = "Required parameter is missing!"
19
20 else:
21     q = form['q'].value
22     try:
23         from parser.parser import TagsParser
24         from parser.build_ast import TagsSemantics
25         parser = TagsParser(parseinfo=False)
26         tree = parser.parse(q, semantics=TagsSemantics())
27     except FailedParse:
28         status = "400 Bad request"
29         title = "Error!"
30         body = "Bad query syntax!"
31     else:
32         if tree[0] == 'NAME': # Single tag - just do redirect
33             tag = tree[1]
34             assert isinstance(tag, str)
35             from tags import tag_exists
36             if tag_exists(tag):
37                 redirect("/Russian/blog/tags/%s.html" % tag, status="301 Moved")
38                 sys.exit()
39             status = "404 Tag not found"
40             title = "Ошибка!"
41             body = "Тег %s не существует!" % tag
42         else: # Process tree
43             from tags import find_tags
44             posts = find_tags(tree)
45             status = None
46             title = "Записи, найденные для выражения " + cgi.escape(q)
47             if posts:
48                 _posts = ["""\
49     <p class="head">
50     <ul>
51     """]
52                 for year, month, day, suburl, _title in posts:
53                     _posts.append('<li>%s-%s-%s <a href="../../../../Russian/blog/%s">%s</a></li>\n' % (year, month, day, suburl, _title))
54                 _posts .append("""\
55     </ul>
56     </p>
57     """)
58                 body = ''.join(_posts)
59             else:
60                 body = "Не найдено ни одной записи."
61
62 response(title, body, status)