]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/blob - search-tags.py
Process single tag
[phdru.name/cgi-bin/blog-ru/search-tags.git] / search-tags.py
1 #! /usr/bin/env python
2 """Search tags CGI"""
3
4 __author__ = "Oleg Broytman <phd@phdru.name>"
5 __copyright__ = "Copyright (C) 2014 PhiloSoft Design"
6 __license__ = "GNU GPL"
7
8 import cgi, sys
9 from ply.lex import LexError
10
11 from html.response import redirect, response
12 from parser.parser import parser
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         tree = parser.parse(q)
24     except LexError:
25         tree = None
26     if tree is None:
27         status = "400 Bad request"
28         title = "Error!"
29         body = "Bad query syntax!"
30     elif tree[0] == 'NAME': # Single tag - just do redirect
31         tag = tree[1]
32         assert isinstance(tag, str)
33         from tags import tag_exists
34         if tag_exists(tag):
35             redirect("/Russian/blog/tags/%s.html" % tag, status="301 Moved")
36             sys.exit()
37         status = "404 Tag not found"
38         title = "Error!"
39         body = "Tag does not exist!"
40
41 response(title, body, status)