1 from django.core.urlresolvers import reverse
2 from django.template.defaultfilters import slugify
3 from forum.utils import html
5 class Visibility(object):
6 def __init__(self, level='public'):
7 if level not in ['public', 'authenticated', 'staff', 'superuser', 'owner']:
10 self.by_reputation = True
12 raise "Invalid visibility level for ui object: %s" % level
14 self.by_reputation = False
18 def show_to(self, user):
19 if self.by_reputation:
20 return user.is_authenticated() and (user.reputation >= int(self.level) or user.is_staff or user.is_superuser)
22 return self.level == 'public' or (user.is_authenticated() and (
23 self.level == 'authenticated' or (
24 self.level == 'superuser' and user.is_superuser) or (
25 self.level == 'staff' and (user.is_staff or user.is_superuser)) or (
26 self.level == 'owner' and user.is_siteowner)))
28 Visibility.PUBLIC = Visibility('public')
29 Visibility.AUTHENTICATED = Visibility('authenticated')
30 Visibility.STAFF = Visibility('staff')
31 Visibility.SUPERUSER = Visibility('superuser')
32 Visibility.OWNER = Visibility('owner')
33 Visibility.REPUTED = lambda r: Visibility(r)
37 def __init__(self, url_pattern):
38 self.url_pattern = url_pattern
40 def __call__(self, u, c):
41 return reverse(self.url_pattern)
44 class ObjectBase(object):
45 class Argument(object):
46 def __init__(self, argument):
47 self.argument = argument
49 def __call__(self, context):
50 if callable(self.argument):
51 return self.argument(context['request'].user, context)
55 def __init__(self, visibility=None, weight=500):
56 self.visibility = visibility
59 def can_render(self, context):
60 return (not self.visibility) or (self.visibility and self.visibility.show_to(context['request'].user))
62 def render(self, context):
65 class LoopBase(ObjectBase):
66 def update_context(self, context):
71 class Link(ObjectBase):
72 def __init__(self, text, url, attrs=None, pre_code='', post_code='', visibility=None, weight=500):
73 super(Link, self).__init__(visibility, weight)
74 self.text = self.Argument(text)
75 self.url = self.Argument(url)
76 self.attrs = self.Argument(attrs or {})
77 self.pre_code = self.Argument(pre_code)
78 self.post_code = self.Argument(post_code)
80 def render(self, context):
81 return "%s %s %s" % (self.pre_code(context),
82 html.hyperlink(self.url(context), self.text(context), **self.attrs(context)),
83 self.post_code(context))
86 class LoopContext(LoopBase):
87 def __init__(self, loop_context, visibility=None, weight=500):
88 super(LoopContext, self).__init__(visibility, weight)
89 self.loop_context = self.Argument(loop_context)
91 def update_context(self, context):
92 context.update(self.loop_context(context))
95 class PageTab(LoopBase):
96 def __init__(self, tab_name, tab_title, url_getter, weight):
97 super(PageTab, self).__init__(weight=weight)
98 self.tab_name = tab_name
99 self.tab_title = tab_title
100 self.url_getter = url_getter
102 def update_context(self, context):
104 tab_name=self.tab_name,
105 tab_title=self.tab_title,
106 tab_url=self.url_getter()
110 class ProfileTab(LoopBase):
111 def __init__(self, name, title, description, url_getter, private=False, weight=500):
112 super(ProfileTab, self).__init__(weight=weight)
115 self.description = description
116 self.url_getter = url_getter
117 self.private = private
119 def can_render(self, context):
120 return not self.private or (
121 context['view_user'] == context['request'].user or context['request'].user.is_superuser)
123 def update_context(self, context):
126 tab_title=self.title,
127 tab_description = self.description,
128 tab_url=self.url_getter(context['view_user'])