diff --git a/instances/templates/instance.html b/instances/templates/instance.html index 1579da2..f548b02 100644 --- a/instances/templates/instance.html +++ b/instances/templates/instance.html @@ -84,8 +84,8 @@
  • - - {% trans "Graphs" %} + + {% trans "Stats" %}
  • @@ -1043,6 +1043,11 @@ {% trans "Real Time" %}
  • +
  • + + {% trans "Logs" %} + +
  • @@ -1089,6 +1094,23 @@ {% endfor %}
    +
    +
    + + + + + + + + + + + +
    {% trans "Date" %}{% trans "User" %}{% trans "Message" %}
    None ...
    +
    +
    +
    @@ -1467,4 +1489,19 @@ }); } + {% endblock %} diff --git a/logs/urls.py b/logs/urls.py index e579387..574405c 100644 --- a/logs/urls.py +++ b/logs/urls.py @@ -4,4 +4,5 @@ from . import views urlpatterns = [ url(r'^$', views.showlogs, name='showlogs'), url(r'^(?P[0-9]+)/$', views.showlogs, name='showlogspage'), + url(r'^vm_logs/(?P[\w\-\.]+)/$', views.vm_logs, name='vm_logs'), ] diff --git a/logs/views.py b/logs/views.py index 830925e..ab6ae1b 100644 --- a/logs/views.py +++ b/logs/views.py @@ -1,8 +1,11 @@ from django.shortcuts import render -from django.http import HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect from django.core.urlresolvers import reverse +from django.contrib.auth.decorators import login_required +from instances.models import Instance from logs.models import Logs from django.conf import settings +import json def addlogmsg(user, instance, message): @@ -14,15 +17,13 @@ def addlogmsg(user, instance, message): add_log_msg.save() +@login_required def showlogs(request, page=1): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) @@ -34,3 +35,27 @@ def showlogs(request, page=1): # TODO: remove last element from queryset, but do not affect database return render(request, 'showlogs.html', locals()) + +@login_required +def vm_logs(request, vname): + """ + :param request: + :param vm: + :return: + """ + + if not request.user.is_superuser: + return HttpResponseRedirect(reverse('index')) + + vm = Instance.objects.get(name=vname) + logs_ = Logs.objects.filter(instance=vm.name, date__gte=vm.created).order_by('-date') + logs = [] + for l in logs_: + log = {} + log['user'] = l.user + log['instance'] = l.instance + log['message'] = l.message + log['date'] = l.date.strftime('%x %X') + logs.append(log) + + return HttpResponse(json.dumps(logs))