diff options
-rw-r--r-- | myadmin/urls.py | 1 | ||||
-rw-r--r-- | myadmin/views.py | 36 | ||||
-rw-r--r-- | templates/admin/changeMID.html | 52 |
3 files changed, 79 insertions, 10 deletions
diff --git a/myadmin/urls.py b/myadmin/urls.py index 9626c51..68e7485 100644 --- a/myadmin/urls.py +++ b/myadmin/urls.py @@ -15,6 +15,7 @@ urlpatterns = [ url(r'^admin/logs/([0-9]+)/?$', views.download_log, name='admin_logs'), url(r'^admin/updatemid/?$', views.update_allocated_mid, name='admin_updatemid'), url(r'^admin/changemid/?$', views.get_allocated_mids, name='admin_getmids'), + url(r'^admin/getusers/?$', views.get_users, name='admin_users'), 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 97a23c3..9054524 100644 --- a/myadmin/views.py +++ b/myadmin/views.py @@ -6,7 +6,7 @@ from django.db.models import Count from django.core.exceptions import ObjectDoesNotExist from sbhs_server.tables.models import Board, Booking, Slot, Experiment, Account from sbhs_server import settings,sbhs -import subprocess,json,serial,os, datetime +import subprocess,json,serial,os, datetime, re # Create your views here. def checkadmin(req): @@ -16,7 +16,7 @@ def checkadmin(req): @login_required(redirect_field_name=None) def index(req): checkadmin(req) - boards = Board.objects.order_by('online').all() + boards = Board.objects.order_by('-online').all() allotment_mode = "Random" if Board.can_do_random_allotment() else "Workshop" return render(req, 'admin/index.html', {"boards": boards, "allotment_mode": allotment_mode}) @@ -117,15 +117,24 @@ def monitor_experiment(req): except Exception as e: return HttpResponse(json.dumps({"status_code":400, "message":"Invalid MID"}), content_type="application/json") - current_booking_id, current_user = current_booking.id, current_booking.account.username + try: + current_booking_id, current_user = current_booking.id, current_booking.account.username + + logfile = Experiment.objects.filter(booking_id=current_booking_id).order_by('created_at').reverse()[0].log - logfile = Experiment.objects.get(booking_id=current_booking_id).log + # get last 20 lines from logs + stdin,stdout = os.popen2("tail -n 20 "+logfile) + stdin.close() + logs = stdout.readlines(); stdout.close() + regex = re.compile(r"^\d+\.\d+ \d{1,3} \d{1,3} \d{1,3}\.?\d+$") + screened_logs = [] + for line in logs: + if regex.match(line): + screened_logs.append(line) - # get last 50 lines from logs - stdin,stdout = os.popen2("tail -n 10 "+logfile) - stdin.close() - logs = stdout.readlines(); stdout.close() - logs = "".join(logs) + logs = "".join(screened_logs) + except Exception as e: + return HttpResponse(json.dumps({"status_code":500, "message":str(e)}), content_type="application/json") data = {"user": current_user, "logs": logs} return HttpResponse(json.dumps({"status_code":200, "message":data}), content_type="application/json") @@ -136,6 +145,15 @@ def get_allocated_mids(req): mid_count = Account.objects.select_related().filter(board__online=1).values('board__mid', 'board_id').annotate(mcount=Count('board_id')).order_by('mcount') return render(req, 'admin/changeMID.html', {"mid_count" : mid_count}) +@csrf_exempt +def get_users(req): + checkadmin(req) + try: + users = list(Account.objects.values_list("username", flat=True)) + return HttpResponse(json.dumps({"status_code":200, "message":users}), content_type="application/json") + except Exception as e: + return HttpResponse(json.dumps({"status_code":500, "message":str(e)}), content_type="application/json") + def user_exists(username): try: user = Account.objects.get(username=username) diff --git a/templates/admin/changeMID.html b/templates/admin/changeMID.html index 5aff464..721a6b8 100644 --- a/templates/admin/changeMID.html +++ b/templates/admin/changeMID.html @@ -10,7 +10,8 @@ Username: <br> - <input id="username" type="text" placeholder="Enter Username"> + <input type="text" id="username" list="users" placeholder="Enter Username"> + <datalist id="users"></datalist> <br><br> @@ -63,6 +64,55 @@ } + // for live search of usernames + if (sessionStorage.getItem("fetchedUsers") == null) { + var BASE_URL = window.location.origin; + var request = $.ajax({ + url: BASE_URL + '/admin/getusers', + method: 'POST' + }); + + request.done(function(data){ + if (data.status_code == 200) { + sessionStorage.setItem("fetchedUsers", true); + populateUsers(data.message); + appendUsers(); + } + else { + console.log("Sorry. Unable to implement live search"); + } + }); + } + + function populateUsers(data) { + for (username of data) { + sessionStorage.setItem("sbhsuser"+username, username); + } + } + + function appendUsers() { + if (sessionStorage.getItem("fetchedUsers")) { + var i, matchedUsers = []; + query = "sbhsuser"; + for (i in sessionStorage) { + if (sessionStorage.hasOwnProperty(i)) { + if (i.match(query)) { + value = sessionStorage.getItem(i); + matchedUsers.push(value); + } + } + } + options = ""; + for (matchedUser of matchedUsers) { + options += "<option>" + matchedUser + "</option>"; + } + if (options.length != 0) { + document.getElementById("users").innerHTML = options; + } + } + } + + appendUsers(); </script> </div> {% endblock %}
\ No newline at end of file |