]> git.openstreetmap.org Git - osqa.git/blob - forum/modules/ui_objects.py
Starts preparing the app for module based html injection.
[osqa.git] / forum / modules / ui_objects.py
1 from django.core.urlresolvers import reverse
2 from forum.utils import html
3
4 class UiObjectUserLevelBase(object):
5     def show_to(self, user):
6         return True
7
8 class SuperuserUiObject(UiObjectUserLevelBase):
9     def show_to(self, user):
10         return user.is_superuser
11
12 class StaffUiObject(UiObjectUserLevelBase):
13     def show_to(self, user):
14         return user.is_staff or user.is_superuser
15
16 class ReputedUserUiObject(UiObjectUserLevelBase):
17     def __init__(self, min_rep):
18         self.min_rep = min_rep
19
20     def show_to(self, user):
21         return user.is_authenticated() and user.reputation >= int(self.min_rep)
22
23 class LoggedInUserUiObject(UiObjectUserLevelBase):
24     def show_to(self, user):
25         return user.is_authenticated()
26
27 class PublicUiObject(UiObjectUserLevelBase):
28     pass
29
30
31
32 class UiObjectArgument(object):
33     def __init__(self, argument):
34         self.argument = argument
35
36     def __call__(self, context):
37         if callable(self.argument):
38             return self.argument(context)
39         else:
40             return self.argument
41
42
43 class UiObjectBase(object):
44     def __init__(self, user_level=None, weight=500):
45         self.user_level = user_level or PublicUiObject()
46         self.weight = weight
47
48     def can_render(self, context):
49         return self.user_level.show_to(context['request'].user)
50
51     def render(self, context):
52         return ''
53
54 class UiLoopObjectBase(UiObjectBase):
55     def update_context(self, context):
56         pass
57
58
59
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)
68
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))
73
74
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)
79
80     def update_context(self, context):
81         context.update(self.loop_context(context))
82
83
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
90
91     def update_context(self, context):
92         context.update(dict(
93             tab_name=self.tab_name,
94             tab_title=self.tab_title,
95             tab_url=reverse(self.url_pattern)
96         ))