changeset 4894:0ec0d096af42

Groups2009: GetVal macro was refactored. It returns an empty string if requested dictionary does not exist. ACL check is made only if dictionary is defined on a wikipage.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Fri, 24 Jul 2009 10:00:21 +0200
parents 802cc30f1937
children 3440ba2a89b2
files MoinMoin/datastruct/backends/__init__.py MoinMoin/datastruct/backends/_tests/__init__.py MoinMoin/macro/__init__.py
diffstat 3 files changed, 31 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/datastruct/backends/__init__.py	Wed Jul 22 23:31:05 2009 +0200
+++ b/MoinMoin/datastruct/backends/__init__.py	Fri Jul 24 10:00:21 2009 +0200
@@ -311,3 +311,9 @@
     def _retrieve_items(self, dict_name):
         raise NotImplementedError()
 
+    def get(self, key, default=None):
+        try:
+            return self[key]
+        except DictDoesNotExistError:
+            return default
+
--- a/MoinMoin/datastruct/backends/_tests/__init__.py	Wed Jul 22 23:31:05 2009 +0200
+++ b/MoinMoin/datastruct/backends/_tests/__init__.py	Fri Jul 24 10:00:21 2009 +0200
@@ -164,3 +164,18 @@
 
         assert u'SomeNotExistingDict' not in dicts
 
+    def test_get(self):
+        dicts = self.request.dicts
+
+        for dict_name in self.dicts:
+            assert dicts.get(dict_name)
+
+        assert u'SomeNotExistingDict' not in dicts
+        assert dicts.get(u'SomeNotExistingDict') is None
+
+        for dict_name, expected_dict in self.dicts.items():
+            test_dict = dicts[dict_name]
+            for key, value in expected_dict.items():
+                assert u'SomeNotExistingKey' not in test_dict
+                assert test_dict.get(u'SomeNotExistingKey') is None
+
--- a/MoinMoin/macro/__init__.py	Wed Jul 22 23:31:05 2009 +0200
+++ b/MoinMoin/macro/__init__.py	Fri Jul 24 10:00:21 2009 +0200
@@ -27,6 +27,7 @@
 from MoinMoin import action, config, util
 from MoinMoin import wikiutil, i18n
 from MoinMoin.Page import Page
+from MoinMoin.datastruct.backends.wiki_dicts import WikiDict
 
 
 names = ["TitleSearch", "WordIndex", "TitleIndex", "GoTo",
@@ -380,12 +381,18 @@
 
     def macro_GetVal(self, page=None, key=None):
         page = wikiutil.get_unicode(self.request, page, 'page')
-        if not self.request.user.may.read(page):
-            raise ValueError("You don't have enough rights on this page")
+
         key = wikiutil.get_unicode(self.request, key, 'key')
         if page is None or key is None:
             raise ValueError("You need to give: pagename, key")
-        d = self.request.dicts[page]
+
+        d = self.request.dicts.get(page, {})
+
+        # Check acl only if dictionary is defined on a wiki page.
+        if isinstance(d, WikiDict) and not self.request.user.may.read(page):
+            raise ValueError("You don't have enough rights on this page")
+
         result = d.get(key, '')
+
         return self.formatter.text(result)