]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/commitdiff
Process single tag
authorOleg Broytman <phd@phdru.name>
Tue, 20 May 2014 03:29:24 +0000 (07:29 +0400)
committerOleg Broytman <phd@phdru.name>
Tue, 20 May 2014 04:31:56 +0000 (08:31 +0400)
Verify the tag exists and do a redirect to its static page.

search-tags.py
tags.py [new file with mode: 0644]

index 9bdfc8bb5bc1e4cd6711e6f7f4caad5fe5bc464c..8d322d12057ed3290646c67c3c99b94846a6e877 100755 (executable)
@@ -5,10 +5,10 @@ __author__ = "Oleg Broytman <phd@phdru.name>"
 __copyright__ = "Copyright (C) 2014 PhiloSoft Design"
 __license__ = "GNU GPL"
 
 __copyright__ = "Copyright (C) 2014 PhiloSoft Design"
 __license__ = "GNU GPL"
 
-import cgi
+import cgi, sys
 from ply.lex import LexError
 
 from ply.lex import LexError
 
-from html.response import response
+from html.response import redirect, response
 from parser.parser import parser
 
 form = cgi.FieldStorage()
 from parser.parser import parser
 
 form = cgi.FieldStorage()
@@ -27,5 +27,15 @@ else:
         status = "400 Bad request"
         title = "Error!"
         body = "Bad query syntax!"
         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 = "Error!"
+        body = "Tag does not exist!"
 
 response(title, body, status)
 
 response(title, body, status)
diff --git a/tags.py b/tags.py
new file mode 100644 (file)
index 0000000..7ff886d
--- /dev/null
+++ b/tags.py
@@ -0,0 +1,25 @@
+blog_filename = "blog_dict.pickle"
+
+try:
+   import cPickle as pickle
+except ImportError:
+   import pickle
+
+try:
+   blog_file = open(blog_filename, "rb")
+except IOError:
+   blog_dict = {}
+else:
+   blog_dict = pickle.load(blog_file)
+   blog_file.close()
+
+
+# blog_dict is a mapping
+# (year, month, day) => [list of (file, title, lead, tags)]
+
+def tag_exists(tag):
+    for posts in blog_dict.itervalues():
+        for _file, _title, _lead, tags in posts:
+            if tag in tags:
+                return True
+    return False