From 114bab218fee3c03ee4f4f0b110367d854127cd1 Mon Sep 17 00:00:00 2001 From: usmannasir <01-134132-158@student.bahria.edu.pk> Date: Fri, 22 Feb 2019 09:19:16 +0500 Subject: [PATCH] search through websites --- plogical/acl.py | 5 ++ plogical/website.py | 47 ++++++++++++++++ static/websiteFunctions/websiteFunctions.js | 54 +++++++++++++++++++ .../websiteFunctions/websiteFunctions.js | 54 +++++++++++++++++++ .../websiteFunctions/listWebsites.html | 7 ++- websiteFunctions/urls.py | 1 + websiteFunctions/views.py | 8 +++ 7 files changed, 175 insertions(+), 1 deletion(-) diff --git a/plogical/acl.py b/plogical/acl.py index 9d29de4f3..81d374c37 100644 --- a/plogical/acl.py +++ b/plogical/acl.py @@ -364,6 +364,11 @@ class ACLManager: return websiteNames + @staticmethod + def searchWebsiteObjects(userID, searchTerm): + admin = Administrator.objects.get(pk=userID) + return Websites.objects.filter(admin=admin, domain__istartswith=searchTerm) + @staticmethod def findWebsiteObjects(currentACL, userID): diff --git a/plogical/website.py b/plogical/website.py index 94e1b2f44..76f91f943 100644 --- a/plogical/website.py +++ b/plogical/website.py @@ -249,6 +249,19 @@ class WebsiteManager: final_json = json.dumps(final_dic) return HttpResponse(final_json) + def searchWebsites(self, userID = None, data = None): + try: + currentACL = ACLManager.loadedACL(userID) + json_data = self.searchWebsitesJson(userID, data['patternAdded']) + pagination = self.websitePagination(currentACL, userID) + final_dic = {'status': 1, 'listWebSiteStatus': 1, 'error_message': "None", "data": json_data, 'pagination': pagination} + final_json = json.dumps(final_dic) + return HttpResponse(final_json) + except BaseException, msg: + dic = {'status': 1, 'listWebSiteStatus': 0, 'error_message': str(msg)} + json_data = json.dumps(dic) + return HttpResponse(json_data) + def getFurtherAccounts(self, userID = None, data = None): try: currentACL = ACLManager.loadedACL(userID) @@ -2019,6 +2032,40 @@ Host gitlab.com json_data = json.dumps(data_ret) return HttpResponse(json_data) + def searchWebsitesJson(self, userID, searchTerm): + + websites = ACLManager.searchWebsiteObjects(userID, searchTerm) + + json_data = "[" + checker = 0 + + try: + ipFile = "/etc/cyberpanel/machineIP" + f = open(ipFile) + ipData = f.read() + ipAddress = ipData.split('\n', 1)[0] + except BaseException, msg: + logging.CyberCPLogFileWriter.writeToFile("Failed to read machine IP, error:" + str(msg)) + ipAddress = "192.168.100.1" + + for items in websites: + if items.state == 0: + state = "Suspended" + else: + state = "Active" + dic = {'domain': items.domain, 'adminEmail': items.adminEmail, 'ipAddress': ipAddress, + 'admin': items.admin.userName, 'package': items.package.packageName, 'state': state} + + if checker == 0: + json_data = json_data + json.dumps(dic) + checker = 1 + else: + json_data = json_data + ',' + json.dumps(dic) + + json_data = json_data + ']' + + return json_data + def findWebsitesJson(self, currentACL, userID, pageNumber): finalPageNumber = ((pageNumber * 10)) - 10 endPageNumber = finalPageNumber + 10 diff --git a/static/websiteFunctions/websiteFunctions.js b/static/websiteFunctions/websiteFunctions.js index 22f8a3ea7..e61bd4640 100644 --- a/static/websiteFunctions/websiteFunctions.js +++ b/static/websiteFunctions/websiteFunctions.js @@ -338,6 +338,60 @@ app.controller('listWebsites', function ($scope, $http) { }; + + $scope.cyberPanelLoading = true; + + $scope.searchWebsites = function () { + + $scope.cyberPanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + var data = { + patternAdded: $scope.patternAdded + }; + + dataurl = "/websites/searchWebsites"; + + $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); + + + function ListInitialData(response) { + $scope.cyberPanelLoading = true; + if (response.data.listWebSiteStatus === 1) { + + var finalData = JSON.parse(response.data.data); + $scope.WebSitesList = finalData; + $("#listFail").hide(); + } + else { + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); + + } + } + + function cantLoadInitialData(response) { + $scope.cyberPanelLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Connect disrupted, refresh the page.', + type: 'error' + }); + } + + + }; + + + }); /* Java script code to list accounts ends here */ diff --git a/websiteFunctions/static/websiteFunctions/websiteFunctions.js b/websiteFunctions/static/websiteFunctions/websiteFunctions.js index 22f8a3ea7..e61bd4640 100644 --- a/websiteFunctions/static/websiteFunctions/websiteFunctions.js +++ b/websiteFunctions/static/websiteFunctions/websiteFunctions.js @@ -338,6 +338,60 @@ app.controller('listWebsites', function ($scope, $http) { }; + + $scope.cyberPanelLoading = true; + + $scope.searchWebsites = function () { + + $scope.cyberPanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + var data = { + patternAdded: $scope.patternAdded + }; + + dataurl = "/websites/searchWebsites"; + + $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); + + + function ListInitialData(response) { + $scope.cyberPanelLoading = true; + if (response.data.listWebSiteStatus === 1) { + + var finalData = JSON.parse(response.data.data); + $scope.WebSitesList = finalData; + $("#listFail").hide(); + } + else { + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); + + } + } + + function cantLoadInitialData(response) { + $scope.cyberPanelLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Connect disrupted, refresh the page.', + type: 'error' + }); + } + + + }; + + + }); /* Java script code to list accounts ends here */ diff --git a/websiteFunctions/templates/websiteFunctions/listWebsites.html b/websiteFunctions/templates/websiteFunctions/listWebsites.html index 84a604238..a0f37c3eb 100644 --- a/websiteFunctions/templates/websiteFunctions/listWebsites.html +++ b/websiteFunctions/templates/websiteFunctions/listWebsites.html @@ -17,10 +17,15 @@

