incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [2/3] git commit: [#5007] don't run private_project() more than necessary
Date Fri, 28 Sep 2012 13:47:39 GMT
[#5007] don't run private_project() more than necessary


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/9667df06
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/9667df06
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/9667df06

Branch: refs/heads/master
Commit: 9667df0647564c545b94df746235418e0d656cd5
Parents: 7309b4b
Author: Dave Brondsema <dbrondsema@geek.net>
Authored: Thu Sep 27 19:10:32 2012 +0000
Committer: Cory Johns <johnsca@geek.net>
Committed: Fri Sep 28 13:47:14 2012 +0000

----------------------------------------------------------------------
 Allura/allura/lib/decorators.py |   27 +++++++++++++++++++++++++++
 Allura/allura/model/auth.py     |    6 +++++-
 2 files changed, 32 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9667df06/Allura/allura/lib/decorators.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/decorators.py b/Allura/allura/lib/decorators.py
index e710ee2..bd26b1e 100644
--- a/Allura/allura/lib/decorators.py
+++ b/Allura/allura/lib/decorators.py
@@ -3,6 +3,8 @@ import json
 import logging
 from collections import defaultdict
 from urllib import unquote
+
+from decorator import decorator
 from tg.decorators import before_validate
 from tg import request, redirect
 
@@ -169,3 +171,28 @@ def Property(function):
     sys.settrace(probeFunc)
     function()
     return property(**func_locals)
+
+
+def getattr_(obj, name, default_thunk):
+    "Similar to .setdefault in dictionaries."
+    try:
+        return getattr(obj, name)
+    except AttributeError:
+        default = default_thunk()
+        setattr(obj, name, default)
+        return default
+
+
+@decorator
+def memoize(func, *args):
+    """
+    Cache the method's result, for the given args
+    """
+    dic = getattr_(func, "memoize_dic", dict)
+    # memoize_dic is created at the first call
+    if args in dic:
+        return dic[args]
+    else:
+        result = func(*args)
+        dic[args] = result
+        return result
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9667df06/Allura/allura/model/auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/auth.py b/Allura/allura/model/auth.py
index 737877c..c12ff9e 100644
--- a/Allura/allura/model/auth.py
+++ b/Allura/allura/model/auth.py
@@ -24,6 +24,7 @@ from ming.orm.declarative import MappedClass
 import allura.tasks.mail_tasks
 from allura.lib import helpers as h
 from allura.lib import plugin
+from allura.lib.decorators import memoize
 
 from .session import main_orm_session, main_doc_session
 from .session import project_orm_session
@@ -272,6 +273,7 @@ class AuthGlobals(MappedClass):
             new=True)
         return g.next_uid
 
+
 class User(MappedClass, ActivityNode, ActivityObject):
     SALT_LEN=8
     class __mongometa__:
@@ -309,9 +311,11 @@ class User(MappedClass, ActivityNode, ActivityObject):
     def url(self):
         return plugin.AuthenticationProvider.get(request).project_url(self)
 
+    @memoize
     def icon_url(self):
         icon_url = None
-        if self.private_project() and self.private_project().icon:
+        private_project = self.private_project()
+        if private_project and private_project.icon:
             icon_url = self.url()+'user_icon'
         elif self.preferences.email_address:
             icon_url = g.gravatar(self.preferences.email_address)


Mime
View raw message