-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.py
74 lines (58 loc) · 2.04 KB
/
extensions.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
import json
from html import escape
from harrier.config import Mode
from harrier.extensions import template
from harrier.render import resolve_url as harrier_resolve_url
@template.pass_context
def resolve_url(ctx, path):
# Need to reference api.tc.com for tc.com/api/
config = ctx['config']
if config.mode == Mode.development:
return harrier_resolve_url(ctx, path)
return config.site_root + harrier_resolve_url(ctx, path)
@template.pass_context
def js_url(ctx, path):
return path if ctx['config'].mode == Mode.development else resolve_url(ctx, path)
@template.pass_context
def code_line_list(ctx, file_name):
with open('./pages/' + file_name, 'r+') as f:
# Return as list as need length of list in jinja template
return list(map(escape, f.readlines()))
def replace_spaces(line):
space_count = 0
for i, char in enumerate(line, start=1):
if char != ' ':
space_count = i - 1
return escape(line).replace(' ', ' ', space_count)
@template.pass_context
def json_line_list(ctx, file_name):
with open('./pages/' + file_name, 'r+') as f:
return map(replace_spaces, f.readlines())
SUBJECT_TYPE_HREF = {
'Job': 'services',
'Lesson': 'appointments',
'Client': 'clients',
'Affiliate': 'agents',
'Tutor': 'contractors',
'Invoice': 'invoices',
'Payment Order': 'payment-orders',
'Credit Request': 'proforma-invoices',
'Student': 'recipients',
'Ad Hoc Charge': 'adhoccharges',
'Ad Hoc Charge Category': 'ahc-categories',
'Application': 'tenders',
'Report': 'reports',
'Task': 'tasks',
'Note': 'notes',
'Package': 'package',
}
@template.pass_context
def get_actions(ctx):
with open('./data/actions.json', 'r') as f:
actions = json.load(f)
for action in actions:
sub_types = action.get('subject_types')
if sub_types:
type_data = [{'name': sub_type, 'href': SUBJECT_TYPE_HREF[sub_type]} for sub_type in sub_types]
action['subject_types_data'] = type_data
return actions