changeset 2180:ec2029ab6585

change subscriptions funcs definitions
author Ana Balica <ana.balica@gmail.com>
date Tue, 03 Sep 2013 22:59:38 +0300
parents 4491afa24f1e
children 0cfaeae59452
files MoinMoin/user.py MoinMoin/util/_tests/test_subscriptions.py MoinMoin/util/subscriptions.py
diffstat 3 files changed, 33 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/user.py	Fri Aug 30 00:14:00 2013 +0300
+++ b/MoinMoin/user.py	Tue Sep 03 22:59:38 2013 +0300
@@ -590,7 +590,7 @@
         if subscriptions & set(self.subscriptions):
             return True
 
-        if get_matched_subscription_patterns(item, self.subscriptions):
+        if get_matched_subscription_patterns(self.subscriptions, **meta):
             return True
         return False
 
--- a/MoinMoin/util/_tests/test_subscriptions.py	Fri Aug 30 00:14:00 2013 +0300
+++ b/MoinMoin/util/_tests/test_subscriptions.py	Tue Sep 03 22:59:38 2013 +0300
@@ -28,7 +28,7 @@
         self.item = Item.create(self.item_name)
 
     def test_get_subscribers(self):
-        users = get_subscribers(self.item)
+        users = get_subscribers(**self.item.meta)
         assert users == set()
 
         name1 = u'baz'
@@ -44,7 +44,7 @@
         user2 = user.User(name=name2, password=password)
         user.create_user(username=name3, password=password, email=email3, locale=u"en")
         user3 = user.User(name=name3, password=password, email1=email3)
-        subscribers = get_subscribers(self.item)
+        subscribers = get_subscribers(**self.item.meta)
         assert subscribers == set()
 
         namere = r'.*'
@@ -62,27 +62,27 @@
             for user_ in users:
                 user_.profile._meta[SUBSCRIPTIONS] = subscriptions
                 user_.save(force=True)
-            subscribers = get_subscribers(self.item)
+            subscribers = get_subscribers(**self.item.meta)
             subscribers_names = {subscriber.name for subscriber in subscribers}
             assert subscribers_names == expected_names
 
     def test_get_matched_subscription_patterns(self):
-        patterns = get_matched_subscription_patterns(self.item, [])
+        meta = self.item.meta
+        patterns = get_matched_subscription_patterns([], **meta)
         assert patterns == []
         non_matching_patterns = [
             "{0}:{1}:{2}".format(NAMERE, "userprofile", ".*"),
             "{0}:{1}:{2}".format(NAMERE, self.namespace, "\d+"),
             "{0}:{1}:{2}".format(NAMEPREFIX, self.namespace, "bar"),
         ]
-        patterns = get_matched_subscription_patterns(self.item, non_matching_patterns)
+        patterns = get_matched_subscription_patterns(non_matching_patterns, **meta)
         assert patterns == []
 
         matching_patterns = [
             "{0}:{1}:{2}".format(NAMERE, self.namespace, "fo+"),
             "{0}:{1}:{2}".format(NAMEPREFIX, self.namespace, "fo"),
         ]
-        patterns = get_matched_subscription_patterns(self.item,
-                                                     non_matching_patterns + matching_patterns)
+        patterns = get_matched_subscription_patterns(non_matching_patterns + matching_patterns, **meta)
         assert patterns == matching_patterns
 
     def test_perf_get_subscribers(self):
@@ -118,7 +118,7 @@
                     user_.profile._meta[SUBSCRIPTIONS] = [subscription]
                     user_.save(force=True)
                 t = time.time()
-                subscribers = get_subscribers(self.item)
+                subscribers = get_subscribers(**self.item.meta)
                 elapsed_time = time.time() - t
                 print "{0}: {1} s".format(subscription.split(':', 1)[0], elapsed_time)
                 subscribers_names = {subscriber.name for subscriber in subscribers}
--- a/MoinMoin/util/subscriptions.py	Fri Aug 30 00:14:00 2013 +0300
+++ b/MoinMoin/util/subscriptions.py	Tue Sep 03 22:59:38 2013 +0300
@@ -23,25 +23,32 @@
 Subscriber = namedtuple('Subscriber', [ITEMID, NAME, EMAIL, LOCALE])
 
 
