/
coursetags.py
137 lines (105 loc) · 4.42 KB
/
coursetags.py
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
__copyright__ = "Copyright (C) 2016 Dong Zhuang, Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from django.template import Library, Node, TemplateSyntaxError
from django.utils import translation
register = Library()
# {{{ get language_code in JS traditional naming format
class GetCurrentLanguageJsFmtNode(Node):
def __init__(self, variable):
self.variable = variable
def render(self, context):
lang_name = (
translation.to_locale(translation.get_language()).replace("_", "-"))
context[self.variable] = lang_name
return ""
@register.tag("get_current_js_lang_name")
def do_get_current_js_lang_name(parser, token):
"""
This will store the current language in the context, in js lang format.
This is different with built-in do_get_current_language, which returns
languange name like "en-us", "zh-hans". This method return lang name
"en-US", "zh-Hans", with the country code capitallized if country code
has 2 characters, and capitalize first if country code has more than 2
characters.
Usage::
{% get_current_language_js_lang_format as language %}
This will fetch the currently active language name with js tradition and
put it's value into the ``language`` context variable.
"""
# token.split_contents() isn't useful here because this tag doesn't
# accept variable as arguments
args = token.contents.split()
if len(args) != 3 or args[1] != "as":
raise TemplateSyntaxError("'get_current_js_lang_name' requires "
"'as variable' (got %r)" % args)
return GetCurrentLanguageJsFmtNode(args[2])
# }}}
# {{{ filter for participation.has_permission()
@register.filter(name="has_permission")
def has_permission(participation, arg):
"""
Check if a participation instance has specific permission.
:param participation: a :class:`participation:` instance
:param arg: String, with permission and arguments separated by comma
:return: a :class:`bool`
"""
has_pperm = False
try:
arg_list = [s.strip() for s in arg.split(",")]
perm = arg_list[0]
argument = None
if len(arg_list) > 1:
argument = arg_list[1]
has_pperm = participation.has_permission(perm, argument)
except Exception:
# fail silently
pass
return has_pperm
# }}}
@register.filter(name="may_set_fake_time")
def may_set_fake_time(user):
"""
Check if a user may set fake time.
:param user: a :class:`accounts.User:` instance
:return: a :class:`bool`
"""
from course.views import may_set_fake_time as msf
return msf(user)
@register.filter(name="may_set_pretend_facility")
def may_set_pretend_facility(user):
"""
Check if a user may set pretend_facility
:param user: a :class:`accounts.User:` instance
:return: a :class:`bool`
"""
from course.views import may_set_pretend_facility as mspf
return mspf(user)
@register.filter(name="commit_message_as_html")
def commit_message_as_html(commit_sha, repo):
from course.versioning import _get_commit_message_as_html
return _get_commit_message_as_html(repo, commit_sha)
@register.filter(name="get_item")
def get_item(dictionary, key):
return dictionary.get(key, None)
@register.filter(name="get_item_or_key")
def get_item_or_key(dictionary, key):
return dictionary.get(key, key)
@register.filter(name="startswith")
def startswith(s, arg):
return s.startswith(arg)