diff --git a/hackIDE/models.py b/hackIDE/models.py index 1347cd2..21bf97d 100644 --- a/hackIDE/models.py +++ b/hackIDE/models.py @@ -24,3 +24,11 @@ class codes(Document): run_status_memory=StringField(required=True) run_status_output=StringField(required=True) run_status_stderr=StringField(required=True) + + +class Users(Document): + username = StringField(required=True) + email = StringField(required=True) + password = StringField(required=True) + code = ListField() + title = ListField() \ No newline at end of file diff --git a/hackIDE/static/hackIDE/js/custom.js b/hackIDE/static/hackIDE/js/custom.js index ce3ac60..c0eb8c2 100644 --- a/hackIDE/static/hackIDE/js/custom.js +++ b/hackIDE/static/hackIDE/js/custom.js @@ -312,24 +312,46 @@ $(document).ready(function(){ } - /** - * function to send AJAX request to 'run/' endpoint + * function to send AJAX request to 'run/' and 'savetoprofile/' endpoint * */ - function runCode(){ + + //check = 1 for running the code + // check = 0 for saving code to profile + + function test(check){ // if a run request is ongoing if(request_ongoing) return; - // hide previous compile/output results - $(".output-response-box").hide(); + if(check == 1) + { + // hide previous compile/output results + $(".output-response-box").hide(); - // Change button text when this method is called - $("#run-code").html("Running.."); + // Change button text when this method is called + $("#run-code").html("Running.."); + + RUN_URL = "run/"; + } + + + if(check == 0) + { + $("#error_save_title").html(""); + $("#save-code-profile").html("Saving"); + $("#title-save").html("Saving"); + // disable button when this method is called + $("#title-save").prop('disabled', true); + $("#save-code-profile").prop('disabled', true); + + var code_title = $('#code-title').val(); + RUN_URL = "savetoprofile/"; + + } - // disable button when this method is called $("#compile-code").prop('disabled', true); $("#run-code").prop('disabled', true); @@ -354,6 +376,11 @@ $(document).ready(function(){ input: input_given, csrfmiddlewaretoken: csrf_token }; + + if(check == 0) + { + run_data.code_title = code_title; + } // AJAX request to Django for running code with input $.ajax({ url: RUN_URL, @@ -363,76 +390,107 @@ $(document).ready(function(){ timeout: 10000, success: function(response){ request_ongoing = false; - - if(location.port == "") - $('#copy_code')[0].innerHTML = '' + window.location.hostname + '/code_id=' + response.code_id + '/'; - else - $('#copy_code')[0].innerHTML = '' + window.location.hostname + ':' + location.port +'/code_id=' + response.code_id + '/'; - - $('#copy_code').css({'display': 'initial'}); - - // Change button text when this method is called - $("#run-code").html("Hack(run) it!"); - - // enable button when this method is called + $("#compile-code").prop('disabled', false); $("#run-code").prop('disabled', false); - $("html, body").delay(500).animate({ - scrollTop: $('#show-results').offset().top - }, 1000); - - $(".output-response-box").show(); - $(".run-status").show(); - $(".time-sec").show(); - $(".memory-kb").show(); + + if(check == 0) + { + if ("error_save_title" in response){ + $("#error_save_title").html(response.error_save_title); + $("#title-save").html("Save"); + $("#save-code-profile").html("Save Code To Profile"); + $("#title-save").prop("disabled", false); + $("#save-code-profile").prop("disabled", false); + } + else + { + $("#save-code-profile").html("Save Code To Profile"); + $("#save-code-profile").prop('disabled', true); + $("#save-code-profile").html("Saved"); + $("#title-save").html("Saved"); + $("#code-title").val(""); + $("#title-save-modal").modal("toggle"); + } + } - if(response.compile_status == "OK"){ - if(response.run_status.status == "AC"){ - $(".output-io").show(); - $(".output-error-box").hide(); - $(".output-io-info").show(); - $(".compile-status").children(".value").html(response.compile_status); - $(".run-status").children(".value").html(response.run_status.status); - $(".time-sec").children(".value").html(response.run_status.time_used); - $(".memory-kb").children(".value").html(response.run_status.memory_used); - $(".output-o").html(response.run_status.output_html); - $(".output-i").html(input_given); + else if(check == 1) + { + if(location.port == "") + $('#copy_code')[0].innerHTML = '' + window.location.hostname + '/code_id=' + response.code_id + '/'; + else + $('#copy_code')[0].innerHTML = '' + window.location.hostname + ':' + location.port +'/code_id=' + response.code_id + '/'; + + $('#copy_code').css({'display': 'initial'}); + + // Change button text when this method is called + $("#run-code").html("Hack(run) it!"); + + // enable button when this method is called + $("html, body").delay(500).animate({ + scrollTop: $('#show-results').offset().top + }, 1000); + + $(".output-response-box").show(); + $(".run-status").show(); + $(".time-sec").show(); + $(".memory-kb").show(); + + if(response.compile_status == "OK"){ + if(response.run_status.status == "AC"){ + $(".output-io").show(); + $(".output-error-box").hide(); + $(".output-io-info").show(); + $(".compile-status").children(".value").html(response.compile_status); + $(".run-status").children(".value").html(response.run_status.status); + $(".time-sec").children(".value").html(response.run_status.time_used); + $(".memory-kb").children(".value").html(response.run_status.memory_used); + $(".output-o").html(response.run_status.output_html); + $(".output-i").html(input_given); + } + else{ + $(".output-io").show(); + $(".output-io-info").hide(); + $(".output-error-box").show(); + $(".compile-status").children(".value").html(response.compile_status); + $(".run-status").children(".value").html(response.run_status.status); + $(".time-sec").children(".value").html(response.run_status.time_used); + $(".memory-kb").children(".value").html(response.run_status.memory_used); + $(".error-key").html("Run-time error (stderr)"); + $(".error-message").html(response.run_status.stderr); + } } else{ $(".output-io").show(); $(".output-io-info").hide(); - $(".output-error-box").show(); - $(".compile-status").children(".value").html(response.compile_status); - $(".run-status").children(".value").html(response.run_status.status); - $(".time-sec").children(".value").html(response.run_status.time_used); - $(".memory-kb").children(".value").html(response.run_status.memory_used); - $(".error-key").html("Run-time error (stderr)"); - $(".error-message").html(response.run_status.stderr); + $(".compile-status").children(".value").html("--"); + $(".run-status").children(".value").html("CE"); + $(".time-sec").children(".value").html("0.0"); + $(".memory-kb").children(".value").html("0"); + $(".error-key").html("Compile error"); + $(".error-message").html(response.compile_status); } } - else{ - $(".output-io").show(); - $(".output-io-info").hide(); - $(".compile-status").children(".value").html("--"); - $(".run-status").children(".value").html("CE"); - $(".time-sec").children(".value").html("0.0"); - $(".memory-kb").children(".value").html("0"); - $(".error-key").html("Compile error"); - $(".error-message").html(response.compile_status); - } }, error: function(error){ request_ongoing = false; - - // Change button text when this method is called - $("#run-code").html("Hack(run) it!"); - - // enable button when this method is called $("#compile-code").prop('disabled', false); $("#run-code").prop('disabled', false); + if(check == 0) + { + $("#save-code-profile").html("Save Code To Profile"); + // enable button when this method is called + $("#save-code-profile").prop('disabled', false); + } + + else if(check == 1) + { + // Change button text when this method is called + $("#run-code").html("Hack(run) it!"); + $("html, body").delay(500).animate({ scrollTop: $('#show-results').offset().top }, 1000); @@ -450,6 +508,7 @@ $(document).ready(function(){ $(".memory-kb").children(".value").html("0"); $(".error-key").html("Server error"); $(".error-message").html("Server couldn't complete request. Please try again!"); + } } }); } @@ -459,6 +518,14 @@ $(document).ready(function(){ lang: languageSelected, csrfmiddlewaretoken: csrf_token }; + + RUN_URL = "run/" + + if(check == 0) + { + run_data.code_title = code_title; + RUN_URL = "savetoprofile/"; + } // AJAX request to Django for running code without input\ var timeout_ms = 10000; $.ajax({ @@ -468,107 +535,141 @@ $(document).ready(function(){ dataType: "json", timeout: timeout_ms, success: function(response){ - if(location.port == "") - $('#copy_code')[0].innerHTML = '' + window.location.hostname + '/code_id=' + response.code_id + '/'; - else - $('#copy_code')[0].innerHTML = '' + window.location.hostname + ':' + location.port +'/code_id=' + response.code_id + '/'; - - $('#copy_code').css({'display': 'initial'}); - request_ongoing = false; - - // Change button text when this method is called - $("#run-code").html("Hack(run) it!"); - - // enable button when this method is called $("#compile-code").prop('disabled', false); $("#run-code").prop('disabled', false); - $("html, body").delay(500).animate({ - scrollTop: $('#show-results').offset().top - }, 1000); + if(check == 0) + { + if ("error_save_title" in response){ + $("#error_save_title").html(response.error_save_title); + $("#title-save").html("Save"); + $("#save-code-profile").html("Save Code To Profile"); + + $("#title-save").prop("disabled", false); + $("#save-code-profile").prop("disabled", false); + } + else + { - $(".output-response-box").show(); - $(".run-status").show(); - $(".time-sec").show(); - $(".memory-kb").show(); + $("#save-code-profile").html("Save Code To Profile"); + $("#save-code-profile").prop('disabled', true); + $("#save-code-profile").html("Saved"); - if(response.compile_status == "OK"){ - if(response.run_status.status == "AC"){ - $(".output-io").show(); - $(".output-error-box").hide(); - $(".output-io-info").show(); - $(".output-i-info").hide(); - $(".compile-status").children(".value").html(response.compile_status); - $(".run-status").children(".value").html(response.run_status.status); - $(".time-sec").children(".value").html(response.run_status.time_used); - $(".memory-kb").children(".value").html(response.run_status.memory_used); - $(".output-o").html(response.run_status.output_html); + $("#title-save").html("Saved"); + $("#code-title").val(""); + + $("#title-save-modal").modal("toggle"); + } + } + + else if(check == 1) + { + + if(location.port == "") + $('#copy_code')[0].innerHTML = '' + window.location.hostname + '/code_id=' + response.code_id + '/'; + else + $('#copy_code')[0].innerHTML = '' + window.location.hostname + ':' + location.port +'/code_id=' + response.code_id + '/'; + + $('#copy_code').css({'display': 'initial'}); + + // Change button text when this method is called + $("#run-code").html("Hack(run) it!"); + + $("html, body").delay(500).animate({ + scrollTop: $('#show-results').offset().top + }, 1000); + + $(".output-response-box").show(); + $(".run-status").show(); + $(".time-sec").show(); + $(".memory-kb").show(); + + if(response.compile_status == "OK"){ + if(response.run_status.status == "AC"){ + $(".output-io").show(); + $(".output-error-box").hide(); + $(".output-io-info").show(); + $(".output-i-info").hide(); + $(".compile-status").children(".value").html(response.compile_status); + $(".run-status").children(".value").html(response.run_status.status); + $(".time-sec").children(".value").html(response.run_status.time_used); + $(".memory-kb").children(".value").html(response.run_status.memory_used); + $(".output-o").html(response.run_status.output_html); + } + else{ + $(".output-io").show(); + $(".output-io-info").hide(); + $(".output-error-box").show(); + $(".compile-status").children(".value").html(response.compile_status); + $(".run-status").children(".value").html(response.run_status.status); + $(".time-sec").children(".value").html(response.run_status.time_used); + $(".memory-kb").children(".value").html(response.run_status.memory_used); + + if (response.run_status.status == "TLE"){ + // Timeout error + $(".error-key").html("Timeout error"); + $(".error-message").html("Time limit exceeded."); + } else if(response.run_status.status == "MLE"){ + // Memory Limit Exceeded + $(".error-key").html("Memory limit error"); + $(".error-message").html("Memory limit exceeded"); + } + else { + // General stack error + $(".error-key").html("Run-time error (stderr)"); + $(".error-message").html(response.run_status.stderr); + } + } } else{ $(".output-io").show(); $(".output-io-info").hide(); - $(".output-error-box").show(); - $(".compile-status").children(".value").html(response.compile_status); - $(".run-status").children(".value").html(response.run_status.status); - $(".time-sec").children(".value").html(response.run_status.time_used); - $(".memory-kb").children(".value").html(response.run_status.memory_used); - - if (response.run_status.status == "TLE"){ - // Timeout error - $(".error-key").html("Timeout error"); - $(".error-message").html("Time limit exceeded."); - } else if(response.run_status.status == "MLE"){ - // Memory Limit Exceeded - $(".error-key").html("Memory limit error"); - $(".error-message").html("Memory limit exceeded"); - } - else { - // General stack error - $(".error-key").html("Run-time error (stderr)"); - $(".error-message").html(response.run_status.stderr); - } + $(".compile-status").children(".value").html("--"); + $(".run-status").children(".value").html("CE"); + $(".time-sec").children(".value").html("0.0"); + $(".memory-kb").children(".value").html("0"); + $(".error-key").html("Compile error"); + $(".error-message").html(response.compile_status); } } - else{ - $(".output-io").show(); - $(".output-io-info").hide(); - $(".compile-status").children(".value").html("--"); - $(".run-status").children(".value").html("CE"); - $(".time-sec").children(".value").html("0.0"); - $(".memory-kb").children(".value").html("0"); - $(".error-key").html("Compile error"); - $(".error-message").html(response.compile_status); - } }, error: function(error){ request_ongoing = false; - - // Change button text when this method is called - $("#run-code").html("Hack(run) it!"); - - // enable button when this method is called $("#compile-code").prop('disabled', false); $("#run-code").prop('disabled', false); - $("html, body").delay(500).animate({ - scrollTop: $('#show-results').offset().top - }, 1000); + if(check == 0) + { + $("#save-code-profile").html("Save Code To Profile"); + // enable button when this method is called + $("#save-code-profile").prop('disabled', false); + } - $(".output-response-box").show(); - $(".run-status").show(); - $(".time-sec").show(); - $(".memory-kb").show(); + else if(check == 1) + { + // Change button text when this method is called + $("#run-code").html("Hack(run) it!"); - $(".output-io").show(); - $(".output-io-info").hide(); - $(".compile-status").children(".value").html("--"); - $(".run-status").children(".value").html("--"); - $(".time-sec").children(".value").html("0.0"); - $(".memory-kb").children(".value").html("0"); - $(".error-key").html("Server error"); - $(".error-message").html("Server couldn't complete request. Please try again!"); + $("html, body").delay(500).animate({ + scrollTop: $('#show-results').offset().top + }, 1000); + + $(".output-response-box").show(); + $(".run-status").show(); + $(".time-sec").show(); + $(".memory-kb").show(); + + $(".output-io").show(); + $(".output-io-info").hide(); + $(".compile-status").children(".value").html("--"); + $(".run-status").children(".value").html("--"); + $(".time-sec").children(".value").html("0.0"); + $(".memory-kb").children(".value").html("0"); + $(".error-key").html("Server error"); + $(".error-message").html("Server couldn't complete request. Please try again!"); + } } }); } @@ -576,6 +677,12 @@ $(document).ready(function(){ } + +$('#title-save').click(function(){ + test(0); +}); + + // when show-settings is clicked $("#show-settings").click(function(event){ @@ -739,9 +846,7 @@ $(document).ready(function(){ // when run-code is clicked $("#run-code").click(function(){ - - runCode(); - + test(1); }); // check if input box is to be show @@ -751,4 +856,150 @@ $(document).ready(function(){ } + $("#register").click(function(){ + var username = $('#signup_username').val(); + var email = $("#signup_email").val(); + var password = $("#signup_password").val(); + + $("#register").html("Registering"); + $("#register").prop('disabled', true); + + var csrf_token = $(":input[name='csrfmiddlewaretoken']").val(); + var form_data = {username : username, email : email, password : password, csrfmiddlewaretoken : csrf_token}; + $.ajax({ + url : 'register/', + type : 'POST', + data : form_data, + dataType : 'json', + timeout : '10000', + success : function(response){ + $("#error_username").html(response.error_username); + $("#error_email").html(response.error_email); + $("#error_password").html(response.error_password); + + $("#register").prop('disabled', false); + $("#register").html("Register"); + + $("#msg").html(response.msg); + if (response.error_username == "" && response.error_email == "" && response.error_password == "") + { + $("#register").html("Registered"); + location.reload(); + } + }, + }); + }); + + $('#login').click(function(){ + var username = $('#login_username').val(); + var password = $("#login_password").val(); + + $("#login").html("Logging In"); + $("#login").prop("disabled", true); + var csrf_token = $(":input[name='csrfmiddlewaretoken']").val(); + + var login_form_data = {username : username, password : password, csrfmiddlewaretoken : csrf_token}; + + $.ajax({ + url : 'login/', + type : 'POST', + data : login_form_data, + dataType : 'json', + timeout : 10000, + success : function(response){ + + $("#login").prop("disabled", false); + $("#login").html("Login"); + + $('#login_msg').html(response.msg); + if(response.msg != "Invalid credentials") + { + $("#login").html("Logged In Sucessfully"); + location.reload(); + } + + }, + }); + }); + + $("#logout").click(function(){ + var csrf_token = $(":input[name='csrfmiddlewaretoken']").val(); + logout_data = {csrfmiddlewaretoken:csrf_token} + + $.ajax({ + url : 'logout/', + type : 'POST', + data : logout_data, + dataType : 'json', + timeout : 10000, + success : function(response){ + location.reload(); + }, + }); + }); + + + $("#data_table").on('click', '#close_data', function(){ + + var i = $(this).closest('tr').attr('id'); + var parent = $(this).closest('tr'); + var csrf_token = $(":input[name='csrfmiddlewaretoken']").val(); + var remove_data = {csrfmiddlewaretoken:csrf_token, id:i}; + + $.ajax({ + url: 'removecode/', + type: "POST", + data: remove_data, + dataType: "json", + timeout: 10000, + success : function(response){ + parent.fadeOut('slow', function(){ + parent.remove(); + }); + }, + }); + }); + + $("#profile_btn").click(function(){ + var csrf_token = $(":input[name='csrfmiddlewaretoken']").val(); + + var profile_data = {csrfmiddlewaretoken:csrf_token}; + $.ajax({ + url: 'displayprofile/', + type: "POST", + data: profile_data, + dataType: "json", + timeout: 10000, + success : function(response){ + $("#data_table").html("Code IdTitle"); + if(location.port == "") + { + for(var i=0;i"+response.code_id[i]+""+ response.code_title[i] +"Open"); + } + } + else + { + for(var i=0;i"+response.code_id[i]+""+ response.code_title[i] +"Open"); + } + } + }, + + }); + + }); + + + editor.on('change', function(){ + $("#title-save").prop('disabled', false); + $("#title-save").html("Save"); + + $("#save-code-profile").prop('disabled', false); + $("#save-code-profile").html("Save Code To Profile"); + }); }); diff --git a/hackIDE/templates/hackIDE/index.html b/hackIDE/templates/hackIDE/index.html index 0da61ca..eab9c2e 100644 --- a/hackIDE/templates/hackIDE/index.html +++ b/hackIDE/templates/hackIDE/index.html @@ -33,14 +33,111 @@ - + @@ -76,7 +173,7 @@ -
+-
+
+ +
+ + + {% endif %} @@ -250,6 +368,7 @@ + \ No newline at end of file diff --git a/hackIDE/urls.py b/hackIDE/urls.py index 2b63000..ebe3808 100644 --- a/hackIDE/urls.py +++ b/hackIDE/urls.py @@ -18,6 +18,12 @@ url(r'^compile/$', views.compileCode, name='compile'), # ex: /run/ url(r'^run/$', views.runCode, name='run'), + url(r'^register/$', views.register, name='register'), + url(r'^login/$', views.login, name='login'), + url(r'^logout/$', views.logout, name='logout'), + url(r'^savetoprofile/$', views.savetoprofile, name='savetoprofile'), + url(r'^displayprofile/$', views.displayprofile, name='displayprofile'), + url(r'^removecode/$', views.removecode, name='removecode'), # ex: /code=ajSkHb/ - url(r'(?P\w{0,50})/$', views.savedCodeView, name='saved-code'), + url(r'code_id=((?P\w{0,50}))/$', views.savedCodeView, name='saved-code'), ] diff --git a/hackIDE/views.py b/hackIDE/views.py index 7501ad4..75edd3a 100644 --- a/hackIDE/views.py +++ b/hackIDE/views.py @@ -8,7 +8,9 @@ from django.shortcuts import render from django.http import JsonResponse, HttpResponseForbidden -from models import codes +from models import codes, Users +from passlib.hash import sha256_crypt +import re import requests, json, os @@ -61,7 +63,11 @@ def missing_argument_error(): """ def index(request): # render the index.html - return render(request, 'hackIDE/index.html', {}) + logged_in = False + if request.session.has_key('username'): + logged_in = True + + return render(request, 'hackIDE/index.html', {'logged_in':logged_in,}) """ @@ -221,3 +227,229 @@ def savedCodeView(request, code_id): 'run_status_memory': run_status_memory, 'run_status_stderr': run_status_status }) + + +def register(request): + if request.is_ajax(): + username = request.POST['username'] + email = request.POST['email'] + error_username = "" + error_email = "" + error_password = "" + flag = False + + if len(username) < 8: + error_username = "Username should be atleast 8 characters." + flag = True + if len(request.POST['password'])< 8: + error_password = "Password should be atleast 8 characters." + flag = True + if not re.search('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', email): + print "email invalid" + flag = True + + + password = sha256_crypt.encrypt(str(request.POST['password'])) + users = Users.objects() + msg = "" + + for user in users: + if username == user.username: + error_username = "Username already exist" + flag = True + if email == user.email: + error_email = "Email already exist" + flag = True + + if flag == False: + new_user = Users.objects.create(username=username, email=email, password=password, code=[], title=[]) + new_user.save() + request.session['username'] = username + + r = {'msg':msg, 'error_username':error_username, 'error_email':error_email, 'error_password':error_password} + return JsonResponse(r, safe=False) + + else: + return HttpResponseForbidden() + + +def login(request): + if request.is_ajax(): + try: + username = request.POST['username'] + password = request.POST['password'] + + users = Users.objects() + + msg = "Invalid credentials" + + for user in users: + if username == user.username and sha256_crypt.verify(str(password), user.password): + msg = "Successfully logged in" + request.session['username'] = username + + r = {'msg':msg, 'username' : username} + return JsonResponse(r, safe=False) + + except Exception as e: + print e + + else: + return HttpResponseForbidden() + + +def logout(request): + if request.is_ajax(): + del request.session['username'] + msg = "Successfully Logged out" + r = {'msg':msg} + return JsonResponse(r, safe=False) + else: + return HttpResponseForbidden() + + +def savetoprofile(request): + if request.is_ajax(): + if len(request.POST['code_title'])>0: + try: + source = request.POST['source'] + # Handle Empty Source Case + source_empty_check(source) + + lang = request.POST['lang'] + # Handle Invalid Language Case + lang_valid_check(lang) + + except KeyError: + # Handle case when at least one of the keys (lang or source) is absent + missing_argument_error() + + else: + # default value of 5 sec, if not set + time_limit = request.POST.get('time_limit', 5) + # default value of 262144KB (256MB), if not set + memory_limit = request.POST.get('memory_limit', 262144) + + run_data = { + 'client_secret': CLIENT_SECRET, + 'async': 0, + 'source': source, + 'lang': lang, + 'time_limit': time_limit, + 'memory_limit': memory_limit, + } + + # if input is present in the request + code_input = "" + if 'input' in request.POST: + run_data['input'] = request.POST['input'] + code_input = run_data['input'] + + """ + Make call to /run/ endpoint of HackerEarth API + and save code and result in database + """ + r = requests.post(RUN_URL, data=run_data) + r = r.json() + cs = "" + rss = "" + rst = "" + rsm = "" + rso = "" + rsstdr = "" + try: + cs = r['compile_status'] + except: + pass + try: + rss=r['run_status']['status'] + except: + pass + try: + rst = r['run_status']['time_used'] + except: + pass + try: + rsm = r['run_status']['memory_used'] + except: + pass + try: + rso = r['run_status']['output_html'] + except: + pass + try: + rsstdr = r['run_status']['stderr'] + except: + pass + + code_response = codes.objects.create( + code_id = r['code_id'], + code_content = source, + lang = lang, + code_input = code_input, + compile_status = cs, + run_status_status = rss, + run_status_time = rst, + run_status_memory = rsm, + run_status_output = rso, + run_status_stderr = rsstdr + ) + code_response.save() + + username = request.session['username'] + user = Users.objects.get(username = username) + user.code.insert(0, str(r['code_id'])) + + code_title = request.POST['code_title'] + user.title.insert(0, code_title) + + user.save() + + return JsonResponse(r, safe=False) + + else: + return JsonResponse({'error_save_title' : "Field cannot be Empty"}, safe=False) + else: + return HttpResponseForbidden() + + +def displayprofile(request): + if request.is_ajax(): + try: + username = request.session['username'] + user = Users.objects.get(username=username) + code_id = user.code + r = {'code_id':code_id, 'code_title':user.title} + + return JsonResponse(r, safe=False) + + except Exception as e: + print e + + else: + return HttpResponseForbidden() + + +def removecode(request): + if request.is_ajax(): + try: + username = request.session['username'] + user = Users.objects.get(username=username) + code_id = user.code + + for i in range(0, len(code_id)): + if code_id[i] == str(request.POST['id']): + user.code.pop(i) + user.title.pop(i) + break + + user.save() + + r = {} + return JsonResponse(r, safe=False) + except Exception as e: + print e + + else: + return HttpResponseForbidden() + diff --git a/hackIDE_project/settings.py b/hackIDE_project/settings.py index 1bf96a5..669e857 100644 --- a/hackIDE_project/settings.py +++ b/hackIDE_project/settings.py @@ -46,7 +46,7 @@ MIDDLEWARE_CLASSES = [ # 'django.middleware.security.SecurityMiddleware', - # 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', @@ -138,3 +138,5 @@ # Simplified static file serving. # https://warehouse.python.org/project/whitenoise/ STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' + +SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies' diff --git a/requirements.txt b/requirements.txt index 1c64f9d..e22da94 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,6 +12,7 @@ decorator==3.4.0 django-cors-headers==1.1.0 gunicorn==19.4.5 oauthlib==0.6.1 +passlib==1.6.5 pycurl==7.19.3 requests==2.2.1 simplejson==3.3.1