1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
import logging
from django import template
from django.core import exceptions as django_exceptions
from django.utils.translation import ugettext as _
from django.contrib.humanize.templatetags import humanize
from django.template import defaultfilters
from askbot import exceptions as askbot_exceptions
from askbot import auth
from askbot import models
from askbot.deps.grapefruit import Color
from askbot.conf import settings as askbot_settings
from askbot.skins import utils as skin_utils
from askbot.utils import functions
from askbot.utils.slug import slugify
register = template.Library()
@register.filter
def collapse(input):
input = str(input)
return ' '.join(input.split())
slugify = register.filter(slugify)
def make_template_filter_from_permission_assertion(
assertion_name = None,
filter_name = None,
allowed_exception = None
):
"""a decorator-like function that will create a True/False test from
permission assertion
"""
def filter_function(user, post):
if askbot_settings.ALWAYS_SHOW_ALL_UI_FUNCTIONS:
return True
if user.is_anonymous():
return False
assertion = getattr(user, assertion_name)
if allowed_exception:
try:
assertion(post)
return True
except allowed_exception:
return True
except django_exceptions.PermissionDenied:
return False
else:
try:
assertion(post)
return True
except django_exceptions.PermissionDenied:
return False
register.filter(filter_name, filter_function)
return filter_function
@register.filter
def can_moderate_user(user, other_user):
if user.is_authenticated() and user.can_moderate_user(other_user):
return True
return False
can_flag_offensive = make_template_filter_from_permission_assertion(
assertion_name = 'assert_can_flag_offensive',
filter_name = 'can_flag_offensive',
allowed_exception = askbot_exceptions.DuplicateCommand
)
can_post_comment = make_template_filter_from_permission_assertion(
assertion_name = 'assert_can_post_comment',
filter_name = 'can_post_comment'
)
can_close_question = make_template_filter_from_permission_assertion(
assertion_name = 'assert_can_close_question',
filter_name = 'can_close_question'
)
can_delete_comment = make_template_filter_from_permission_assertion(
assertion_name = 'assert_can_delete_comment',
filter_name = 'can_delete_comment'
)
#this works for questions, answers and comments
can_delete_post = make_template_filter_from_permission_assertion(
assertion_name = 'assert_can_delete_post',
filter_name = 'can_delete_post'
)
can_reopen_question = make_template_filter_from_permission_assertion(
assertion_name = 'assert_can_reopen_question',
filter_name = 'can_reopen_question'
)
can_edit_post = make_template_filter_from_permission_assertion(
assertion_name = 'assert_can_edit_post',
filter_name = 'can_edit_post'
)
can_retag_question = make_template_filter_from_permission_assertion(
assertion_name = 'assert_can_retag_question',
filter_name = 'can_retag_question'
)
can_accept_best_answer = make_template_filter_from_permission_assertion(
assertion_name = 'assert_can_accept_best_answer',
filter_name = 'can_accept_best_answer'
)
@register.filter
def can_see_offensive_flags(user, post):
"""Determines if a User can view offensive flag counts.
there is no assertion like this User.assert_can...
so all of the code is here
user can see flags on own posts
otherwise enough rep is required
or being a moderator or administrator
suspended or blocked users cannot see flags
"""
if user.is_authenticated():
if user == post.get_owner():
return True
if user.reputation >= askbot_settings.MIN_REP_TO_VIEW_OFFENSIVE_FLAGS:
return True
elif user.is_administrator() or user.is_moderator():
return True
else:
return False
else:
return False
@register.filter
def can_view_user_edit(request_user, target_user):
return auth.can_view_user_edit(request_user, target_user)
@register.filter
def can_view_user_votes(request_user, target_user):
return auth.can_view_user_votes(request_user, target_user)
@register.filter
def can_view_user_preferences(request_user, target_user):
return auth.can_view_user_preferences(request_user, target_user)
@register.filter
def is_user_self(request_user, target_user):
return auth.is_user_self(request_user, target_user)
@register.filter
def cnprog_intword(number):
try:
if 1000 <= number < 10000:
string = str(number)[0:1]
return '<span class="thousand">%sk</span>' % string
else:
return number
except:
return number
@register.filter
def humanize_counter(number):
if number == 0:
return _('no items in counter')
elif number >= 1000:
number = number/1000
s = '%.1f' % number
if s.endswith('.0'):
return s[:-2] + 'k'
else:
return s + 'k'
else:
return str(number)
@register.filter
def absolute_value(number):
return abs(number)
|