]> git.openstreetmap.org Git - osqa.git/blob - 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
1 from django.core.urlresolvers import reverse
2 from django.template.defaultfilters import slugify
3 from forum.utils import html
4
5 class Visibility(object):
6     def __init__(self, level='public'):
7         if level not in ['public', 'authenticated', 'staff', 'superuser', 'owner']:
8             try:
9                 int(level)
10                 self.by_reputation = True
11             except:
12                 raise "Invalid visibility level for ui object: %s" % level
13         else:
14             self.by_reputation = False
15
16         self.level = level
17
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)
21         else:
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)))
27
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)
34
35
36 class Url(object):
37     def __init__(self, url_pattern):
38         self.url_pattern = url_pattern
39
40     def __call__(self, u, c):
41         return reverse(self.url_pattern)
42
43
44 class ObjectBase(object):
45     class Argument(object):
46         def __init__(self, argument):
47             self.argument = argument
48
49         def __call__(self, context):
50             if callable(self.argument):
51                 return self.argument(context['request'].user, context)
52             else:
53                 return self.argument
54
55     def __init__(self, visibility=None, weight=500):
56         self.visibility = visibility
57         self.weight = weight
58
59     def can_render(self, context):
60         return (not self.visibility) or (self.visibility and self.visibility.show_to(context['request'].user))
61
62     def render(self, context):
63         return ''
64
65 class LoopBase(ObjectBase):
66     def update_context(self, context):
67         pass
68
69
70
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)
79
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))
84
85
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)
90
91     def update_context(self, context):
92         context.update(self.loop_context(context))
93
94
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
101
102     def update_context(self, context):
103         context.update(dict(
104             tab_name=self.tab_name,
105             tab_title=self.tab_title,
106             tab_url=self.url_getter()
107         ))
108
109
110 class ProfileTab(LoopBase):
111     def __init__(self, name, title, description, url_getter, private=False, weight=500):
112         super(ProfileTab, self).__init__(weight=weight)
113         self.name = name
114         self.title = title
115         self.description = description
116         self.url_getter = url_getter
117         self.private = private
118
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)
122
123     def update_context(self, context):        
124         context.update(dict(
125             tab_name=self.name,
126             tab_title=self.title,
127             tab_description = self.description,
128             tab_url=self.url_getter(context['view_user'])
129         ))