1 from django.core.urlresolvers import reverse
2 from forum.utils import html
4 class UiObjectUserLevelBase(object):
5 def show_to(self, user):
8 class SuperuserUiObject(UiObjectUserLevelBase):
9 def show_to(self, user):
10 return user.is_superuser
12 class StaffUiObject(UiObjectUserLevelBase):
13 def show_to(self, user):
14 return user.is_staff or user.is_superuser
16 class ReputedUserUiObject(UiObjectUserLevelBase):
17 def __init__(self, min_rep):
18 self.min_rep = min_rep
20 def show_to(self, user):
21 return user.is_authenticated() and user.reputation >= int(self.min_rep)
23 class LoggedInUserUiObject(UiObjectUserLevelBase):
24 def show_to(self, user):
25 return user.is_authenticated()
27 class PublicUiObject(UiObjectUserLevelBase):
32 class UiObjectArgument(object):
33 def __init__(self, argument):
34 self.argument = argument
36 def __call__(self, context):
37 if callable(self.argument):
38 return self.argument(context)
43 class UiObjectBase(object):
44 def __init__(self, user_level=None, weight=500):
45 self.user_level = user_level or PublicUiObject()
48 def can_render(self, context):
49 return self.user_level.show_to(context['request'].user)
51 def render(self, context):
54 class UiLoopObjectBase(UiObjectBase):
55 def update_context(self, context):
60 class UiLinkObject(UiObjectBase):
61 def __init__(self, text, url, attrs=None, pre_code='', post_code='', user_level=None, weight=500):
62 super(UiLinkObject, self).__init__(user_level, weight)
63 self.text = UiObjectArgument(text)
64 self.url = UiObjectArgument(url)
65 self.attrs = UiObjectArgument(attrs or {})
66 self.pre_code = UiObjectArgument(pre_code)
67 self.post_code = UiObjectArgument(post_code)
69 def render(self, context):
70 return "%s %s %s" % (self.pre_code(context),
71 html.hyperlink(self.url(context), self.text(context), **self.attrs(context)),
72 self.post_code(context))
75 class UiLoopContextObject(UiLoopObjectBase):
76 def __init__(self, loop_context, user_level=None, weight=500):
77 super(UiLoopContextObject, self).__init__(user_level, weight)
78 self.loop_context = UiObjectArgument(loop_context)
80 def update_context(self, context):
81 context.update(self.loop_context(context))
84 class UiTopPageTabObject(UiLoopObjectBase):
85 def __init__(self, tab_name, tab_title, url_pattern, weight):
86 super(UiTopPageTabObject, self).__init__(weight=weight)
87 self.tab_name = tab_name
88 self.tab_title = tab_title
89 self.url_pattern = url_pattern
91 def update_context(self, context):
93 tab_name=self.tab_name,
94 tab_title=self.tab_title,
95 tab_url=reverse(self.url_pattern)