X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=search-tags.py;h=fe006bfd5b69f41d1759baa56b8905ad1da58893;hb=af6063f9b0099f34c8817cd5c63802a160bc0463;hp=6675a896935f07a040c1c3c2e38989a6a990c9f0;hpb=5249d271093a266289a1b96946f2a14a36b2ac2e;p=phdru.name%2Fcgi-bin%2Fblog-ru%2Fsearch-tags.git diff --git a/search-tags.py b/search-tags.py index 6675a89..fe006bf 100755 --- a/search-tags.py +++ b/search-tags.py @@ -1,13 +1,61 @@ #! /usr/bin/env python -"""Search tags""" +# coding: koi8-r +"""Search tags CGI""" __author__ = "Oleg Broytman " __copyright__ = "Copyright (C) 2014 PhiloSoft Design" __license__ = "GNU GPL" -import cgi -from parse_query import parser +import cgi, sys +from ply.lex import LexError + +from html.response import redirect, response +from parser.parser import parser form = cgi.FieldStorage() if not form.has_key('q'): - print "Error" + status = "400 Bad request" + title = "Error!" + body = "Required parameter is missing!" + +else: + q = form['q'].value + try: + tree = parser.parse(q) + except LexError: + tree = None + if tree is None: + status = "400 Bad request" + title = "Error!" + body = "Bad query syntax!" + elif tree[0] == 'NAME': # Single tag - just do redirect + tag = tree[1] + assert isinstance(tag, str) + from tags import tag_exists + if tag_exists(tag): + redirect("/Russian/blog/tags/%s.html" % tag, status="301 Moved") + sys.exit() + status = "404 Tag not found" + title = "ïÛÉÂËÁ!" + body = "ôÅÇ %s ÎÅ ÓÕÝÅÓÔ×ÕÅÔ!" % tag + else: # Process tree + from tags import find_tags + posts = find_tags(tree) + status = None + title = "úÁÐÉÓÉ, ÎÁÊÄÅÎÎÙÅ ÄÌÑ ×ÙÒÁÖÅÎÉÑ " + q + if posts: + _posts = ["""\ +

+

+

+""") + body = ''.join(_posts) + else: + body = "îÅ ÎÁÊÄÅÎÏ ÎÉ ÏÄÎÏÊ ÚÁÐÉÓÉ." + +response(title, body, status)