From 97b5bd0e702e5fe02138bb33f47fc4ebd93f7795 Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Sun, 18 Aug 2019 21:07:54 +0500 Subject: [PATCH] list websites: choose to show number of records --- .../websiteFunctions/websiteFunctions.js | 47 +++-------- .../websiteFunctions/listWebsites.html | 37 ++++++--- websiteFunctions/urls.py | 1 + websiteFunctions/views.py | 8 ++ websiteFunctions/website.py | 80 +++++++++++++++++++ 5 files changed, 127 insertions(+), 46 deletions(-) diff --git a/websiteFunctions/static/websiteFunctions/websiteFunctions.js b/websiteFunctions/static/websiteFunctions/websiteFunctions.js index f9fc0908a..9f22ba41b 100755 --- a/websiteFunctions/static/websiteFunctions/websiteFunctions.js +++ b/websiteFunctions/static/websiteFunctions/websiteFunctions.js @@ -231,37 +231,10 @@ $("#listFail").hide(); app.controller('listWebsites', function ($scope, $http) { - url = "/websites/submitWebsiteListing"; + $scope.currentPage = 1; + $scope.recordsToShow = 10; - var data = {page: 1}; - - var config = { - headers: { - 'X-CSRFToken': getCookie('csrftoken') - } - }; - - $http.post(url, data, config).then(ListInitialData, cantLoadInitialData); - - - function ListInitialData(response) { - - if (response.data.listWebSiteStatus === 1) { - var finalData = JSON.parse(response.data.data); - $scope.WebSitesList = finalData; - $scope.pagination = response.data.pagination; - $("#listFail").hide(); - } else { - $("#listFail").fadeIn(); - $scope.errorMessage = response.data.error_message; - - } - } - - function cantLoadInitialData(response) { - } - - $scope.getFurtherWebsitesFromDB = function (pageNumber) { + $scope.getFurtherWebsitesFromDB = function () { var config = { headers: { @@ -269,10 +242,13 @@ app.controller('listWebsites', function ($scope, $http) { } }; - var data = {page: pageNumber}; + var data = { + page: $scope.currentPage, + recordsToShow: $scope.recordsToShow + }; - dataurl = "/websites/submitWebsiteListing"; + dataurl = "/websites/fetchWebsitesList"; $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); @@ -280,8 +256,9 @@ app.controller('listWebsites', function ($scope, $http) { function ListInitialData(response) { if (response.data.listWebSiteStatus === 1) { - var finalData = JSON.parse(response.data.data); - $scope.WebSitesList = finalData; + $scope.WebSitesList = JSON.parse(response.data.data); + $scope.pagination = response.data.pagination; + $scope.clients = JSON.parse(response.data.data); $("#listFail").hide(); } else { $("#listFail").fadeIn(); @@ -291,11 +268,11 @@ app.controller('listWebsites', function ($scope, $http) { } function cantLoadInitialData(response) { - console.log("not good"); } }; + $scope.getFurtherWebsitesFromDB(); $scope.cyberPanelLoading = true; diff --git a/websiteFunctions/templates/websiteFunctions/listWebsites.html b/websiteFunctions/templates/websiteFunctions/listWebsites.html index ef1931371..84dda9598 100755 --- a/websiteFunctions/templates/websiteFunctions/listWebsites.html +++ b/websiteFunctions/templates/websiteFunctions/listWebsites.html @@ -21,11 +21,22 @@

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

-
+
+
+
+ +
+
+
@@ -124,16 +135,20 @@
-
-
- +
+
+
+
+
+
+
+ +
+
+
diff --git a/websiteFunctions/urls.py b/websiteFunctions/urls.py index c75ea0815..08beb86e8 100755 --- 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'^fetchWebsitesList$', views.fetchWebsitesList, name='fetchWebsitesList'), 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 f62f89a7b..16c12fff5 100755 --- a/websiteFunctions/views.py +++ b/websiteFunctions/views.py @@ -125,6 +125,14 @@ def getFurtherAccounts(request): except KeyError: return redirect(loadLoginPage) +def fetchWebsitesList(request): + try: + userID = request.session['userID'] + wm = WebsiteManager() + return wm.fetchWebsitesList(userID, json.loads(request.body)) + except KeyError: + return redirect(loadLoginPage) + def submitWebsiteDeletion(request): try: userID = request.session['userID'] diff --git a/websiteFunctions/website.py b/websiteFunctions/website.py index 9a289311a..d9f83b34e 100755 --- a/websiteFunctions/website.py +++ b/websiteFunctions/website.py @@ -334,6 +334,86 @@ class WebsiteManager: json_data = json.dumps(dic) return HttpResponse(json_data) + + def fetchWebsitesList(self, userID=None, data=None): + try: + currentACL = ACLManager.loadedACL(userID) + pageNumber = int(data['page']) + recordsToShow = int(data['recordsToShow']) + + endPageNumber, finalPageNumber = self.recordsPointer(pageNumber, recordsToShow) + websites = ACLManager.findWebsiteObjects(currentACL, userID) + pagination = self.getPagination(len(websites), recordsToShow) + json_data = self.findWebsitesListJson(websites[finalPageNumber:endPageNumber]) + + 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 findWebsitesListJson(self, websites): + + 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" + + diskUsed = "%sMB" % str(virtualHostUtilities.getDiskUsage("/home/" + items.domain, items.package.diskSpace)[0]) + + dic = {'domain': items.domain, 'adminEmail': items.adminEmail, 'ipAddress': ipAddress, + 'admin': items.admin.userName, 'package': items.package.packageName, 'state': state, 'diskUsed': diskUsed} + + 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 recordsPointer(self, page, toShow): + finalPageNumber = ((page * toShow)) - toShow + endPageNumber = finalPageNumber + toShow + return endPageNumber, finalPageNumber + + def getPagination(self, records, toShow): + pages = float(records) / float(toShow) + + pagination = [] + counter = 1 + + if pages <= 1.0: + pages = 1 + pagination.append(counter) + else: + pages = ceil(pages) + finalPages = int(pages) + 1 + + for i in range(1, finalPages): + pagination.append(counter) + counter = counter + 1 + + return pagination + def submitWebsiteDeletion(self, userID=None, data=None): try: