]> git.openstreetmap.org Git - osqa.git/blob - forum/modules/ui_objects.py
Adds a head content injection spot for modules, and a new ui object for template...
[osqa.git] / forum / modules / ui_objects.py
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
5
6 class Visibility(object):
7     def __init__(self, level='public'):
8         if level not in ['public', 'authenticated', 'staff', 'superuser', 'owner']:
9             try:
10                 int(level)
11                 self.by_reputation = True
12             except:
13                 raise "Invalid visibility level for ui object: %s" % level
14         else:
15             self.by_reputation = False
16
17         self.level = level
18
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)
22         else:
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)))
28
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)
35
36
37 class Url(object):
38     def __init__(self, url_pattern):
39         self.url_pattern = url_pattern
40
41     def __call__(self, u, c):
42         return reverse(self.url_pattern)
43
44
45 class ObjectBase(object):
46     class Argument(object):
47         def __init__(self, argument):
48             self.argument = argument
49
50         def __call__(self, context):
51             if callable(self.argument):
52                 return self.argument(context['request'].user, context)
53             else:
54                 return self.argument
55
56     def __init__(self, visibility=None, weight=500):
57         self.visibility = visibility
58         self.weight = weight
59
60     def can_render(self, context):
61         return (not self.visibility) or (self.visibility and self.visibility.show_to(context['request'].user))
62
63     def render(self, context):
64         return ''
65
66 class LoopBase(ObjectBase):
67     def update_context(self, context):
68         pass
69
70
71
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)
80
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))
85
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)
90
91     def render(self, context):
92         return self.template.render(context)
93         
94
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)
99
100     def update_context(self, context):
101         context.update(self.loop_context(context))
102
103
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
110
111     def update_context(self, context):
112         context.update(dict(
113             tab_name=self.tab_name,
114             tab_title=self.tab_title,
115             tab_url=self.url_getter()
116         ))
117
118
119 class ProfileTab(LoopBase):
120     def __init__(self, name, title, description, url_getter, private=False, weight=500):
121         super(ProfileTab, self).__init__(weight=weight)
122         self.name = name
123         self.title = title
124         self.description = description
125         self.url_getter = url_getter
126         self.private = private
127
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)
131
132     def update_context(self, context):        
133         context.update(dict(
134             tab_name=self.name,
135             tab_title=self.title,
136             tab_description = self.description,
137             tab_url=self.url_getter(context['view_user'])
138         ))