From 6018d7686ba1e8e4cae78273014cf17b2e72c5b3 Mon Sep 17 00:00:00 2001 From: Justin Merrell Date: Sat, 14 May 2022 23:32:42 +0000 Subject: [PATCH] feat(Scripting): Able to edit and delete scripts --- .../static/js/developer/custom_scripts.js | 62 ++++++++++++++++++- bb_dashboard/templates/developer.html | 57 ++++++++--------- .../developer/custom_script_table.html | 4 +- ...te-modal.html => modal_delete_script.html} | 7 ++- .../developer/modal_edit_script.html | 34 ++++++++++ .../templates/developer/modal_new_script.html | 2 +- bb_dashboard/urls.py | 4 +- bb_dashboard/views/__init__.py | 2 +- bb_dashboard/views/views_tab_developer.py | 55 +++++++++++++++- 9 files changed, 190 insertions(+), 37 deletions(-) rename bb_dashboard/templates/developer/{delete-modal.html => modal_delete_script.html} (63%) create mode 100644 bb_dashboard/templates/developer/modal_edit_script.html diff --git a/bb_dashboard/static/js/developer/custom_scripts.js b/bb_dashboard/static/js/developer/custom_scripts.js index e8a706f..6b1caa6 100644 --- a/bb_dashboard/static/js/developer/custom_scripts.js +++ b/bb_dashboard/static/js/developer/custom_scripts.js @@ -3,7 +3,9 @@ function AddScript() { $("#add_script").modal('toggle'); } -function DeleteScriptModal() { +function DeleteScriptModal(script_id, script_name) { + document.getElementById("delete_script_button").dataset.script_id = script_id; + document.getElementById("script_name_confirmation").innerHTML = "Deleting: " + script_name; $("#delete_script").modal('toggle'); } @@ -30,3 +32,61 @@ function AddNewScript() { xhttp.send(formData); } +/* ------------------------------- Edit Script ------------------------------ */ +function EditScript(script_id) { + var xhttp = new XMLHttpRequest(); + var url = '/dashboard/tab/developer/script_edit/'+script_id; + + xhttp.onload = function () { + var response = JSON.parse(this.responseText); + document.getElementById("update_script_button").dataset.script_id = script_id; + document.getElementById("edit_script_name").value = response['script_name']; + document.getElementById("edit_script_description").value = response['script_description']; + document.getElementById("edit_script_code").value = response['script_code']; + $("#edit_script").modal('toggle'); + }; + + xhttp.open('GET', url); + xhttp.send(); +} + +/* ------------------------------ Update Script ----------------------------- */ +function UpdateScript(script_id) { + var script_name = document.getElementById("edit_script_name").value; + var script_description = document.getElementById("edit_script_description").value; + var script_code = document.getElementById("edit_script_code").value; + + var formData = new FormData(); + var xhttp = new XMLHttpRequest(); + var url = '/dashboard/tab/developer/script_update' + + formData.append('script_id', script_id); + formData.append('script_name', script_name); + formData.append('script_description', script_description); + formData.append('script_code', script_code); + + xhttp.onload = function () { + $("#edit_script").modal('toggle'); + } + + xhttp.open('POST', url, true); + xhttp.setRequestHeader("X-CSRFToken", csrftoken); + xhttp.send(formData); +} + +/* ------------------------------ Delete Script ----------------------------- */ +function DeleteScript(script_id) { + var formData = new FormData(); + var xhttp = new XMLHttpRequest(); + var url = '/dashboard/tab/developer/script_delete' + + formData.append('script_id', script_id); + + xhttp.onload = function () { + $("#delete_script").modal('toggle'); + } + + xhttp.open('POST', url, true); + xhttp.setRequestHeader("X-CSRFToken", csrftoken); + xhttp.send(formData); +} diff --git a/bb_dashboard/templates/developer.html b/bb_dashboard/templates/developer.html index bf765ac..c94ad6d 100644 --- a/bb_dashboard/templates/developer.html +++ b/bb_dashboard/templates/developer.html @@ -11,48 +11,49 @@
-
-

Custom Scripts

-
- -
-
+
+

Custom Scripts

+
- +
+
- -
- - - - - - - - +
Script NameDescriptionEditDelete
- - - {% include "developer/custom_script_table.html" %} - + + + + + + + + + + -
Script NameDescriptionEditDelete
+ + + {% include "developer/custom_script_table.html" %} + -
-
+ - +
+ + - + + {% include "developer/modal_new_script.html" %} -{% include "developer/delete-modal.html" %} +{% include "developer/modal_delete_script.html" %} +{% include "developer/modal_edit_script.html" %} {% endblock content %} diff --git a/bb_dashboard/templates/developer/custom_script_table.html b/bb_dashboard/templates/developer/custom_script_table.html index 0fc0a38..185f8ad 100644 --- a/bb_dashboard/templates/developer/custom_script_table.html +++ b/bb_dashboard/templates/developer/custom_script_table.html @@ -6,13 +6,13 @@ - edit + edit - delete + delete diff --git a/bb_dashboard/templates/developer/delete-modal.html b/bb_dashboard/templates/developer/modal_delete_script.html similarity index 63% rename from bb_dashboard/templates/developer/delete-modal.html rename to bb_dashboard/templates/developer/modal_delete_script.html index dd6ad59..161dd7a 100644 --- a/bb_dashboard/templates/developer/delete-modal.html +++ b/bb_dashboard/templates/developer/modal_delete_script.html @@ -4,11 +4,14 @@ diff --git a/bb_dashboard/templates/developer/modal_edit_script.html b/bb_dashboard/templates/developer/modal_edit_script.html new file mode 100644 index 0000000..c03e40a --- /dev/null +++ b/bb_dashboard/templates/developer/modal_edit_script.html @@ -0,0 +1,34 @@ + diff --git a/bb_dashboard/templates/developer/modal_new_script.html b/bb_dashboard/templates/developer/modal_new_script.html index d99c297..b1be6f8 100644 --- a/bb_dashboard/templates/developer/modal_new_script.html +++ b/bb_dashboard/templates/developer/modal_new_script.html @@ -20,7 +20,7 @@ - + diff --git a/bb_dashboard/urls.py b/bb_dashboard/urls.py index 23db68e..83379a9 100644 --- a/bb_dashboard/urls.py +++ b/bb_dashboard/urls.py @@ -15,7 +15,9 @@ # ----------------------------------- Tabs ----------------------------------- # path('tab/developer/script_add/', views.add_new_script, name='add_new_script'), - + path('tab/developer/script_edit/', views.edit_script, name='edit_script'), + path('tab/developer/script_update', views.update_script, name='update_script'), + path('tab/developer/script_delete', views.delete_script, name='delete_script'), # ------------------- Backwards Compatability Page Handling ------------------ # re_path(r'^.*\.*', views.pages, name='pages'), diff --git a/bb_dashboard/views/__init__.py b/bb_dashboard/views/__init__.py index da8bbb9..5c7b030 100644 --- a/bb_dashboard/views/__init__.py +++ b/bb_dashboard/views/__init__.py @@ -4,4 +4,4 @@ from .views_colo import onboarding -from .views_tab_developer import add_new_script +from .views_tab_developer import add_new_script, edit_script, update_script, delete_script diff --git a/bb_dashboard/views/views_tab_developer.py b/bb_dashboard/views/views_tab_developer.py index dcea2e3..3d2bdfa 100644 --- a/bb_dashboard/views/views_tab_developer.py +++ b/bb_dashboard/views/views_tab_developer.py @@ -1,6 +1,6 @@ ''' bb_dashboard - views_tab_developer.py ''' -from django.http import HttpResponse +from django.http import HttpResponse, JsonResponse from django.contrib.auth.decorators import login_required from bb_data.models import CustomScript @@ -20,3 +20,56 @@ def add_new_script(request): new_script.save() return HttpResponse(status=200) + +@login_required +def edit_script(request, script_id): + ''' + URL: dashboard/tab/developer/script_edit// + Method: GET + Returns the script values to be edited + ''' + script = CustomScript.objects.get(id=script_id) + return JsonResponse( + { + 'script_name': script.name, + 'script_description': script.description, + 'script_code': script.script + }, status=200, safe=False) + +@login_required +def update_script(request): + ''' + URL: dashboard/tab/developer/script_update + Updates the script changes. + ''' + script_id = request.POST.get('script_id') + + script = CustomScript.objects.get(id=script_id) + + if script.user != request.user: + return HttpResponse("Unable to edit selected script", status=403) + + script.name = request.POST.get('script_name') + script.description = request.POST.get('script_description') + script.script = request.POST.get('script_code') + script.save() + + return HttpResponse(status=200) + +@login_required +def delete_script(request): + ''' + URL: dashboard/tab/developer/script_delete + Method: POST + Deletes the script. + ''' + script_id = request.POST.get('script_id') + + script = CustomScript.objects.get(id=script_id) + + if script.user != request.user: + return HttpResponse("Unable to delete selected script", status=403) + + script.delete() + + return HttpResponse(status=200)