diff rhodecode/lib/helpers.py @ 734:49eb69d78988 beta

implemented user dashboards, and following system.
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 22 Nov 2010 03:57:47 +0100
parents 7df3855bf6c7
children dbec976d9975
line wrap: on
line diff
--- a/rhodecode/lib/helpers.py	Mon Nov 22 03:51:28 2010 +0100
+++ b/rhodecode/lib/helpers.py	Mon Nov 22 03:57:47 2010 +0100
@@ -3,6 +3,8 @@
 Consists of functions to typically be used within templates, but also
 available to Controllers. This module is available to both as 'h'.
 """
+import random
+import hashlib
 from pygments.formatters import HtmlFormatter
 from pygments import highlight as code_highlight
 from pylons import url, app_globals as g
@@ -36,6 +38,24 @@
 
 reset = _reset
 
+
+def get_token():
+    """Return the current authentication token, creating one if one doesn't
+    already exist.
+    """
+    token_key = "_authentication_token"
+    from pylons import session
+    if not token_key in session:
+        try:
+            token = hashlib.sha1(str(random.getrandbits(128))).hexdigest()
+        except AttributeError: # Python < 2.4
+            token = hashlib.sha1(str(random.randrange(2 ** 128))).hexdigest()
+        session[token_key] = token
+        if hasattr(session, 'save'):
+            session.save()
+    return session[token_key]
+
+
 #Custom helpers here :)
 class _Link(object):
     '''
@@ -415,10 +435,10 @@
                 cs_links += html_tmpl % (', '.join(r for r in revs[revs_limit:]),
                                          _('and %s more revisions') \
                                             % (len(revs) - revs_limit))
-                
+
             return literal(cs_links)
         return ''
-    
+
     def get_fork_name():
         if action == 'user_forked_repo':
             from rhodecode.model.scm import ScmModel