You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2012/09/27 21:34:25 UTC
[2/3] git commit: [#5007] don't run private_project() more than
necessary
[#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/daf7a00d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/daf7a00d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/daf7a00d
Branch: refs/heads/db/5007
Commit: daf7a00da2572a744413ef70d620aa48912de1fd
Parents: 3a489c9
Author: Dave Brondsema <db...@geek.net>
Authored: Thu Sep 27 19:10:32 2012 +0000
Committer: Dave Brondsema <db...@geek.net>
Committed: Thu Sep 27 19:10:32 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/daf7a00d/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/daf7a00d/Allura/allura/model/auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/auth.py b/Allura/allura/model/auth.py
index af8975f..fca3859 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, default=None)