1 from django.core.urlresolvers import reverse
2 from django.template.defaultfilters import slugify
3 from django import template
4 from forum.utils import html
6 class Visibility(object):
7 def __init__(self, level='public'):
8 if level not in ['public', 'authenticated', 'staff', 'superuser', 'owner']:
11 self.by_reputation = True
13 raise "Invalid visibility level for ui object: %s" % level
15 self.by_reputation = False
19 def show_to(self, user):
20 if self.by_reputation:
21 return user.is_authenticated() and (user.reputation >= int(self.level) or user.is_staff or user.is_superuser)
23 return self.level == 'public' or (user.is_authenticated() and (
24 self.level == 'authenticated' or (
25 self.level == 'superuser' and user.is_superuser) or (
26 self.level == 'staff' and (user.is_staff or user.is_superuser)) or (
27 self.level == 'owner' and user.is_siteowner)))
29 Visibility.PUBLIC = Visibility('public')
30 Visibility.AUTHENTICATED = Visibility('authenticated')
31 Visibility.STAFF = Visibility('staff')
32 Visibility.SUPERUSER = Visibility('superuser')
33 Visibility.OWNER = Visibility('owner')
34 Visibility.REPUTED = lambda r: Visibility(r)
38 def __init__(self, url_pattern):
39 self.url_pattern = url_pattern
41 def __call__(self, u, c):
42 return reverse(self.url_pattern)
45 class ObjectBase(object):
46 class Argument(object):
47 def __init__(self, argument):
48 self.argument = argument
50 def __call__(self, context):
51 if callable(self.argument):
52 return self.argument(context['request'].user, context)
56 def __init__(self, visibility=None, weight=500):
57 self.visibility = visibility
60 def can_render(self, context):
61 return (not self.visibility) or (self.visibility and self.visibility.show_to(context['request'].user))
63 def render(self, context):
66 class LoopBase(ObjectBase):
67 def update_context(self, context):
72 class Link(ObjectBase):
73 def __init__(self, text, url, attrs=None, pre_code='', post_code='', visibility=None, weight=500):
74 super(Link, self).__init__(visibility, weight)
75 self.text = self.Argument(text)
76 self.url = self.Argument(url)
77 self.attrs = self.Argument(attrs or {})
78 self.pre_code = self.Argument(pre_code)
79 self.post_code = self.Argument(post_code)
81 def render(self, context):
82 return "%s %s %s" % (self.pre_code(context),
83 html.hyperlink(self.url(context), self.text(context), **self.attrs(context)),
84 self.post_code(context))
86 class Include(ObjectBase):
87 def __init__(self, tpl, visibility=None, weight=500):
88 super(Include, self).__init__(visibility, weight)
89 self.template = template.loader.get_template(tpl)
91 def render(self, context):
92 return self.template.render(context)
95 class LoopContext(LoopBase):
96 def __init__(self, loop_context, visibility=None, weight=500):
97 super(LoopContext, self).__init__(visibility, weight)
98 self.loop_context = self.Argument(loop_context)
100 def update_context(self, context):
101 context.update(self.loop_context(context))
104 class PageTab(LoopBase):
105 def __init__(self, tab_name, tab_title, url_getter, weight):
106 super(PageTab, self).__init__(weight=weight)
107 self.tab_name = tab_name
108 self.tab_title = tab_title
109 self.url_getter = url_getter
111 def update_context(self, context):
113 tab_name=self.tab_name,
114 tab_title=self.tab_title,
115 tab_url=self.url_getter()
119 class ProfileTab(LoopBase):
120 def __init__(self, name, title, description, url_getter, private=False, weight=500):
121 super(ProfileTab, self).__init__(weight=weight)
124 self.description = description
125 self.url_getter = url_getter
126 self.private = private
128 def can_render(self, context):
129 return not self.private or (
130 context['view_user'] == context['request'].user or context['request'].user.is_superuser)
132 def update_context(self, context):
135 tab_title=self.title,
136 tab_description = self.description,
137 tab_url=self.url_getter(context['view_user'])