{% trans "List Websites" %}

+ src="{% static 'images/loading.gif' %}">

{% trans "On this page you can launch, list, modify and delete websites from your server." %}

+
+ +
+
diff --git a/websiteFunctions/urls.py b/websiteFunctions/urls.py index 7881c8aeb..25d2d2c4b 100644 --- a/websiteFunctions/urls.py +++ b/websiteFunctions/urls.py @@ -17,6 +17,7 @@ urlpatterns = [ url(r'^submitWebsiteCreation$', views.submitWebsiteCreation, name='submitWebsiteCreation'), url(r'^submitWebsiteDeletion$', views.submitWebsiteDeletion, name='submitWebsiteDeletion'), url(r'^submitWebsiteListing$', views.getFurtherAccounts, name='submitWebsiteListing'), + url(r'^searchWebsites$', views.searchWebsites, name='searchWebsites'), url(r'^submitWebsiteModification$', views.deleteWebsite, name='submitWebsiteModification'), url(r'^submitWebsiteStatus$', views.submitWebsiteStatus, name='submitWebsiteStatus'), diff --git a/websiteFunctions/views.py b/websiteFunctions/views.py index 265e0d7cd..7dabd234f 100644 --- a/websiteFunctions/views.py +++ b/websiteFunctions/views.py @@ -108,6 +108,14 @@ def fetchDomains(request): except KeyError: return redirect(loadLoginPage) +def searchWebsites(request): + try: + userID = request.session['userID'] + wm = WebsiteManager() + return wm.searchWebsites(userID, json.loads(request.body)) + except KeyError: + return redirect(loadLoginPage) + def getFurtherAccounts(request): try: userID = request.session['userID']