-
Notifications
You must be signed in to change notification settings - Fork 1
/
logview.py
316 lines (296 loc) · 10.7 KB
/
logview.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
#!/usr/bin/env python
# -*- coding: utf8 -*-
import log
import datetime
import util
import re
##########################
## Main view page
##########################
def get_log(keyword, date1, date2, location, limit):
where = []
if keyword is not None:
where.append('log like "%'+keyword+'%"')
if date1 is not None:
if date2 is None:
d1 = date1.split("-")
date2 = datetime.date(int(d1[0]), int(d1[1]), int(d1[2]))
date2 += datetime.timedelta(1)
time1 = str(date1) + ' 05:00'
time2 = str(date2) + ' 05:00'
where.append('time > "'+time1+'"')
where.append('time < "'+time2+'"')
if location is not None:
[lat,lng] = location.split(',')
wstr = '(latitude-' + str(lng) + ')*(latitude-' + str(lng) + ') + (longitude-' + str(lat) + ')*(longitude-' + str(lat) +') < 0.000001'
where.append(wstr)
query = 'SELECT rowid, * FROM events '
if len(where):
query += "WHERE " + " AND ".join(where) + " ORDER BY time"
else:
if limit is None:
query += " ORDER BY time desc"
else:
query += " ORDER BY time desc LIMIT " + str(limit)
log0 = util.query_db(query)
return log0
def get_media(date1, date2):
where = []
if date1 is not None:
if date2 is None:
d1 = date1.split("-")
date2 = datetime.date(int(d1[0]), int(d1[1]), int(d1[2]))
date2 += datetime.timedelta(1)
time1 = str(date1) + ' 05:00'
time2 = str(date2) + ' 05:00'
where.append('time > "'+time1+'"')
where.append('time < "'+time2+'"')
query = 'SELECT * FROM media '
if len(where): query += "WHERE " + " AND ".join(where) + " ORDER BY time"
media = util.query_db(query)
return media
def html_log(events):
html = '''<div id='events'>
<table width="95%" border=0 cellpadding=4>
<tr>
<td width="10%"><h1>Time</h1></td>
<td width="82%"><h1>Log ('''+str(len(events))+''')</h1></td>
<td width="8%"></td>
</tr>'''
for (idx, event) in enumerate(events):
(rid, category, time0, lat, lng, log_string) = event
col = "DDDDEE" if idx%2==0 else "FFFFFF"
timestamp = util.format_time(time0)
if lat is None:
actions = '<a href="/edit/?id='+str(rid)+'">[e]</a>'
else:
actions = '''<a href="https://maps.google.com/maps?q='''+str(lat)+''','''+str(lng)+'''">[m]</a>
<a href="javascript:SetEditLog('''+str(rid)+''');">[e]</a>'''
links = re.compile('[~!@#\^][^ \'.?,!\(\)]+').findall(log_string)
for link in links:
log_string = re.sub(link, '<a href="?keyword='+link[1:]+'">'+link+'</a>', log_string)
#weblinks = re.compile('\^\((.+)\)').findall(log_string)
#for link in weblinks:
# log_string = re.sub(link, '<a href="http://'+link+'">[web]</a>', log_string)
html += '''
<tr bgcolor="'''+col+'''">
<td>'''+ timestamp +'''</td>
<td>'''+ log_string +'''</td>
<td>'''+ actions +'''</td>
</tr>'''
html += '</table>'
html += '</div>'
# modify log viewbox
html += '<div id="modifylog" style="display:none; z-index:1;"></div>'
html += '''<script>
function HideModifyLog() {
document.getElementById('modifylog').style.display = 'none';
}
function SetEditLog(idx) {
document.getElementById('modifylog').style.display = 'block';
document.getElementById('modifylog').innerHTML = '<iframe src="/edit/?id='+idx+'" width="100%" height="100%"></iframe><br/><a href="javascript:HideModifyLog();"> [close]</a>';
}
</script>'''
return html
def html_media(media):
html ='''<div id='media'>'''
if len(media) > 0:
skip_thumb = max(1, int(len(media)/4.0))
for (r, (idx, typ, path, time)) in enumerate(media):
if r % skip_thumb == 0:
html += '<a href="'+path+'" rel="lightbox[logimages]"><img src="'+path+'" width=160 /></a>'
else:
html += '<a href="'+path+'" rel="lightbox[logimages]"></a>'
html += '</div>'
return html
def html_calendar():
html = '''<div id='calendar'>
<table width=280 border=2 cellspacing=0>
<tr>
<td width=40><h1>M</h1></td>
<td width=40><h1>T</h1></td>
<td width=40><h1>W</h1></td>
<td width=40><h1>R</h1></td>
<td width=40><h1>F</h1></td>
<td width=40><h1>Sa</h1></td>
<td width=40><h1>Su</h1></td>
</tr>
<tr>'''
date1 = datetime.date(2012, 8, 6) # the beginning of time
date2 = datetime.date.today()
day_count = (date2 - date1).days + 1
for d in [d for d in (date1 + datetime.timedelta(n) for n in range(day_count)) if d <= date2]:
if d.day==1:
txt = '<b>'+str(d.month)+'</b>/'+str(d.day)
else:
txt = str(d.day)
html += '\t\t<td><a href="?date1='+str(d)+'">'+txt+'</a></td>\n'
if d.weekday()==6:
html += '\t</tr>\n\t<tr>\n'
html += '\n\t</tr>\n</table>\n</div>'
return html
##########################
## Edit logger entry
##########################
def html_edit(id, time0, lat, lng, log0, action):
html = '<div id="editlog"><h1>'
# edit an entry
if action is None:
(time0, lat, lng, log0) = util.query_db('SELECT time, latitude, longitude, log FROM events WHERE rowid='+str(id))[0]
html += '''Edit entry #'''+str(id)+'''<p/>
<form name="editor" action="" method="get">
<input type="hidden" name="id" value="'''+str(id)+'''">
<input type="hidden" name="action" value="modify">
Time: <input type="text" name="time" value="'''+time0+'''">
<p/>
Latitude: <input type="text" name="lat" value="'''+str(lat)+'''">
Longitude: <input type="text" name="lng" value="'''+str(lng)+'''">
<p/>
<textarea name="log0" cols="60" rows="10">'''+str(log0)+'''</textarea>
<p/>
<input type="submit" value="Edit">
</form>
<p/><a href="?action=delete&id='''+str(id)+'''">Delete</a>'''
# entry modified
elif str(action) == "modify":
html += '''<b>Modified</b>:
<br/>Id: '''+str(id)+'''
<br/>Time: '''+str(time0)+'''
<br/>Latitude: '''+str(lat)+'''
<br/>Longitude: '''+str(lng)+'''
<br/>Log: '''+str(log0)+'''
<p/><a href="/">[back]</a>'''
log.modify_event(id, time0, lat, lng, log0)
# entry deleted
elif str(action) == "delete":
(time0, lat, lng, log0) = util.query_db('SELECT time, latitude, longitude, log FROM events WHERE rowid='+str(id))[0]
html += '''<b>Deleted:</b>
<br/>Id: '''+str(id)+'''
<br/>Time: '''+str(time0)+'''
<br/>Latitude: '''+str(lat)+'''
<br/>Longitude: '''+str(lng)+'''
<br/>Log: '''+str(log0)+'''
<p/><a href="/">[back]</a>'''
log.delete_event(id)
html += '</h1></div>'
return html
def html_edit_name(id, name, alias, action):
html = '<div id="editname"><h1>'
# edit a name
if action is None:
(name, alias) = util.query_db('SELECT name, alias FROM people WHERE rowid='+str(id))[0]
html += '''Edit Name #'''+str(id)+'''<p/>
<form name="editor" action="" method="get">
<input type="hidden" name="id" value="'''+str(id)+'''">
<input type="hidden" name="action" value="modify">
Name: <input type="text" name="name" value="'''+name+'''">
Alias: <input type="text" name="alias" value="'''+alias+'''">
<p/><input type="submit" value="Edit">
</form>
<p/><a href="?action=delete&id='''+str(id)+'''">Delete</a>'''
# name modified
elif str(action) == "modify":
html += '''<b>Modified:</b>
<br/>Id: '''+str(id)+'''
<br/>Name: '''+name+'''
<br/>Alias: '''+alias+'''
<p/><a href="/">[back]</a>'''
log.modify_name_lookup(id,name,alias)
# name deleted
elif str(action) == "delete":
(name, alias) = util.query_db('SELECT name, alias FROM people WHERE rowid='+str(id))[0]
html += '''<b>Deleted:</b>
<br/>Id: '''+str(id)+'''
<br/>Name: '''+name+'''
<br/>Alias: '''+alias+'''
<p/><a href="/">[back]</a>'''
log.delete_name_lookup(id)
html += '</h1></div>'
return html
def html_edit_location(id, name, lat, lng, action):
html = '<div id="editlocation"><h1>'
# edit a location
if action is None:
(name, lat, lng) = util.query_db('SELECT name, latitude, longitude FROM locations WHERE rowid='+str(id))[0]
html += '''Edit location #'''+str(id)+'''<p/>
<form name="editor" action="" method="get">
<input type="hidden" name="id" value="'''+str(id)+'''">
<input type="hidden" name="action" value="modify">
Name: <input type="text" name="name" value="'''+name+'''">
<p/>
Latitude: <input type="text" name="lat" value="'''+str(lat)+'''">
Longitude: <input type="text" name="lng" value="'''+str(lng)+'''">
<p/><input type="submit" value="Edit">
</form>
<p/><a href="?action=delete&id='''+str(id)+'''">Delete</a>'''
# location modified
elif str(action) == "modify":
html += '''<b>Modified:</b>
<br/>Id: '''+str(id)+'''
<br/>Name: '''+name+'''
<br/>Latitude: '''+str(lat)+'''
<br/>Longitude: '''+str(lng)+'''
<p/><a href="/">[back]</a>'''
log.modify_location_lookup(id,name,lat,lng)
# location deleted
elif str(action) == "delete":
(name, lat, lng) = util.query_db('SELECT name, latitude, longitude FROM locations WHERE rowid='+str(id))[0]
html += '''<b>Deleted:</b>
<br/>Id: '''+str(id)+'''
<br/>Name: '''+name+'''
<br/>Latitude: '''+str(lat)+'''
<br/>Longitude: '''+str(lng)+'''
<p/><a href="/">[back]</a>'''
log.delete_location_lookup(id)
html += '</h1></div>'
return html
def html_mass_edit(backlog_text = None, action = None):
html = '''<style>
#backlog {
background-color:#FFF;
border:4px solid #5599DD;
margin:10px;
margin-top:16px;
padding:3px;
}
#massedit {
margin:10px;
margin-top:32px;
}
</style>'''
if backlog_text is not None:
html += '<table id="backlog" width="96%" border=1 style="margin:32px">'
html += '<tr><td width="8%"><b>category</b></td><td width="60%"><b>log</b></td><td width="16%"><b>time</b></td><td width="16%"><b>location</b></td></tr>'
backlog_lines = backlog_text.split('\n')
for l in backlog_lines:
logstring = re.compile('^log ').split(l)[-1]
(logstring, time0, gps, category) = log.process_log_string(logstring, 'log', False)
print gps
if gps[0] is None: location = '<font color="#FF0000">none</font>'
else: location = '%0.3f, %0.3f' % gps
html += '<tr><td>'+category+'</td><td>'+logstring+'</td><td>'+time0+'</td><td>'+location+'</td></tr>'
if action=='addtolog':
log.add_log(logstring, time0, gps, category)
html += '</table><p>'
if action != 'addtolog':
last_log = util.query_db('SELECT rowid, time, log FROM events ORDER BY time DESC LIMIT 1')
html += '<p><div id="massedit">'
for l in last_log: html += '<br> '+str(l[0])+' ('+l[1][:-3]+') :: '+l[2]
html += '''<form name="editor" action="" method="post">
<textarea id="backlog" name="backlog" cols=110 rows=30 maxlength=2500>'''
if backlog_text is not None:
html += backlog_text
html += '''</textarea><p/>
<select name="action">
<option value="validate">validate</option>
<option value="addtolog">add to log</option>
</select>
<input type="submit" value="Submit">
<input type="button" value="Save" id="save">
<a href="javascript:removeLimit();">[remove limit]</a>
</form>
</div>
'''
else:
html += '<p><a href="/">back</a>'
return html