1
1
# -*- coding: utf-8 -*-
2
- from django .http import HttpResponseRedirect
2
+ from django .http import HttpResponseRedirect , HttpResponse
3
3
from django .views .generic import CreateView , ListView , UpdateView , DeleteView
4
4
from django .views .generic .base import TemplateResponseMixin , ContextMixin , View
5
5
from apps .pinger .forms import UploadFileForm , HaystackCreateModelForm
6
6
from apps .pinger .models import Haystack
7
7
from django .core .urlresolvers import reverse_lazy
8
8
from apps .util .mixins import MessageMixin
9
+ from settings .common import LOGFILE
10
+
9
11
10
12
class HaystackUploadView (TemplateResponseMixin , ContextMixin , View ):
11
13
form_class = UploadFileForm
@@ -40,26 +42,31 @@ def line_to_haystack(self, line):
40
42
)
41
43
straw .save ()
42
44
45
+
43
46
class HaystackListView (ListView ):
44
47
model = Haystack
45
48
49
+
46
50
class HaystackCreateView (MessageMixin , CreateView ):
47
51
model = Haystack
48
52
form_class = HaystackCreateModelForm
49
53
success_message = 'Added new item!'
50
54
success_url = reverse_lazy ('hay_list' )
51
55
56
+
52
57
class HaystackUpdateView (MessageMixin , UpdateView ):
53
58
model = Haystack
54
59
form_class = HaystackCreateModelForm
55
60
success_message = 'Data was successfully edited'
56
61
success_url = reverse_lazy ('hay_list' )
57
62
63
+
58
64
class HaystackDeleteView (MessageMixin , DeleteView ):
59
65
model = Haystack
60
66
success_message = 'Link was removed from monitoring system.'
61
67
success_url = reverse_lazy ('hay_list' )
62
68
69
+
63
70
class HaystackRunView (View ):
64
71
success_url = reverse_lazy ('hay_list' )
65
72
@@ -69,3 +76,18 @@ def get(self, request, *args, **kwargs):
69
76
pinger .run ()
70
77
return HttpResponseRedirect (self .success_url )
71
78
79
+
80
+ class HaystackLogView (View ):
81
+
82
+ def get (self , request , * args , ** kwargs ):
83
+ num = int (kwargs .pop ('num' ) or 5 )
84
+
85
+ from apps .util .util import tail
86
+
87
+ with open (LOGFILE , 'r' ) as f :
88
+ data , more = tail (f , num )
89
+ if more :
90
+ data = 'Showing last %d lines. Full og file avialbale at: %s\n \n %s' % (num , LOGFILE , data )
91
+ response = HttpResponse (data , content_type = 'text/plain' )
92
+
93
+ return response
0 commit comments