]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/blobdiff - search-tags.py
Rename calc_tree to find_tags
[phdru.name/cgi-bin/blog-ru/search-tags.git] / search-tags.py
index 9bdfc8bb5bc1e4cd6711e6f7f4caad5fe5bc464c..afdd6c47823cf0aef61b6429c945758dcca6a349 100755 (executable)
@@ -1,14 +1,15 @@
 #! /usr/bin/env python
+# coding: koi8-r
 """Search tags CGI"""
 
 __author__ = "Oleg Broytman <phd@phdru.name>"
 __copyright__ = "Copyright (C) 2014 PhiloSoft Design"
 __license__ = "GNU GPL"
 
-import cgi
+import cgi, sys
 from ply.lex import LexError
 
-from html.response import response
+from html.response import redirect, response
 from parser.parser import parser
 
 form = cgi.FieldStorage()
@@ -27,5 +28,27 @@ else:
         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 = []
+            for suburl, title in posts:
+                _posts.append('<a href="/Russian/blog/%s">%s</a>' % (suburl, title))
+            body = "<br>\n".join(_posts)
+        else:
+            body = "Не найдено ни одной записи."
 
 response(title, body, status)