]> git.openstreetmap.org Git - osqa.git/commitdiff
Makes some more fixes and tweaks on the module system.
authorhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Thu, 8 Jul 2010 08:04:32 +0000 (08:04 +0000)
committerhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Thu, 8 Jul 2010 08:04:32 +0000 (08:04 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@499 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/modules/decorators.py
forum/modules/ui_objects.py
forum/views/decorators.py
forum/views/readers.py
forum/views/users.py

index c7c564030adafdb8263ca922e6eb85144625cda4..13bddac89357785c8df24e9402483de3b5724e02 100644 (file)
@@ -5,17 +5,26 @@ class DecoratableObject(object):
     MODE_PARAMS = 1\r
     MODE_RESULT = 2\r
 \r
-    def __init__(self, fn):\r
-        a = inspect.getargspec(fn)\r
+    def __init__(self, fn, is_method=False):\r
         self._callable = fn\r
+        self.is_method = is_method\r
+\r
         self._params_decoration = None\r
         self._result_decoration = None\r
 \r
-    def _decorate(self, fn, needs_origin, method=False):\r
+    def _decorate(self, fn, mode, **kwargs):\r
+        if mode == self.MODE_OVERRIDE:\r
+            self._decorate_full(fn, **kwargs)\r
+        elif mode == self.MODE_PARAMS:\r
+            self._decorate_params(fn)\r
+        elif mode == self.MODE_RESULT:\r
+            self._decorate_result(fn, **kwargs)\r
+\r
+    def _decorate_full(self, fn, needs_origin=True):\r
         origin = self._callable\r
 \r
         if needs_origin:\r
-            if method:\r
+            if self.is_method:\r
                 self._callable = lambda inst, *args, **kwargs: fn(inst, origin, *args, **kwargs)\r
             else:\r
                 self._callable = lambda *args, **kwargs: fn(origin, *args, **kwargs)\r
@@ -28,10 +37,11 @@ class DecoratableObject(object):
 \r
         self._params_decoration.append(fn)\r
 \r
-    def _decorate_result(self, fn):\r
+    def _decorate_result(self, fn, needs_params=False):\r
         if not self._result_decoration:\r
             self._result_decoration = []\r
 \r
+        fn._needs_params = needs_params\r
         self._result_decoration.append(fn)\r
 \r
     def __call__(self, *args, **kwargs):\r
@@ -43,86 +53,96 @@ class DecoratableObject(object):
 \r
         if self._result_decoration:\r
             for dec in self._result_decoration:\r
-                res = dec(res)\r
+                if dec._needs_params:\r
+                    res = dec(res, *args, **kwargs)\r
+                else:\r
+                    res = dec(res)\r
 \r
         return res\r
 \r
+def _check_decoratable(origin, install=True):\r
+    if not isinstance(origin, DecoratableObject):\r
+        if inspect.ismethod(origin) and not hasattr(origin, '_decoratable_obj'):\r
+            decoratable = DecoratableObject(origin)\r
 \r
-def _create_decorator(origin, needs_origin, mode, method=False):\r
-    def decorator(fn):\r
-        if mode == DecoratableObject.MODE_OVERRIDE:\r
-            origin._decorate(fn, needs_origin, method=method)\r
-        elif mode == DecoratableObject.MODE_PARAMS:\r
-            origin._decorate_params(fn)\r
-        elif mode == DecoratableObject.MODE_RESULT:\r
-            origin._decorate_result(fn)\r
-\r
-        return fn\r
+            def decoratable_method(self, *args, **kwargs):\r
+                return decoratable(self, *args, **kwargs)\r
 \r
-    return decorator\r
+            decoratable_method._decoratable_obj = decoratable\r
 \r
+            def decoratable_decorate(fn, mode, **kwargs):\r
+                decoratable._decorate(fn, mode, **kwargs)\r
 \r
-def _decorate_method(origin, needs_origin, mode):\r
-    if not hasattr(origin, '_decoratable_obj'):\r
-        name = origin.__name__\r
-        cls = origin.im_class\r
+            decoratable_method._decorate = decoratable_decorate\r
 \r
-        decoratable = DecoratableObject(origin)\r
+            if install:\r
+                setattr(origin.im_class, origin.__name__, decoratable_method)\r
 \r
-        def decoratable_method(self, *args, **kwargs):\r
-            return decoratable(self, *args, **kwargs)\r
+            return decoratable_method\r
+                \r
+        elif inspect.isfunction(origin):\r
+            decoratable = DecoratableObject(origin)\r
 \r
-        decoratable_method._decoratable_obj = decoratable\r
-        setattr(cls, name, decoratable_method)\r
-    else:\r
-        decoratable = origin._decoratable_obj\r
+            if install:\r
+                setattr(inspect.getmodule(origin), origin.__name__, decoratable)\r
 \r
-    return _create_decorator(decoratable, needs_origin, mode, method=True)\r
+            return decoratable\r
 \r
-def _decorate_function(origin, needs_origin, mode):\r
-    if not isinstance(origin, DecoratableObject):\r
-        mod = inspect.getmodule(origin)\r
+    return origin\r
 \r
-        name = origin.__name__\r
-        origin = DecoratableObject(origin)\r
-        setattr(mod, name, origin)\r
 \r
-    return _create_decorator(origin, needs_origin, mode)\r
+def decorate(origin, needs_origin=True):\r
+    origin = _check_decoratable(origin)\r
 \r
+    def decorator(fn):\r
+        origin._decorate(fn, DecoratableObject.MODE_OVERRIDE, needs_origin=needs_origin)\r
+        \r
+    return decorator\r
 \r
-def decorate(origin, needs_origin=True, mode=DecoratableObject.MODE_OVERRIDE):\r
-    if inspect.ismethod(origin):\r
-        return _decorate_method(origin, needs_origin, mode)\r
 \r
-    if inspect.isfunction(origin) or isinstance(origin, DecoratableObject):\r
-        return _decorate_function(origin, needs_origin, mode)\r
+def _decorate_params(origin):\r
+    origin = _check_decoratable(origin)\r
 \r
     def decorator(fn):\r
-        return fn\r
+        origin._decorate(fn, DecoratableObject.MODE_PARAMS)\r
 \r
     return decorator\r
 \r
+decorate.params = _decorate_params\r
 \r
-def _decorate_params(origin):\r
-    return decorate(origin, mode=DecoratableObject.MODE_PARAMS)\r
+def _decorate_result(origin, needs_params=False):\r
+    origin = _check_decoratable(origin)\r
 \r
-decorate.params = _decorate_params\r
+    def decorator(fn):\r
+        origin._decorate(fn, DecoratableObject.MODE_RESULT, needs_params=needs_params)\r
 \r
-def _decorate_result(origin):\r
-    return decorate(origin, mode=DecoratableObject.MODE_RESULT)\r
+    return decorator\r
 \r
 decorate.result = _decorate_result\r
 \r
 def _decorate_with(fn):\r
     def decorator(origin):\r
-        if not isinstance(origin, DecoratableObject):\r
-            decoratable = DecoratableObject(origin)\r
-        else:\r
-            decoratable = origin\r
+        origin = _check_decoratable(origin)\r
+        origin._decorate(fn, DecoratableObject.MODE_OVERRIDE, needs_origin=True)\r
+        return origin\r
+    return decorator\r
+\r
+decorate.withfn = _decorate_with\r
 \r
-        decoratable._decorate(fn, True, False)\r
-        return decoratable\r
+def _decorate_result_with(fn, needs_params=False):\r
+    def decorator(origin):\r
+        origin = _check_decoratable(origin)\r
+        origin._decorate(fn, DecoratableObject.MODE_RESULT, needs_params=needs_params)\r
+        return origin\r
     return decorator\r
 \r
+decorate.result.withfn = _decorate_result_with\r
+\r
+def _decorate_params_with(fn):\r
+    def decorator(origin):\r
+        origin = _check_decoratable(origin)\r
+        origin._decorate(fn, DecoratableObject.MODE_PARAMS)\r
+        return origin\r
+    return decorator\r
 \r
-decorate.withfn = _decorate_with
\ No newline at end of file
+decorate.params.withfn = _decorate_params_with
\ No newline at end of file
index e7f626fb417797115d9ee4f4ace5681e2a066d40..e42c94017ce6545cdc70ffbf8187607a71652a98 100644 (file)
@@ -2,6 +2,7 @@ from django.core.urlresolvers import reverse
 from django.template.defaultfilters import slugify
 from django import template
 from forum.utils import html
+from forum.models.user import AnonymousUser
 from ui import Registry
 from copy import copy
 
@@ -62,7 +63,7 @@ class ObjectBase(object):
 
         def __call__(self, context):
             if callable(self.argument):
-                user = context.get('request', None) and context['request'].user or None
+                user = context.get('request', None) and context['request'].user or AnonymousUser()
                 return self.argument(user, context)
             else:
                 return self.argument
@@ -81,10 +82,7 @@ class ObjectBase(object):
             try:
                 return self._visible_to(context['viewer'])
             except KeyError:
-                if self.visibility:
-                    return False
-                else:
-                    return True
+                return self._visible_to(AnonymousUser())
 
     def render(self, context):
         return ''
index 2219f98174a36b4721c46f2676ac33b3145c51a0..758b6876294907998de3fec82cee22fabfef30b0 100644 (file)
@@ -9,10 +9,8 @@ from forum.modules import ui, decorate
 import logging\r
 \r
 def render(template=None, tab=None, tab_title='', weight=500, tabbed=True):\r
-    def decorator(func):\r
-        def decorated(func, request, *args, **kwargs):\r
-            context = func(request, *args, **kwargs)\r
-\r
+    def decorator(func):        \r
+        def decorated(context, request, *args, **kwargs):\r
             if isinstance(context, HttpResponse):\r
                 return context\r
 \r
@@ -22,11 +20,11 @@ def render(template=None, tab=None, tab_title='', weight=500, tabbed=True):
             return render_to_response(context.pop('template', template), context,\r
                                       context_instance=RequestContext(request))\r
 \r
-        if tabbed and tab:\r
+        if tabbed and tab and tab_title:\r
             ui.register(ui.PAGE_TOP_TABS,\r
                         ui.PageTab(tab, tab_title, lambda: reverse(func.__name__), weight=weight))\r
             \r
-        return decorate.withfn(decorated)(func)\r
+        return decorate.result.withfn(decorated, needs_params=True)(func)\r
 \r
     return decorator\r
 \r
index 999d320c86cc9d60db6d67173ca639441a11888e..8ed1b27365bf814fd51bc18b8d48ed31eacea3c6 100644 (file)
@@ -279,7 +279,7 @@ def answer_redirect(request, answer):
     return HttpResponsePermanentRedirect("%s?%s=%s#%s" % (
         answer.question.get_absolute_url(), _('page'), page, answer.id))
 
-@decorators.render("question.html", 'questions', tabbed=False)
+@decorators.render("question.html", 'questions')
 def question(request, id, slug, answer=None):
     try:
         question = Question.objects.get(id=id)
index 9cac193ff0625591602fe083d65d89c443a13fb5..fa575421d71acc9942ff6f751aae2e19c52569a8 100644 (file)
@@ -174,27 +174,32 @@ def suspend(request, id):
 \r
 def user_view(template, tab_name, tab_title, tab_description, private=False, tabbed=True, render_to=None, weight=500):\r
     def decorator(fn):\r
-        def decorated(fn, request, id, slug=None):\r
+        def params(request, id, slug=None):\r
             user = get_object_or_404(User, id=id)\r
             if private and not (user == request.user or request.user.is_superuser):\r
                 return HttpResponseUnauthorized(request)\r
 \r
             if render_to and (not render_to(user)):\r
                 return HttpResponseRedirect(user.get_profile_url())\r
-                \r
-            context = fn(request, user)\r
 \r
+            return [request, user], {}\r
+\r
+        decorated = decorate.params.withfn(params)(fn)\r
+\r
+        def result(context, request, user):\r
             rev_page_title = user.username + " - " + tab_description\r
 \r
             context.update({\r
-            "tab": "users",\r
-            "active_tab" : tab_name,\r
-            "tab_description" : tab_description,\r
-            "page_title" : rev_page_title,\r
-            "can_view_private": (user == request.user) or request.user.is_superuser\r
+                "tab": "users",\r
+                "active_tab" : tab_name,\r
+                "tab_description" : tab_description,\r
+                "page_title" : rev_page_title,\r
+                "can_view_private": (user == request.user) or request.user.is_superuser\r
             })\r
             return render_to_response(template, context, context_instance=RequestContext(request))\r
 \r
+        decorated = decorate.result.withfn(result, needs_params=True)(decorated)\r
+\r
         if tabbed:\r
             def url_getter(vu):\r
                 try:\r
@@ -206,7 +211,7 @@ def user_view(template, tab_name, tab_title, tab_description, private=False, tab
                 tab_name, tab_title, tab_description,url_getter, private, render_to, weight\r
             ))\r
 \r
-        return decorate.withfn(decorated)(fn)\r
+        return decorated\r
     return decorator\r
 \r
 \r