]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/modules/ui_objects.py
More polished PAI for module html injection and added a couple more places to inject...
[osqa.git] / forum / modules / ui_objects.py
index 01a61d38c7ad9b8ff2a48d1241d61d3787e9c2a9..70bee93a1b695d69aa9325a93ad1668a127c29e9 100644 (file)
@@ -1,70 +1,81 @@
 from django.core.urlresolvers import reverse
+from django.template.defaultfilters import slugify
 from forum.utils import html
 
-class UiObjectUserLevelBase(object):
-    def show_to(self, user):
-        return True
-
-class SuperuserUiObject(UiObjectUserLevelBase):
-    def show_to(self, user):
-        return user.is_superuser
-
-class StaffUiObject(UiObjectUserLevelBase):
-    def show_to(self, user):
-        return user.is_staff or user.is_superuser
-
-class ReputedUserUiObject(UiObjectUserLevelBase):
-    def __init__(self, min_rep):
-        self.min_rep = min_rep
+class Visibility(object):
+    def __init__(self, level='public'):
+        if level not in ['public', 'authenticated', 'staff', 'superuser', 'owner']:
+            try:
+                int(level)
+                self.by_reputation = True
+            except:
+                raise "Invalid visibility level for ui object: %s" % level
+        else:
+            self.by_reputation = False
 
-    def show_to(self, user):
-        return user.is_authenticated() and user.reputation >= int(self.min_rep)
+        self.level = level
 
-class LoggedInUserUiObject(UiObjectUserLevelBase):
     def show_to(self, user):
-        return user.is_authenticated()
-
-class PublicUiObject(UiObjectUserLevelBase):
-    pass
-
+        if self.by_reputation:
+            return user.is_authenticated() and (user.reputation >= int(self.level) or user.is_staff or user.is_superuser)
+        else:
+            return self.level == 'public' or (user.is_authenticated() and (
+                self.level == 'authenticated' or (
+                self.level == 'superuser' and user.is_superuser) or (
+                self.level == 'staff' and (user.is_staff or user.is_superuser)) or (
+                self.level == 'owner' and user.is_siteowner)))
+
+Visibility.PUBLIC = Visibility('public')
+Visibility.AUTHENTICATED = Visibility('authenticated')
+Visibility.STAFF = Visibility('staff')
+Visibility.SUPERUSER = Visibility('superuser')
+Visibility.OWNER = Visibility('owner')
+Visibility.REPUTED = lambda r: Visibility(r)
+
+
+class Url(object):
+    def __init__(self, url_pattern):
+        self.url_pattern = url_pattern
 
+    def __call__(self, u, c):
+        return reverse(self.url_pattern)
 
-class UiObjectArgument(object):
-    def __init__(self, argument):
-        self.argument = argument
 
-    def __call__(self, context):
-        if callable(self.argument):
-            return self.argument(context)
-        else:
-            return self.argument
+class ObjectBase(object):
+    class Argument(object):
+        def __init__(self, argument):
+            self.argument = argument
 
+        def __call__(self, context):
+            if callable(self.argument):
+                return self.argument(context['request'].user, context)
+            else:
+                return self.argument
 
-class UiObjectBase(object):
-    def __init__(self, user_level=None, weight=500):
-        self.user_level = user_level or PublicUiObject()
+    def __init__(self, visibility=None, weight=500):
+        self.visibility = visibility
         self.weight = weight
 
     def can_render(self, context):
-        return self.user_level.show_to(context['request'].user)
+        return (not self.visibility) or (self.visibility and self.visibility.show_to(context['request'].user))
 
     def render(self, context):
         return ''
 
-class UiLoopObjectBase(UiObjectBase):
+class LoopBase(ObjectBase):
     def update_context(self, context):
         pass
 
 
 
-class UiLinkObject(UiObjectBase):
-    def __init__(self, text, url, attrs=None, pre_code='', post_code='', user_level=None, weight=500):
-        super(UiLinkObject, self).__init__(user_level, weight)
-        self.text = UiObjectArgument(text)
-        self.url = UiObjectArgument(url)
-        self.attrs = UiObjectArgument(attrs or {})
-        self.pre_code = UiObjectArgument(pre_code)
-        self.post_code = UiObjectArgument(post_code)
+class Link(ObjectBase):
+    def __init__(self, text, url, attrs=None, pre_code='', post_code='', visibility=None, weight=500):
+        super(Link, self).__init__(visibility, weight)
+        self.text = self.Argument(text)
+        self.url = self.Argument(url)
+        self.attrs = self.Argument(attrs or {})
+        self.pre_code = self.Argument(pre_code)
+        self.post_code = self.Argument(post_code)
 
     def render(self, context):
         return "%s %s %s" % (self.pre_code(context),
@@ -72,25 +83,47 @@ class UiLinkObject(UiObjectBase):
             self.post_code(context))
 
 
-class UiLoopContextObject(UiLoopObjectBase):
-    def __init__(self, loop_context, user_level=None, weight=500):
-        super(UiLoopContextObject, self).__init__(user_level, weight)
-        self.loop_context = UiObjectArgument(loop_context)
+class LoopContext(LoopBase):
+    def __init__(self, loop_context, visibility=None, weight=500):
+        super(LoopContext, self).__init__(visibility, weight)
+        self.loop_context = self.Argument(loop_context)
 
     def update_context(self, context):
         context.update(self.loop_context(context))
 
 
-class UiTopPageTabObject(UiLoopObjectBase):
-    def __init__(self, tab_name, tab_title, url_pattern, weight):
-        super(UiTopPageTabObject, self).__init__(weight=weight)
+class PageTab(LoopBase):
+    def __init__(self, tab_name, tab_title, url_getter, weight):
+        super(PageTab, self).__init__(weight=weight)
         self.tab_name = tab_name
         self.tab_title = tab_title
-        self.url_pattern = url_pattern
+        self.url_getter = url_getter
 
     def update_context(self, context):
         context.update(dict(
             tab_name=self.tab_name,
             tab_title=self.tab_title,
-            tab_url=reverse(self.url_pattern)
+            tab_url=self.url_getter()
         ))
+
+
+class ProfileTab(LoopBase):
+    def __init__(self, name, title, description, url_getter, private=False, weight=500):
+        super(ProfileTab, self).__init__(weight=weight)
+        self.name = name
+        self.title = title
+        self.description = description
+        self.url_getter = url_getter
+        self.private = private
+
+    def can_render(self, context):
+        return not self.private or (
+            context['view_user'] == context['request'].user or context['request'].user.is_superuser)
+
+    def update_context(self, context):        
+        context.update(dict(
+            tab_name=self.name,
+            tab_title=self.title,
+            tab_description = self.description,
+            tab_url=self.url_getter(context['view_user'])
+        ))
\ No newline at end of file