diff rhodecode/lib/utils2.py @ 2845:6b176c679896 beta

failsafe the GET `page` argument
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 17 Sep 2012 22:17:25 +0200
parents c61c2ccea2b4
children ccbdff90e5a0
line wrap: on
line diff
--- a/rhodecode/lib/utils2.py	Mon Sep 17 21:26:32 2012 +0200
+++ b/rhodecode/lib/utils2.py	Mon Sep 17 22:17:25 2012 +0200
@@ -147,6 +147,23 @@
     return hashlib.sha1(username + salt).hexdigest()
 
 
+def safe_int(val, default=None):
+    """
+    Returns int() of val if val is not convertable to int use default
+    instead
+
+    :param val:
+    :param default:
+    """
+
+    try:
+        val = int(val)
+    except ValueError:
+        val = default
+
+    return val
+
+
 def safe_unicode(str_, from_encoding=None):
     """
     safe unicode function. Does few trick to turn str_ into unicode