forked from garmoncheg/django-log-file-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
109 lines (100 loc) · 3.85 KB
/
views.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
import os, csv
from django.http import HttpResponse
from django.core.paginator import Paginator
from django.shortcuts import render_to_response
from django.http import HttpResponseBadRequest
from django.contrib.auth.decorators import login_required, user_passes_test
from settings import *
from models import LogFilesManager
__all__ = ['logfiles_list', 'logfile_view', 'logfile_to_csv']
@login_required
@user_passes_test(lambda u: u.is_superuser)
def logfiles_list(request, template_name='listfiles.html'):
"""Lists Log Files in settings directory"""
manager = LogFilesManager()
files_list = manager.list_logfiles(LOG_FILES_DIR)
indexes = {}
if files_list:
count = 0
for index in files_list:
indexes[str(count)] = index
count += 1
return render_to_response(template_name, {'files_list': indexes,})
@login_required
@user_passes_test(lambda u: u.is_superuser)
def logfile_view(request, logfile_id, template_name='logfile.html'):
"""Returns a list of Log File content parsed by main regexp"""
page = request.GET.get('page')
if not page:
page = 1
else:
try:
page = int(page)
except ValueError:
page = 1
pass
manager = LogFilesManager()
files_list = manager.list_logfiles(LOG_FILES_DIR)
try:
filename = files_list[int(logfile_id)]
except Exception:
return HttpResponseBadRequest()
logfile = os.path.join(LOG_FILES_DIR, files_list[int(logfile_id)])
log_file_object = manager.get_file(logfile)
fake_log_file_lines = manager.get_file_lines_count(log_file_object)
paginator = Paginator(fake_log_file_lines, LOG_FILES_PAGINATE_LINES)
paginated_lines = paginator.page(page)
first_line = paginated_lines.object_list[0]
current_file_dict = manager.parse_log_file(logfile, first_line)
paginated_lines.object_list = current_file_dict
header_list = manager.compile_header_from_regexp()
context = {
'header_list': header_list,
'file_name': filename,
'file_id': logfile_id,
'paginator': paginated_lines,
'page': page,
}
return render_to_response(template_name, context)
@login_required
@user_passes_test(lambda u: u.is_superuser)
def logfile_to_csv(request, logfile_id):
"""Exports selected log file to PDF"""
manager = LogFilesManager()
files_list = manager.list_logfiles(LOG_FILES_DIR)
try:
filename = files_list[int(logfile_id)]
except Exception:
return HttpResponseBadRequest()
logfile = os.path.join(LOG_FILES_DIR, files_list[int(logfile_id)])
file_dict = manager.parse_log_file(logfile, 0, full=True)
# Constructing CSV file
header_list = manager.compile_header_from_regexp()
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % filename
writer = csv.writer(response)
if header_list:
writer.writerow(header_list)
if file_dict:
for item in file_dict:
row_list = []
for atom in item:
for cell in atom:
row_list += [unicode(cell).encode('utf8'),]
writer.writerow(row_list)
return response
@login_required
@user_passes_test(lambda u: u.is_superuser)
def logfile_to_log(request, logfile_id):
"""Exports selected log file to PDF"""
manager = LogFilesManager()
files_list = manager.list_logfiles(LOG_FILES_DIR)
try:
filename = files_list[int(logfile_id)]
except Exception:
return HttpResponseBadRequest()
logfile = os.path.join(LOG_FILES_DIR, files_list[int(logfile_id)])
logfile_content = '\n'.join(open(logfile, 'r').readlines())
response = HttpResponse(logfile_content, content_type='text/plain')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response