]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/modules/ui_objects.py
Configure logging "the Django way" and ensure that all messages are written to the...
[osqa.git] / forum / modules / ui_objects.py
index 7fcf96caea9ccb155a4854492279f7c5ff452937..a51044ad527f3a9fe661880258c2060e389812dc 100644 (file)
@@ -2,11 +2,12 @@ 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
 
 class Visibility(object):
-    def __init__(self, level='public'):
+    def __init__(self, level='public', negated=False):
         if level not in ['public', 'authenticated', 'staff', 'superuser', 'owner']:
             try:
                 int(level)
@@ -17,7 +18,7 @@ class Visibility(object):
             self.by_reputation = False
 
         self.level = level
-        self.negated = False
+        self.negated = negated
 
     def show_to(self, user):
         if self.by_reputation:
@@ -35,8 +36,7 @@ class Visibility(object):
             return res
 
     def __invert__(self):
-        inverted = copy(self)
-        inverted.negated = True
+        return Visibility(self.level, not self.negated)
         
 
 Visibility.PUBLIC = Visibility('public')
@@ -62,14 +62,15 @@ 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
 
-    def __init__(self, visibility=None, weight=500):
+    def __init__(self, visibility=None, weight=500, name=''):
         self.visibility = visibility
         self.weight = weight
+        self.name = name
 
     def _visible_to(self, user):
         return (not self.visibility) or (self.visibility and self.visibility.show_to(user))
@@ -78,7 +79,10 @@ class ObjectBase(object):
         try:
             return self._visible_to(context['request'].user)
         except KeyError:
-            return True
+            try:
+                return self._visible_to(context['viewer'])
+            except KeyError:
+                return self._visible_to(AnonymousUser())
 
     def render(self, context):
         return ''
@@ -90,8 +94,8 @@ class LoopBase(ObjectBase):
 
 
 class Link(ObjectBase):
-    def __init__(self, text, url, attrs=None, pre_code='', post_code='', visibility=None, weight=500):
-        super(Link, self).__init__(visibility, weight)
+    def __init__(self, text, url, attrs=None, pre_code='', post_code='', visibility=None, weight=500, name=''):
+        super(Link, self).__init__(visibility, weight, name)
         self.text = self.Argument(text)
         self.url = self.Argument(url)
         self.attrs = self.Argument(attrs or {})
@@ -104,8 +108,8 @@ class Link(ObjectBase):
             self.post_code(context))
 
 class Include(ObjectBase):
-    def __init__(self, tpl, visibility=None, weight=500):
-        super(Include, self).__init__(visibility, weight)
+    def __init__(self, tpl, visibility=None, weight=500, name=''):
+        super(Include, self).__init__(visibility, weight, name)
         self.template = template.loader.get_template(tpl)
 
     def render(self, context):
@@ -115,8 +119,8 @@ class Include(ObjectBase):
         
 
 class LoopContext(LoopBase):
-    def __init__(self, loop_context, visibility=None, weight=500):
-        super(LoopContext, self).__init__(visibility, weight)
+    def __init__(self, loop_context, visibility=None, weight=500, name=''):
+        super(LoopContext, self).__init__(visibility, weight, name)
         self.loop_context = self.Argument(loop_context)
 
     def update_context(self, context):
@@ -124,8 +128,8 @@ class LoopContext(LoopBase):
 
 
 class PageTab(LoopBase):
-    def __init__(self, tab_name, tab_title, url_getter, weight):
-        super(PageTab, self).__init__(weight=weight)
+    def __init__(self, tab_name, tab_title, url_getter, weight, name=''):
+        super(PageTab, self).__init__(weight=weight, name=name)
         self.tab_name = tab_name
         self.tab_title = tab_title
         self.url_getter = url_getter
@@ -140,7 +144,7 @@ class PageTab(LoopBase):
 
 class ProfileTab(LoopBase):
     def __init__(self, name, title, description, url_getter, private=False, render_to=None, weight=500):
-        super(ProfileTab, self).__init__(weight=weight)
+        super(ProfileTab, self).__init__(weight=weight, name=name)
         self.name = name
         self.title = title
         self.description = description
@@ -163,8 +167,8 @@ class ProfileTab(LoopBase):
 
 
 class AjaxMenuItem(ObjectBase):
-    def __init__(self, label, url, a_attrs=None, span_label='', span_attrs=None, visibility=None, weight=500):
-        super(AjaxMenuItem, self).__init__(visibility, weight)
+    def __init__(self, label, url, a_attrs=None, span_label='', span_attrs=None, visibility=None, weight=500, name=''):
+        super(AjaxMenuItem, self).__init__(visibility, weight, name)
         self.label = self.Argument(label)
         self.url = self.Argument(url)
         self.a_attrs = self.Argument(a_attrs or {})
@@ -178,8 +182,8 @@ class AjaxMenuItem(ObjectBase):
             **{'class': 'item'})
 
 class AjaxMenuGroup(ObjectBase, Registry):
-    def __init__(self, label, items, visibility=None, weight=500):
-        super(AjaxMenuGroup, self).__init__(visibility, weight)
+    def __init__(self, label, items, visibility=None, weight=500, name=''):
+        super(AjaxMenuGroup, self).__init__(visibility, weight, name)
         self.label = label
 
         for item in items: