From 3f4c3f8d938f6a5bbda235dd7bc8f41e828878aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=87a=C4=9Fatay=20Onur=20=C5=9Eeng=C3=B6r?= Date: Fri, 5 Nov 2021 15:17:36 +0300 Subject: [PATCH] Add Project Settings view This view is accessible in the Files view and allows users to change the TMs and TBs associated with projects --- app/src/index.css | 7 +++- app/src/index.html | 27 +++++++++++++ app/src/index.js | 89 +++++++++++++++++++++++++++++++++++++++++-- backend/api/models.py | 16 ++++++++ backend/api/views.py | 30 +++++++++++++++ 5 files changed, 164 insertions(+), 5 deletions(-) diff --git a/app/src/index.css b/app/src/index.css index da5792b..c3de974 100644 --- a/app/src/index.css +++ b/app/src/index.css @@ -135,13 +135,13 @@ header#projects-header, header#settings { input { padding: 0.25em; } -main#editor-view, main#files-view, main#mysql-view, main#projects-view, main#tb-view, main#tm-view, main#overlay { +main#editor-view, main#files-view, main#mysql-view, main#projects-view, main#project-settings-view, main#tb-view, main#tm-view, main#overlay { background-color: #f5f5f5; grid-column: 2; grid-row: 2; overflow: hidden; } -main#editor-view, main#files-view, main#mysql-view, main#tb-view, main#tm-view, main#overlay { +main#editor-view, main#files-view, main#mysql-view, main#project-settings-view, main#tb-view, main#tm-view, main#overlay { display: none; } main#editor-view, main#tb-view, main#tm-view { @@ -161,6 +161,9 @@ main#overlay { top: 0px; width: 100vw; } +main#project-settings-view table { + width: auto; +} main#projects-view { overflow-y: scroll; } diff --git a/app/src/index.html b/app/src/index.html index 085be69..89c5df4 100644 --- a/app/src/index.html +++ b/app/src/index.html @@ -38,6 +38,7 @@ +
@@ -67,6 +68,32 @@
+
+
+
+ + + + + + + + + + + + +
+ + + +
+ + + +
+
+
diff --git a/app/src/index.js b/app/src/index.js index 5975769..1726406 100644 --- a/app/src/index.js +++ b/app/src/index.js @@ -594,6 +594,10 @@ function fireOnReady() { document.getElementById("btn-mysql-view").onclick = function() { toggleView("mysql-view", "block", "mysql-header", "btn-mysql-view"); } + document.getElementById("btn-project-settings").onclick = function() { + toggleView("project-settings-view", "block", "project-settings-header", null); + getRelevantKDBs(); + } function setFooter() { let footerString; @@ -614,13 +618,92 @@ function fireOnReady() { function toggleView(viewId, viewDisplay, headerId, buttonId) { activeView.style.display = "none"; activeHeader.style.display = "none"; - activeButton.classList.remove("active"); + if (activeButton) + { + activeButton.classList.remove("active"); + } activeView = document.getElementById(viewId); activeView.style.display = viewDisplay; activeHeader = document.getElementById(headerId); activeHeader.style.display = "block"; activeButton = document.getElementById(buttonId); - activeButton.classList.add("active"); + if (buttonId) + { + activeButton.classList.add("active"); + } + } + + function getRelevantKDBs() { + let xhttp = new XMLHttpRequest(); + + let queryURL = "http://127.0.0.1:8000/project/" + + filesView.getAttribute("cur-p-id") + + "?task=get_relevant_kdbs"; + + xhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + relevantKDBs = JSON.parse(this.responseText); + let projectTMSelect = document.getElementById("select-project-settings-tm"); + projectTMSelect.innerHTML = null; + let projectTBSelect = document.getElementById("select-project-settings-tb"); + projectTBSelect.innerHTML = null; + [...Object.keys(relevantKDBs["tm"])].forEach((key, i) => { + let option = new Option(relevantKDBs["tm"][key]["name"], key); + projectTMSelect.appendChild(option); + option.selected = relevantKDBs["tm"][key]["selected"]; + }); + [...Object.keys(relevantKDBs["tb"])].forEach((key, i) => { + let option = new Option(relevantKDBs["tb"][key]["name"], key); + projectTBSelect.appendChild(option); + option.selected=relevantKDBs["tb"][key]["selected"]; + }); + } + } + + xhttp.open("GET", queryURL, true); + xhttp.send(); + } + + document.getElementById("form-project-settings").onsubmit = function(e) { + e.preventDefault(); + + let xhttp = new XMLHttpRequest(); + + let parameters = new FormData(); + parameters.append("task", "set_language_resources"); + + let tMs = ""; + [...this['tm'].selectedOptions].forEach((option, i) => { + if (tMs != "") + { + tMs += ";" + } + tMs += option.value + }) + if (tMs != "") + { + parameters.append("tm", tMs); + } + + let tBs = ""; + [...this['tb'].selectedOptions].forEach((option, i) => { + if (tBs != "") + { + tBs += ";" + } + tBs += option.value + }) + if (tBs != "") + { + parameters.append("tb", tBs); + } + + let queryURL = "http://127.0.0.1:8000/project/" + + filesView.getAttribute("cur-p-id") + + "?task=set_language_resources"; + + xhttp.open("POST", queryURL, true); + xhttp.send(parameters); } document.getElementById("toggle-sidebar").onclick = function() { @@ -821,7 +904,7 @@ function fireOnReady() { window.importProject(); } - document.forms[0].onsubmit = function(e) { + document.getElementById("project-package").onsubmit = function(e) { e.preventDefault(); let filesToPackage = []; diff --git a/backend/api/models.py b/backend/api/models.py index 11f2868..f7f1aff 100644 --- a/backend/api/models.py +++ b/backend/api/models.py @@ -165,3 +165,19 @@ def get_target_direction(self): return self.target_direction else: return LanguageProfile.get_language_direction(self.target_language) + + def get_relevant_kdbs(self): + project_kdbs = self.language_resources.all() + relevant_kdbs_dict = {'tm':{}, 'tb':{}} + for relevant_kdb in KaplanDatabase.objects \ + .filter(source_language=self.source_language) \ + .filter(target_language=self.target_language): + + relevant_kdb_dict = { + 'name': relevant_kdb.title, + 'selected': relevant_kdb in project_kdbs + } + + relevant_kdbs_dict[relevant_kdb.role][relevant_kdb.id] = relevant_kdb_dict + + return relevant_kdbs_dict diff --git a/backend/api/views.py b/backend/api/views.py index fd59590..ed6e340 100644 --- a/backend/api/views.py +++ b/backend/api/views.py @@ -530,9 +530,39 @@ def project_view(request, project_id): request.POST['files'].split(';')) return JsonResponse({'message': 'Target files updated.'}) + elif request.POST.get('task') == 'set_language_resources': + kdbs = [] + tms = request.POST.get('tm') + if tms: + for tm_i in tms.split(';'): + if tm_i == '': + continue + kdb = KaplanDatabase.objects.get(id=int(tm_i)) + if (kdb.source_language == project.source_language + and kdb.target_language == project.target_language): + kdbs.append(kdb) + + tbs = request.POST.get('tb') + if tbs: + for tb_i in tbs.split(';'): + if tb_i == '': + continue + kdb = KaplanDatabase.objects.get(id=int(tb_i)) + if (kdb.source_language == project.source_language + and kdb.target_language == project.target_language): + kdbs.append(kdb) + + + project.language_resources.clear() + project.language_resources.add(*kdbs) + project.save() + + return JsonResponse({'status': 'success'}) else: if request.GET.get('task') == 'get_manifest': return JsonResponse(project.get_project_metadata()) + elif request.GET.get('task') == 'get_relevant_kdbs': + return JsonResponse(project.get_relevant_kdbs()) else: files_dict = {} reports_dict = {}