-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsettings.py
156 lines (109 loc) · 4.18 KB
/
settings.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import oyaml as yaml
import datetime
from jinja2 import Template
from re import search, sub
# Open Config file
stream = open('settings.yml', 'r')
data = yaml.load(stream, Loader=yaml.SafeLoader)
# make groups more accessible
groupDict = {}
for g in data['padgroups']:
values = data['padgroups'][g]
groupDict[values['name']] = values
# retain backwards compatibility with old configs
if 'auth' not in data.keys():
data['auth'] = {'method': 'ldap'}
data['auth']['ldap'] = data['ldap']
# Returns the allowed padgroups for uid and ldapgroups
def getPadGroups(uid, groupids):
buf = []
for g in data['padgroups']:
groupIter = data['padgroups'][g]
hasUserKey = "user" in groupIter.keys()
hasGroupKey = "groups" in groupIter.keys()
# If both keys dont exist everyone has access
if not hasGroupKey and not hasUserKey:
buf.append(groupIter['name'])
continue
# check for username
if hasUserKey and uid in groupIter['user']:
buf.append(groupIter['name'])
continue
# Check for groups
if hasGroupKey:
if any(gu in groupIter["groups"] for gu in groupids):
buf.append(groupIter['name'])
continue
return buf
def getSecretKey():
key = data['default']['secretkey'] # FIXME
return key
def getDefaultGroup(uid, groupids):
return getPadGroups(uid, groupids)[0]
# check if the group has a name suggestion
def groupHasPadnameSuggestion(group):
if group not in groupDict.keys():
return False
return "padname" in groupDict[group].keys()
# check if the group name suggestion is mandatory
def groupPadnameSuggestionMandatory(group):
# Mandatory is irrelevant if there is no name suggestion
if not groupHasPadnameSuggestion(group):
return False
# if key exists check the value
if "padnameismandatory" in groupDict[group].keys():
return groupDict[group]['padnameismandatory']
return False
# check if the group has a template
def groupHasTemplate(group):
if group not in groupDict.keys():
return False
return "content" in groupDict[group].keys()
# check if the group template is mandatory
def groupTemplateMandatory(group):
if groupHasTemplate(group):
if "contentismandatory" in groupDict[group].keys():
return groupDict[group]['contentismandatory']
return False
# check if the datetime is adjustable for the group
def datetimeAdjustable(group):
if "datetimeadjustable" in groupDict[group].keys():
return groupDict[group]['datetimeadjustable']
return False
# calculate default date from defaultweekday
def getDateDefault(group):
if "weekdaydefault" in groupDict[group].keys():
weekdaydefault = max(1, min(7, int(groupDict[group]['weekdaydefault'])))
d = datetime.date.today()
while d.isoweekday() != weekdaydefault:
d += datetime.timedelta(1)
return d.strftime("%Y-%m-%d")
return ""
# check if either the name or content include the date
def groupHasDate(group):
pattern = "{{ *date(time)? *}}"
return (search(pattern, groupDict[group].get('padname', "")) is not None
or search(pattern, groupDict[group].get('content', "")) is not None)
# check if either the name or content include the date
def groupHasTime(group):
pattern = "{{ *(date)?time *}}"
return (search(pattern, groupDict[group].get('padname', "")) is not None
or search(pattern, groupDict[group].get('content', "")) is not None)
# Helper
def render(template, timestamp):
t = Template(template)
tDate = timestamp.strftime('%Y-%m-%d')
tTime = timestamp.strftime('%H:%M')
tDatetime = tDate + ' ' + tTime
return t.render(date=tDate, time=tTime, datetime=tDatetime)
# render group template
def getGroupTemplate(group, timestamp):
if not groupHasTemplate:
return None
body = render(groupDict[group]['content'], timestamp)
return "<!DOCTYPE HTML><html><body>%s</body></html>" % body
# render group name
def getGroupPadname(group):
if not groupHasPadnameSuggestion(group):
return ""
return sub(r" (?![^{]*}})", '_', groupDict[group]['padname'])