]> git.openstreetmap.org Git - osqa.git/blob - forum/registry.py
ad75b1abffe1fa8644eeb83d9e57995450f6966f
[osqa.git] / forum / registry.py
1 from forum.modules import ui, get_modules_script
2 from django.utils.translation import ugettext as _
3 from django.utils.encoding import smart_unicode
4 from django.core.urlresolvers import reverse
5 from django.template.defaultfilters import slugify
6
7 from forum.models import User
8 from forum.templatetags.extra_tags import get_score_badge
9 from forum.utils.html import cleanup_urls
10 from forum import settings
11
12
13 try:
14     from django.template import get_templatetags_modules
15     modules_template_tags = get_modules_script('templatetags')
16     django_template_tags = get_templatetags_modules()
17
18     for m in modules_template_tags:
19         django_template_tags.append(m.__name__)
20 except:
21     pass
22
23 ui.register(ui.HEADER_LINKS,
24             ui.Link(_('faq'), ui.Url('faq'), weight=400, name='FAQ'),
25             ui.Link(_('about'), ui.Url('about'), weight=300, name='ABOUT'),
26
27             ui.Link(
28                     visibility=ui.Visibility.ANONYMOUS,
29                     text=_('login'),
30                     url=lambda u, c: reverse('auth_signin'),
31                     weight=200, name='LOGIN'),
32             ui.Link(
33                     visibility=ui.Visibility.AUTHENTICATED,
34                     text=_('login'),
35                     url=lambda u, c: reverse('logout'),
36                     weight=200, name='LOGOUT'),
37             ui.Link(
38                     visibility=ui.Visibility.AUTHENTICATED,
39                     text=lambda u, c: smart_unicode(u.username),
40                     url=lambda u, c: u.get_profile_url(),
41                     post_code=lambda u, c: get_score_badge(u),
42                     weight=100, name='ACCOUNT'),
43
44             ui.Link(
45                     visibility=ui.Visibility.SUPERUSER,
46                     text=_('administration'),
47                     url=lambda u, c: reverse('admin_index'),
48                     weight=0, name='ADMINISTRATION')
49
50 )
51
52 class SupportLink(ui.Link):
53     def can_render(self, context):
54         return bool(settings.SUPPORT_URL)
55
56
57 ui.register(ui.FOOTER_LINKS,
58             ui.Link(
59                     text=_('contact'),
60                     url=lambda u, c: settings.CONTACT_URL and settings.CONTACT_URL or "%s?next=%s" % (reverse('feedback'), cleanup_urls( c['request'].path)),
61                     weight=400),
62             SupportLink(_('support'), settings.SUPPORT_URL, attrs={'target': '_blank'}, weight=300),
63             ui.Link(_('privacy'), ui.Url('privacy'), weight=200),
64             ui.Link(_('faq'), ui.Url('faq'), weight=100),
65             ui.Link(_('about'), ui.Url('about'), weight=0),
66 )
67
68 class ModerationMenuGroup(ui.AjaxMenuGroup):
69     def can_render(self, context):
70         return context['user'] != context['viewer'] and super(ModerationMenuGroup, self).can_render(context)
71
72 class SuperUserSwitchMenuItem(ui.UserMenuItem):
73     def can_render(self, context):
74         return context['viewer'].is_siteowner or not context['user'].is_superuser
75
76 ui.register(ui.USER_MENU,
77             ui.UserMenuItem(
78                 label=_("edit profile"),
79                 url=lambda u, c: reverse('edit_user', kwargs={
80                     'id': c['user'].id,
81                     'slug' : slugify(c['user'].username)
82                 }),
83                 span_attrs={'class': 'user-edit'},
84                 weight=0,
85                 name='EDIT_PROFILE'
86             ),
87             ui.UserMenuItem(
88                 label=_("authentication settings"),
89                 url=lambda u, c: reverse('user_authsettings', kwargs={'id': c['user'].id}),
90                 span_attrs={'class': 'user-auth'},
91                 weight=100,
92                 name='AUTH_SETTINGS'
93             ),
94             ui.UserMenuItem(
95                 label=_("email notification settings"),
96                 url=lambda u, c: User.objects.get(id=int(c['user'].id)).get_user_subscriptions_url(),
97                 span_attrs={'class': 'user-subscriptions'},
98                 weight=200,
99                 name='EMAIL_SETTINGS'
100             ),
101             ui.UserMenuItem(
102                 label=_("other preferences"),
103                 url=lambda u, c: reverse('user_preferences', kwargs={'id': c['user'].id, 'slug': slugify(c['user'].username)}),
104                 weight=200,
105                 name='OTHER_PREFS'
106             ),
107             ModerationMenuGroup(_("Moderation tools"), items=(
108                 ui.UserMenuItem(
109                     label=lambda u, c: c['user'].is_suspended() and _("withdraw suspension") or _("suspend this user"),
110                     url=lambda u, c: reverse('user_suspend', kwargs={'id': c['user'].id}),
111                     a_attrs=lambda u, c: {'class': c['user'].is_suspended() and 'ajax-command confirm' or 'ajax-command withprompt'},
112                     render_to=lambda u: not u.is_superuser,
113                     name='SUSPENSION'
114                 ),
115                 ui.UserMenuItem(
116                     label=lambda u, c: _("give/take karma"),
117                     url=lambda u, c: reverse('user_award_points', kwargs={'id': c['user'].id}),
118                     a_attrs=lambda u, c: {'id': 'award-rep-points', 'class': 'ajax-command withprompt'},
119                     span_attrs={'class': 'user-award_rep'},
120                     render_to=lambda u: not u.is_suspended(),
121                     name='KARMA'
122                 ),
123                 ui.UserMenuItem(
124                     label=lambda u, c: c['user'].is_staff and _("remove moderator status") or _("grant moderator status"),
125                     url=lambda u, c: reverse('user_powers', kwargs={'id': c['user'].id, 'action':c['user'].is_staff and 'remove' or 'grant', 'status': 'staff'}),
126                     a_attrs=lambda u, c: {'class': 'ajax-command confirm'},
127                     span_attrs={'class': 'user-moderator'},
128                     name='MODERATOR'
129                 ),
130                 SuperUserSwitchMenuItem(
131                     label=lambda u, c: c['user'].is_superuser and _("remove super user status") or _("grant super user status"),
132                     url=lambda u, c: reverse('user_powers', kwargs={'id': c['user'].id, 'action':c['user'].is_superuser and 'remove' or 'grant', 'status': 'super'}),
133                     a_attrs=lambda u, c: {'class': 'ajax-command confirm'},
134                     span_attrs={'class': 'user-superuser'},
135                     name='SUPERUSER'
136                 ),
137             ), visibility=ui.Visibility.SUPERUSER, weight=500, name='MOD_TOOLS')
138 )