-
Notifications
You must be signed in to change notification settings - Fork 0
/
libcams.py
348 lines (287 loc) · 10.5 KB
/
libcams.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# CAMS - libcams.py
#
# Copyright (C) 2009, 2010, 2011. 2012, 2013
# Guillaume Tucker <guillaume@mangoz.org>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
import os
import csv
import datetime
import logging
from django.conf.urls import url as django_url
from django.core.urlresolvers import reverse
from django.http import HttpResponse
CAMS_VERSION = (0, 6)
# -----------------------------------------------------------------------------
# Main menu
class Menu(object):
def __init__(self):
self._items = []
# ToDo: get the namespace from the urlpatterns?
def add(self, urlpatterns=[], items=[], namespace=''):
url_perms = dict()
for url in urlpatterns:
url_name = getattr(url, 'name', None)
if url_name is None:
continue
try:
cls = getattr(url, 'viewcls')
perms = getattr(cls, 'perms')
except AttributeError:
perms = []
url_perms[url_name] = perms
for item in items:
if not item.perms:
item.perms = url_perms.get(item.url_name, [])
if namespace:
item.url_name = ':'.join([namespace, item.url_name])
self._items.append(item)
def get_user_items(self, user):
for it in self._items:
if it.user_authorised(user):
yield it
def set_current(self, url_name):
for it in self._items:
if it.url_name == url_name:
it.current = True
else:
it.current = False
class Item(object):
def __init__(self, url_name, title='', perms=[]):
self.url_name = url_name
self._url = None
if not title:
self.title = url_name
else:
self.title = title
self.perms = perms
self.current = False
def __str__(self):
return ', '.join([self.url_name, self.title, str(self.perms)])
@property
def url(self):
if self._url is None:
self._url = reverse(self.url_name)
return self._url
def user_authorised(self, user):
for p in self.perms:
if not user.has_perm(p):
return False
return True
class StaffItem(Item):
def __init__(self, url_name, title=''):
super(Menu.StaffItem, self).__init__(url_name, title)
def user_authorised(self, user):
return user.is_staff
# -----------------------------------------------------------------------------
# CSV file response
class CSVFileResponse(object):
def __init__(self, fields, **kwargs):
self._resp = HttpResponse(mimetype='text/csv')
self._csv = csv.writer(self._resp, **kwargs)
self._csv.writerow(fields)
def write(self, values):
values_utf8 = []
for v in values:
values_utf8.append(v.encode('utf-8'))
self._csv.writerow(values_utf8)
def set_file_name(self, f):
self._resp['Content-Disposition'] = \
'attachement; filename=\"{:s}\"'.format(f)
@property
def response(self):
return self._resp
# -----------------------------------------------------------------------------
# History
class HistoryLogger(object):
def __init__(self, logger_name, plain_logger_name=None):
self._logger = logging.getLogger(logger_name)
if plain_logger_name:
self._plain_logger = logging.getLogger(plain_logger_name)
else:
self._plain_logger = None
def create(self, user, obj, fields):
self._log(user, obj, 'CREATE', self._make_msg(obj, fields))
def create_form(self, user, form, extra=[]):
self.create(user, form.instance, form.fields.keys() + extra)
def edit(self, user, obj, fields):
self._log(user, obj, 'EDIT', self._make_msg(obj, fields))
def edit_form(self, user, form, extra=[]):
changed_data = form.changed_data + extra
if changed_data:
self.edit(user, form.instance, changed_data)
def edit_changed(self, user, obj, form):
changed_fields = list()
for f in obj._meta.fields:
if f.name in form.fields.keys():
if getattr(obj, f.name) != getattr(form.instance, f.name):
changed_fields.append(f.name)
if changed_fields:
self.edit(user, form.instance, changed_fields)
def delete(self, user, obj):
self._log(user, obj, 'DELETE', '')
def _make_msg(self, obj, fields):
msg_str = []
for it in fields:
x = getattr(obj, '{}_str'.format(it), None)
if not x:
x = getattr(obj, it)
pk = getattr(x, 'pk', None)
if not pk:
x = unicode(x).replace('\\', '\\\\').replace('\"', '\\\"')
x = u'\"{}\"'.format(x)
else:
x = u':'.join([x.__class__.__name__, unicode(x.pk)])
msg_str.append(u'{}: {}'.format(it, x))
return u', '.join(msg_str)
def _log(self, user, obj, action, msg):
e = {'uid': user.id, 'action': action, 'otype': type(obj).__name__,
'oid': obj.pk}
self._logger.info(msg, extra=e)
if self._plain_logger:
plain_msg = u'{} {} [{}:{}] {}'. \
format(user.username, action, e['otype'], e['oid'], msg)
self._plain_logger.info(plain_msg)
def unescape(self, txt):
return txt.replace('\\\"', '\"').replace('\\\\', '\\')
class HistoryParser(object):
cache = dict()
def __init__(self, file_name, classes):
self._file_name = file_name
self._classes = classes
self._data = list()
self._f = None
@property
def data(self):
return self._data
def open(self):
self._f = open(self._file_name, 'r')
self._mtime = os.path.getmtime(self._file_name)
cache_mtime, self._data, self._lines = \
self.cache.setdefault(self._file_name, (None, list(), list()))
if self._mtime != cache_mtime:
self._data = list()
self._lines = self._f.readlines()
def close(self):
self._f.close()
self._f = None
self.cache[self._file_name] = (self._mtime, self._data, self._lines)
def __getitem__(self, i):
if self._f is None:
raise Exception('History log file not open')
while len(self._data) < (i + 1):
line = self._lines.pop(-1)
if not line:
raise IndexError
pline = HistoryParser.Line(line)
self._data.append(HistoryParser.Item \
(datetime=self._parse_date_time(pline),
user=self._parse_obj(pline),
obj=self._parse_obj(pline),
action=pline.parse_block(),
args=pline.parse_args()))
return self._data[i]
def _parse_date_time(self, pline):
block = pline.parse_block()
date_ints = []
for date_str in block.split(' '):
date_ints += [int(x) for x in date_str.split('.')]
return datetime.datetime(*date_ints)
def _parse_obj(self, pline):
block = pline.parse_block()
obj_class, obj_pk = block.split(':')
cls = self._classes.get(obj_class)
if not cls:
return None
try:
return cls.objects.get(pk=obj_pk)
except cls.DoesNotExist:
return None
class Line(object):
def __init__(self, line):
self._line = line
self._pos = 0
def parse_block(self):
start = self._line.find('[', self._pos)
if start == -1:
raise Exception("Failed to find block start")
start += 1
stop = self._line.find(']', start)
if stop == -1:
raise Exception("Failed to find block end")
self._pos = stop + 1
return self._line[start:stop]
def parse_args(self):
return self._line[self._pos:].strip()
class Item(object):
def __init__(self, **kw):
for k, v in kw.items():
setattr(self, k, v)
# -----------------------------------------------------------------------------
# Helpers
def get_first_words(text, max_l=24):
if len(text) <= max_l:
return text
i = 0
j = 0
while (i < max_l) and (j >= 0):
i = j
j = text.find(' ', i + 1, max_l)
return text[:i] + '...'
def str2list(match, sep=' '):
match = match.strip()
words = match.split(sep)
ret = []
for word in words:
word.strip()
if word:
ret.append(word)
return ret
def get_time_string():
now = datetime.datetime.today()
return '{:d}-{:02d}-{:02d}_{:02d}:{:02d}:{:02d}'.format \
(now.year, now.month, now.day, now.hour, now.minute, now.second)
# ToDo: that should really go away eventually
# (class methods in the models should be able to do that in a nicer way)
def get_obj_address(obj, sep=', '):
addr = []
if obj.line_1:
addr.append(obj.line_1)
if obj.line_2:
addr.append(obj.line_2)
if obj.line_3:
addr.append(obj.line_3)
if obj.town:
addr.append(obj.town)
if obj.postcode:
addr.append(obj.postcode)
return sep.join(addr)
def make_group_file_name (group, sx = ''):
if group.board:
board_str = '-%s' % group.board.name.replace (' ', '')
else:
board_str = ''
return '%s%s%s_%s' % (group.name.replace (' ', '_'),
board_str, sx, get_time_string ())
def get_user_email(user):
if user.email:
return user.email
try:
return user.player.person.contact_set.exclude(email='')[0].email
except IndexError:
return None
def urlcls(regex, cls, **kw):
url_obj = django_url(regex, cls.as_view(), **kw)
url_obj.viewcls = cls
return url_obj