X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=search-tags.py;h=27f14f0865f895f893349651784ff059b9114ba7;hb=c7e506be9b8c785dbdfae35247e5f3ed5c0e90f0;hp=cbac10c6bbdac3eee8d9b171667938976afdfeeb;hpb=457d8766cac234bb006d4c2aff05e4519f2e58ac;p=phdru.name%2Fcgi-bin%2Fblog-ru%2Fsearch-tags.git diff --git a/search-tags.py b/search-tags.py index cbac10c..27f14f0 100755 --- a/search-tags.py +++ b/search-tags.py @@ -1,13 +1,16 @@ #! /usr/bin/env python +# 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 -from html.response import response +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'): @@ -15,4 +18,37 @@ if not form.has_key('q'): 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 calc_tree + posts = calc_tree(tree) + status = None + title = "úÁÐÉÓÉ, ÎÁÊÄÅÎÎÙÅ ÄÌÑ ×ÙÒÁÖÅÎÉÑ " + q + if posts: + _posts = [] + for suburl, title in posts: + _posts.append('%s' % (suburl, title)) + body = "
\n".join(_posts) + else: + body = "îÅ ÎÁÊÄÅÎÏ ÎÉ ÏÄÎÏÊ ÚÁÐÉÓÉ." + response(title, body, status)