-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
34 lines (28 loc) · 1.2 KB
/
log.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
from urllib import quote
def _now():
from datetime import datetime
import time
t = time.time()
timezone = (datetime.fromtimestamp(t) - datetime.utcfromtimestamp(t)).seconds
zonestr = "%+03d:%02d" % (timezone/3600, timezone%3600/60)
return datetime.fromtimestamp(t).strftime('%Y-%m-%dT%H:%M:%S') + zonestr
def log_event(kind, user, ip, txt):
from django.conf import settings
message = "%s [%s] ip: %s; user: %s; %s\n" % (_now(), kind, ip, user, txt)
f = open(settings.MUSIC_HUB_FOLDER + "events.log", 'a')
f.write(message)
f.close()
def _log_song_event(kind, request, song):
headers = request.META
log_event(kind=kind,
user="%s (%d)" % (quote(request.user.username), request.user.id),
ip=headers.get('HTTP_X_FORWARDED_FOR', headers['REMOTE_ADDR']),
txt="original_filename: %s; code: %s; file_size: %d" % (
quote(song.original_name), quote(song.get_code()), song.data_file.size)
)
def log_song_upload(request, song):
_log_song_event('upload', request, song)
def log_song_download(request, song):
_log_song_event('download', request, song)
def log_song_delete(request, song):
_log_song_event('delete', request, song)