]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/blob - search-tags.py
8e047e1b400fb46fdd79bb1c0f5652f249091433
[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 PhiloSoft Design"
7 __license__ = "GNU GPL"
8
9 import cgi, sys
10 from ply.lex import LexError
11
12 from html.response import redirect, response
13 from parser.parser import parser
14
15 form = cgi.FieldStorage()
16 if not form.has_key('q'):
17     status = "400 Bad request"
18     title = "Error!"
19     body = "Required parameter is missing!"
20
21 else:
22     q = form['q'].value
23     try:
24         tree = parser.parse(q)
25     except LexError:
26         tree = None
27     if tree is None:
28         status = "400 Bad request"
29         title = "Error!"
30         body = "Bad query syntax!"
31     elif tree[0] == 'NAME': # Single tag - just do redirect
32         tag = tree[1]
33         assert isinstance(tag, str)
34         from tags import tag_exists
35         if tag_exists(tag):
36             redirect("/Russian/blog/tags/%s.html" % tag, status="301 Moved")
37             sys.exit()
38         status = "404 Tag not found"
39         title = "Ошибка!"
40         body = "Тег %s не существует!" % tag
41
42 response(title, body, status)