From 93b771d4dcb515a8662611b135d13abe27c767ed Mon Sep 17 00:00:00 2001 From: coderick14 Date: Fri, 30 Jun 2017 20:14:49 +0530 Subject: Add feature to fetch logs based on date-time --- myadmin/urls.py | 5 +- myadmin/views.py | 45 ++++++++++- templates/admin/sub_nav.html | 1 + templates/admin/user_logs.html | 167 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 templates/admin/user_logs.html diff --git a/myadmin/urls.py b/myadmin/urls.py index 20e6311..7272f79 100644 --- a/myadmin/urls.py +++ b/myadmin/urls.py @@ -6,6 +6,7 @@ urlpatterns = [ url(r'^admin/?$', views.index, name='admin_index'), url(r'^admin/bookings/?$', views.booking_index, name='admin_bookings'), url(r'^admin/webcam/?$', views.webcam_index, name='admin_webcam'), + url(r'^admin/viewlogs/?$', views.logs_index, name='admin_viewlogs'), url(r'^admin/profile/([0-9]+)/?$', views.profile, name='admin_profile'), url(r'^admin/testing/?$', views.testing, name='admin_testing'), url(r'^admin/resetdevice/?$', views.reset_device, name='admin_reset_device'), @@ -17,6 +18,8 @@ urlpatterns = [ url(r'^admin/changemid/?$', views.get_allocated_mids, name='admin_getmids'), url(r'^admin/getusers/?$', views.get_users, name='admin_users'), url(r'^admin/toggledevice', views.toggle_device_status, name='toggle_status'), + url(r'^admin/rangelogs/?$', views.range_logs, name='admin_range_logs'), + url(r'^admin/explogs/([0-9]+)/?$', views.download_experiment_log, name='admin_explogs'), url(r'^admin/toggle_allotment_mode/?$', views.toggle_allotment_mode, name='admin_toggle_allotment_mode'), -] \ No newline at end of file +] diff --git a/myadmin/views.py b/myadmin/views.py index f0c811a..8e1372d 100644 --- a/myadmin/views.py +++ b/myadmin/views.py @@ -45,6 +45,12 @@ def webcam_index(req): boards = Board.objects.all() return render(req, 'admin/webcam_index.html', {"boards": boards}) +@login_required(redirect_field_name=None) +def logs_index(req): + checkadmin(req) + date = (datetime.datetime.now()).strftime("%Y-%m-%d") + return render(req, 'admin/user_logs.html', {"nowdate" : date}) + @login_required(redirect_field_name=None) def profile(req, mid): checkadmin(req) @@ -174,7 +180,7 @@ def toggle_device_status(req): return HttpResponse(json.dumps({"status_code":200, "message":"Toggle successful"}), content_type="application/json") except Exception as e: - return HttpResponse(json.dumps({"status_code":400, "message":"Unsuccessful"}), content_type="application/json") + return HttpResponse(json.dumps({"status_code":500, "message":"Unsuccessful"}), content_type="application/json") def user_exists(username): @@ -214,6 +220,43 @@ def download_log(req, mid): except: return HttpResponse("Requested log file doesn't exist.Please Try in the next hour after your slot ends.") +@login_required(redirect_field_name=None) +@csrf_exempt +def range_logs(req): + checkadmin(req) + try: + start_date = req.POST.get("start_date") + end_date = req.POST.get("end_date") + start_time = req.POST.get("start_time") + end_time = req.POST.get("end_time") + except: + return HttpResponse(json.dumps({"status_code":400, "message":"Invalid parameters"}), content_type="application/json") + + try: + start = start_date + " " + start_time + end = end_date + " " + end_time + log_files = Experiment.objects.filter(created_at__range=[start, end]).values("id", "log") + + return HttpResponse(json.dumps({"status_code":200, "message":list(log_files)}), content_type="application/json") + except Exception as e: + return HttpResponse(json.dumps({"status_code": 500, "message": "Some error occured" + str(e)}), content_type="application/json") + +@login_required(redirect_field_name=None) +@csrf_exempt +def download_experiment_log(req, experiment_id): + """ Downloads the experimental log file. + Input: req: request object, experiment_id: experimental id + Output: HttpResponse object + """ + checkadmin(req) + try: + experiment_data = Experiment.objects.select_related("booking", "booking__account").get(id=experiment_id) + f = open(os.path.join(settings.EXPERIMENT_LOGS_DIR, experiment_data.log), "r") + data = f.read() + f.close() + return HttpResponse(data, content_type='text/text') + except: + return HttpResponse("Requested log file doesn't exist.") @csrf_exempt def reset_device(req): diff --git a/templates/admin/sub_nav.html b/templates/admin/sub_nav.html index b493274..c77205b 100644 --- a/templates/admin/sub_nav.html +++ b/templates/admin/sub_nav.html @@ -3,4 +3,5 @@ Show all images Test boards Update MID +Fetch Logs

\ No newline at end of file diff --git a/templates/admin/user_logs.html b/templates/admin/user_logs.html new file mode 100644 index 0000000..4586b56 --- /dev/null +++ b/templates/admin/user_logs.html @@ -0,0 +1,167 @@ +{% extends "layout.html" %} +{% load staticfiles %} + +{% block headerfiles %} + + +{% endblock %} + +{% block content %} +
+
+ {% include "account/sub_nav.html" %} +
+ {% include "admin/sub_nav.html" %} + +

Filter log files

+
+ +
+ + +
+ + +
+ + +
+ + + + + + + +
+ +
+
+
+

+ + + + + + + + + + + +
Sl NoUserFilename
+
+ +{% endblock %} \ No newline at end of file -- cgit