[Xfce4-commits] <xfce-buildbot-scripts:master> Store and retrieve the chosen layout from cookies

Enrico Tröger noreply at xfce.org
Sat Oct 10 00:20:03 CEST 2009


Updating branch refs/heads/master
         to bb29c2ab4d07a1089fbf8d745a62e90a2d9ddda2 (commit)
       from cedc1802520b88550c8761b91e24196456c2c95d (commit)

commit bb29c2ab4d07a1089fbf8d745a62e90a2d9ddda2
Author: Enrico Tröger <enrico.troeger at uvena.de>
Date:   Sat Oct 10 00:18:43 2009 +0200

    Store and retrieve the chosen layout from cookies

 xfcebuildstatus.py |   58 +++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/xfcebuildstatus.py b/xfcebuildstatus.py
index 89ffbce..393ac89 100644
--- a/xfcebuildstatus.py
+++ b/xfcebuildstatus.py
@@ -20,15 +20,15 @@
 #       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
 
-from xmlrpclib import ServerProxy, Error
-from string import Template
+from cgi import FieldStorage, print_environ
+from Cookie import SimpleCookie
 from datetime import datetime
-import cgi
+from string import Template
+from xmlrpclib import ServerProxy, Error
 import os
 import pprint
 
 
-
 XMLRPC_URL = 'http://localhost:8020/xmlrpc'
 PLATFORM_NAMES = {
     'debian': 'Debian GNU/Linux 5.0.3 (x86_64)',
@@ -515,19 +515,59 @@ def parse_url():
         return ('', '')
 
 #----------------------------------------------------------------------
+def get_layout(fs, env, headers):
+    """
+    Try to read the desired layout (normal or liquid) from the query string (?layout=normal).
+    If found, set the passed layout as Cookie to store it permanently.
+    Otherwise try to read the value from a Cookie.
+    Finally, use 'normal' as default.
+
+    @param fs (cgi.FieldStorage)
+    @param env (dict)
+    @param headers (list)
+    @return layout (str)
+    """
+    # default
+    layout = 'normal'
+    # read/set layout cookie
+    if fs.has_key('layout') :
+        layout = fs['layout'].value
+        layout_cookie = SimpleCookie()
+        layout_cookie['layout'] = layout
+        # 4 weeks
+        layout_cookie['layout']['max-age'] = 2592000
+        layout_cookie['layout']['path'] = '/'
+        headers.append(layout_cookie)
+    else:
+        try:
+            layout_cookie = SimpleCookie()
+            layout_cookie.load(os.environ['HTTP_COOKIE'])
+            layout = layout_cookie['layout'].value
+        except KeyError:
+            pass
+
+    # prevent malicious user input by manipulated cookies
+    if layout != 'liquid':
+        layout = 'normal'
+
+    return layout
+
+#----------------------------------------------------------------------
 def main():
+    headers = []
+
     # parse query string
-    fs = cgi.FieldStorage(keep_blank_values=True)
-    layout = fs['layout'].value if fs.has_key('layout') else 'normal'
+    fs = FieldStorage(keep_blank_values=True)
     debug = fs.has_key('debug')
     if debug:
         import cgitb
         cgitb.enable(display=1)
+    layout = get_layout(fs, os.environ, headers)
 
-    # handle arguments
+    # parse arguments
     url, args = parse_url()
 
-    headers = []
+    # handle arguments, do the action, generate HTML
     html = HtmlGen(url, layout)
     if args:
         args_len = len(args)
@@ -550,7 +590,7 @@ def main():
     print str(html)
     if debug:
         print debug_out
-        print cgi.print_environ()
+        print print_environ()
 
 main()
 



More information about the Xfce4-commits mailing list