Added a checkHasAny method
authorSverre Rabbelier <srabbelier@gmail.com>
Tue, 24 Feb 2009 19:51:40 +0000
changeset 1486 c417a4188e73
parent 1485 430df988d395
child 1487 d18c71f4fabe
Added a checkHasAny method A simple 'OR' for checkers. Patch by: Sverre Rabbelier
app/soc/views/helper/access.py
--- a/app/soc/views/helper/access.py	Tue Feb 24 19:49:27 2009 +0000
+++ b/app/soc/views/helper/access.py	Tue Feb 24 19:51:40 2009 +0000
@@ -426,6 +426,29 @@
     if not self.hasMembership(roles, django_args):
       raise out_of_band.AccessViolation(message_fmt)
 
+  def checkHasAny(self, django_args, checks):
+    """Checks if any of the checks passes.
+
+    If none of the specified checks passes, the exception that the first of the
+    checks raised is reraised.
+    """
+
+    first = None
+
+    for checker_name, args in checks:
+      try:
+        self.doCheck(checker_name, django_args, args)
+        break
+      except out_of_band.Error, e:
+        # store the first esception
+        first = first if first else e
+    else:
+      # one check passed, all is well
+      return
+
+    # none passed, re-raise the first exception
+    raise first
+
   def allow(self, django_args):
     """Never raises an alternate HTTP response.  (an access no-op, basically).