-def get_subscribers(item):
+def get_subscribers(**meta):
     """ Get all users that are subscribed to the item
 
-    :param item: Item object
+    :param meta: key/value pairs from item metadata - itemid, name, namespace, tags keys
     :return: a set of Subscriber objects
     """
-    meta = item.meta
-    namespace = meta[NAMESPACE]
-    terms = [Term(SUBSCRIPTION_IDS, "{0}:{1}".format(ITEMID, meta[ITEMID])), ]
-    terms.extend(Term(SUBSCRIPTION_IDS, "{0}:{1}:{2}".format(NAME, namespace, name))
-                 for name in meta[NAME])
-    terms.extend(Term(SUBSCRIPTION_IDS, "{0}:{1}:{2}".format(TAGS, namespace, tag))
-                 for tag in meta[TAGS])
+    itemid = meta.get(ITEMID)
+    name = meta.get(NAME)
+    namespace = meta.get(NAMESPACE)
+    tags = meta.get(TAGS)
+    terms = []
+    if itemid is not None:
+        terms.extend([Term(SUBSCRIPTION_IDS, "{0}:{1}".format(ITEMID, itemid))])
+    if namespace is not None:
+        if name is not None:
+            terms.extend(Term(SUBSCRIPTION_IDS, "{0}:{1}:{2}".format(NAME, namespace, name_))
+                         for name_ in name)
+        if tags is not None:
+            terms.extend(Term(SUBSCRIPTION_IDS, "{0}:{1}:{2}".format(TAGS, namespace, tag))
+                         for tag in tags)
     query = Or(terms)
     # TODO: check ACL behaviour - is this avoiding the protection layer?
     with flaskg.storage.indexer.ix[LATEST_REVS].searcher() as searcher:
         result_iterators = [searcher.search(query, limit=None), ]
         subscription_patterns = searcher.lexicon(SUBSCRIPTION_PATTERNS)
-        patterns = get_matched_subscription_patterns(item, subscription_patterns)
+        patterns = get_matched_subscription_patterns(subscription_patterns, **meta)
         result_iterators.extend(searcher.documents(subscription_patterns=pattern) for pattern in patterns)
         subscribers = set()
         for user in chain.from_iterable(result_iterators):
@@ -52,24 +59,24 @@
     return subscribers
 
 
-def get_matched_subscription_patterns(item, subscription_patterns):
+def get_matched_subscription_patterns(subscription_patterns, **meta):
     """ Get all the subscriptions with patterns that match at least one of item names
 
-    :param item: Item object
     :param subscription_patterns: a list of subscription patterns (the ones that
                                     start with NAMERE or NAMEPREFIX)
+    :param meta: key/value pairs from item metadata - name and namespace keys
     :return: a list of matched subscription patterns
     """
-    meta = item.meta
-    item_namespace = meta[NAMESPACE]
+    item_names = meta.get(NAME)
+    item_namespace = meta.get(NAMESPACE)
     matched_subscriptions = []
     for subscription in subscription_patterns:
         keyword, value = subscription.split(":", 1)
-        if keyword in (NAMEPREFIX, NAMERE, ):
+        if keyword in (NAMEPREFIX, NAMERE, ) and item_namespace is not None and item_names:
             namespace, pattern = value.split(":", 1)
             if item_namespace == namespace:
                 if keyword == NAMEPREFIX:
-                    if any(name.startswith(pattern) for name in meta[NAME]):
+                    if any(name.startswith(pattern) for name in item_names):
                         matched_subscriptions.append(subscription)
                 elif keyword == NAMERE:
                     try:
@@ -77,6 +84,6 @@
                     except re.error:
                         logging.error("Subscription pattern '{0}' has failed compilation.".format(pattern))
                         continue
-                    if any(pattern.search(name) for name in meta[NAME]):
+                    if any(pattern.search(name) for name in item_names):
                         matched_subscriptions.append(subscription)
     return matched_subscriptions