From 2d4c0a513d9e99ad49b8ff4965f74d87a9d09b2b Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Tue, 22 Sep 2020 19:33:39 +0500 Subject: [PATCH] finish frontend for refactored scheduled backups --- backup/backupManager.py | 217 ++++++++++++++++++-- backup/static/backup/backup.js | 25 ++- backup/templates/backup/backupSchedule.html | 27 ++- backup/urls.py | 3 + backup/views.py | 24 +++ static/backup/backup.js | 146 +++++++++++-- websiteFunctions/models.py | 6 + 7 files changed, 389 insertions(+), 59 deletions(-) diff --git a/backup/backupManager.py b/backup/backupManager.py index 03838e7c6..45ec81431 100755 --- a/backup/backupManager.py +++ b/backup/backupManager.py @@ -242,7 +242,8 @@ class BackupManager: gD = GDrive.objects.get(name=selectedAccount) - if ACLManager.checkGDriveOwnership(gD, admin, currentACL) == 1 and ACLManager.checkOwnership(selectedWebsite, admin, currentACL) == 1: + if ACLManager.checkGDriveOwnership(gD, admin, currentACL) == 1 and ACLManager.checkOwnership( + selectedWebsite, admin, currentACL) == 1: pass else: return ACLManager.loadErrorJson('status', 0) @@ -334,7 +335,8 @@ class BackupManager: gD = GDrive.objects.get(name=selectedAccount) - if ACLManager.checkGDriveOwnership(gD, admin, currentACL) == 1 and ACLManager.checkOwnership(website, admin, currentACL) == 1: + if ACLManager.checkGDriveOwnership(gD, admin, currentACL) == 1 and ACLManager.checkOwnership(website, admin, + currentACL) == 1: pass else: return ACLManager.loadErrorJson('status', 0) @@ -728,7 +730,6 @@ class BackupManager: except: finalDic['user'] = "root" - execPath = "/usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py" execPath = execPath + " submitDestinationCreation --ipAddress " + finalDic['ipAddress'] + " --password " \ + finalDic['password'] + " --port " + finalDic['port'] + ' --user %s' % (finalDic['user']) @@ -743,16 +744,16 @@ class BackupManager: if output.find('1,') > -1: - config = {'type': data['type'], 'ip': data['IPAddress'], 'username': data['userName'], 'port': data['backupSSHPort'], 'path': data['path']} - nd = NormalBackupDests(name=data['name'], config = json.dumps(config)) + config = {'type': data['type'], 'ip': data['IPAddress'], 'username': data['userName'], + 'port': data['backupSSHPort'], 'path': data['path']} + nd = NormalBackupDests(name=data['name'], config=json.dumps(config)) nd.save() - - final_dic = {'status' : 1, 'destStatus': 1, 'error_message': "None"} + final_dic = {'status': 1, 'destStatus': 1, 'error_message': "None"} final_json = json.dumps(final_dic) return HttpResponse(final_json) else: - final_dic = {'status' : 0, 'destStatus': 0, 'error_message': output} + final_dic = {'status': 0, 'destStatus': 0, 'error_message': output} final_json = json.dumps(final_dic) return HttpResponse(final_json) else: @@ -760,13 +761,13 @@ class BackupManager: nd = NormalBackupDests(name=data['name'], config=json.dumps(config)) nd.save() - final_dic = {'status' : 1, 'destStatus': 1, 'error_message': "None"} + final_dic = {'status': 1, 'destStatus': 1, 'error_message': "None"} final_json = json.dumps(final_dic) return HttpResponse(final_json) except BaseException as msg: - final_dic = {'status' : 0, 'destStatus': 0, 'error_message': str(msg)} + final_dic = {'status': 0, 'destStatus': 0, 'error_message': str(msg)} final_json = json.dumps(final_dic) return HttpResponse(final_json) @@ -935,7 +936,7 @@ class BackupManager: config = {'frequency': backupFrequency} - nbj = NormalBackupJobs(owner=nbd, name=name, config=config) + nbj = NormalBackupJobs(owner=nbd, name=name, config=json.dumps(config)) nbj.save() final_json = json.dumps({'status': 1, 'scheduleStatus': 0}) @@ -1472,11 +1473,12 @@ class BackupManager: recordsToShow = int(data['recordsToShow']) page = int(str(data['page']).strip('\n')) - nbd = NormalBackupJobs.objects.get(name=selectedAccount) if ACLManager.currentContextPermission(currentACL, 'scheDuleBackups') == 0: return ACLManager.loadErrorJson('scheduleStatus', 0) + nbd = NormalBackupJobs.objects.get(name=selectedAccount) + websites = nbd.normalbackupsites_set.all() from s3Backups.s3Backups import S3Backups @@ -1507,12 +1509,40 @@ class BackupManager: json_data = json_data + ']' + config = json.loads(nbd.config) - data_ret = {'status': 1, 'websites': json_data, 'pagination': pagination} + try: + lastRun = config['lastRun'] + except: + lastRun = 'Never' + + try: + allSites = config['allSites'] + except: + allSites = 'Selected Only' + + try: + frequency = config['frequency'] + except: + frequency = 'Never' + + try: + currentStatus = config['currentStatus'] + except: + currentStatus = 'Not running' + + data_ret = { + 'status': 1, + 'websites': json_data, + 'pagination': pagination, + 'lastRun': lastRun, + 'allSites': allSites, + 'currently': frequency, + 'currentStatus': currentStatus + } json_data = json.dumps(data_ret) return HttpResponse(json_data) - except BaseException as msg: data_ret = {'status': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) @@ -1558,15 +1588,39 @@ class BackupManager: data = json.loads(request.body) - selectedJob = data['selectedJob'] - selectedWebsite = data['selectedWebsite'] - - nbj = NormalBackupJobs.objects.get(name=selectedJob) - website = Websites.objects.get(domain=selectedWebsite) - if ACLManager.currentContextPermission(currentACL, 'scheDuleBackups') == 0: return ACLManager.loadErrorJson('scheduleStatus', 0) + selectedJob = data['selectedJob'] + type = data['type'] + + nbj = NormalBackupJobs.objects.get(name=selectedJob) + + if type == 'all': + config = json.loads(nbj.config) + + try: + if config['allSites'] == 'all': + config['allSites'] = 'Selected Only' + nbj.config = json.dumps(config) + nbj.save() + data_ret = {'status': 1} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + except: + pass + config['allSites'] = type + nbj.config = json.dumps(config) + nbj.save() + + data_ret = {'status': 1} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + selectedWebsite = data['selectedWebsite'] + + website = Websites.objects.get(domain=selectedWebsite) + try: NormalBackupSites.objects.get(owner=nbj, domain=website) except: @@ -1611,3 +1665,126 @@ class BackupManager: data_ret = {'status': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) return HttpResponse(json_data) + + def changeAccountFrequencyNormal(self, request=None, userID=None, data=None): + try: + + userID = request.session['userID'] + currentACL = ACLManager.loadedACL(userID) + admin = Administrator.objects.get(pk=userID) + + data = json.loads(request.body) + + selectedJob = data['selectedJob'] + backupFrequency = data['backupFrequency'] + + nbj = NormalBackupJobs.objects.get(name=selectedJob) + + if ACLManager.currentContextPermission(currentACL, 'scheDuleBackups') == 0: + return ACLManager.loadErrorJson('scheduleStatus', 0) + + config = json.loads(nbj.config) + config['frequency'] = backupFrequency + + nbj.config = json.dumps(config) + nbj.save() + + data_ret = {'status': 1} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + except BaseException as msg: + data_ret = {'status': 0, 'error_message': str(msg)} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + + def deleteAccountNormal(self, request=None, userID=None, data=None): + try: + + userID = request.session['userID'] + currentACL = ACLManager.loadedACL(userID) + + data = json.loads(request.body) + + selectedJob = data['selectedJob'] + + nbj = NormalBackupJobs.objects.get(name=selectedJob) + + if ACLManager.currentContextPermission(currentACL, 'scheDuleBackups') == 0: + return ACLManager.loadErrorJson('scheduleStatus', 0) + + nbj.delete() + + data_ret = {'status': 1} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + except BaseException as msg: + data_ret = {'status': 0, 'error_message': str(msg)} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + def fetchNormalLogs(self, request=None, userID=None, data=None): + try: + + userID = request.session['userID'] + currentACL = ACLManager.loadedACL(userID) + admin = Administrator.objects.get(pk=userID) + + data = json.loads(request.body) + + selectedJob = data['selectedJob'] + recordsToShow = int(data['recordsToShow']) + page = int(str(data['page']).strip('\n')) + + if ACLManager.currentContextPermission(currentACL, 'scheDuleBackups') == 0: + return ACLManager.loadErrorJson('scheduleStatus', 0) + + nbj = NormalBackupJobs.objects.get(name=selectedJob) + + logs = nbj.normalbackupjoblogs_set.all().order_by('-id') + + from s3Backups.s3Backups import S3Backups + + pagination = S3Backups.getPagination(len(logs), recordsToShow) + endPageNumber, finalPageNumber = S3Backups.recordsPointer(page, recordsToShow) + logs = logs[finalPageNumber:endPageNumber] + + json_data = "[" + checker = 0 + counter = 0 + + from plogical.backupSchedule import backupSchedule + + for log in logs: + + if log.status == backupSchedule.INFO: + status = 'INFO' + else: + status = 'ERROR' + + dic = { + 'type': status, + 'message': log.message + } + + if checker == 0: + json_data = json_data + json.dumps(dic) + checker = 1 + else: + json_data = json_data + ',' + json.dumps(dic) + + counter = counter + 1 + + json_data = json_data + ']' + + data_ret = {'status': 1, 'logs': json_data, 'pagination': pagination} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + + except BaseException as msg: + data_ret = {'status': 0, 'error_message': str(msg)} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) diff --git a/backup/static/backup/backup.js b/backup/static/backup/backup.js index 07f504662..7998f52cf 100755 --- a/backup/static/backup/backup.js +++ b/backup/static/backup/backup.js @@ -1573,7 +1573,7 @@ app.controller('backupDestinations', function ($scope, $http) { // -app.controller('scheduleBackup', function ($scope, $http) { +app.controller('scheduleBackup', function ($scope, $http, $window) { $scope.cyberPanelLoading = true; $scope.driveHidden = true; @@ -1660,7 +1660,6 @@ app.controller('scheduleBackup', function ($scope, $http) { text: 'Schedule successfully added.', type: 'success' }); - $scope.fetchWebsites(); } else { new PNotify({ title: 'Operation Failed!', @@ -1715,6 +1714,10 @@ app.controller('scheduleBackup', function ($scope, $http) { $scope.websites = JSON.parse(response.data.websites); $scope.pagination = response.data.pagination; $scope.currently = response.data.currently; + $scope.allSites = response.data.allSites; + $scope.lastRun = response.data.lastRun; + $scope.currentStatus = response.data.currentStatus; + } else { new PNotify({ title: 'Operation Failed!', @@ -1737,7 +1740,7 @@ app.controller('scheduleBackup', function ($scope, $http) { }; - $scope.addSite = function () { + $scope.addSite = function (type) { $scope.cyberPanelLoading = false; var config = { @@ -1747,7 +1750,8 @@ app.controller('scheduleBackup', function ($scope, $http) { }; var data = { selectedWebsite: $scope.selectedWebsite, - selectedJob: $scope.selectedJob + selectedJob: $scope.selectedJob, + type: type }; dataurl = "/backup/addSiteNormal"; @@ -1794,10 +1798,10 @@ app.controller('scheduleBackup', function ($scope, $http) { } }; var data = { - selectedAccount: $scope.selectedAccount + selectedJob: $scope.selectedJob }; - dataurl = "/backup/deleteAccountgDrive"; + dataurl = "/backup/deleteAccountNormal"; $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); @@ -1810,6 +1814,7 @@ app.controller('scheduleBackup', function ($scope, $http) { text: 'Account successfully deleted.', type: 'success' }); + location.reload(); } else { new PNotify({ title: 'Operation Failed!', @@ -1840,11 +1845,11 @@ app.controller('scheduleBackup', function ($scope, $http) { } }; var data = { - selectedAccount: $scope.selectedAccount, + selectedJob: $scope.selectedJob, backupFrequency: $scope.backupFrequency }; - dataurl = "/backup/changeAccountFrequencygDrive"; + dataurl = "/backup/changeAccountFrequencyNormal"; $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); @@ -1941,13 +1946,13 @@ app.controller('scheduleBackup', function ($scope, $http) { }; var data = { - selectedAccount: $scope.selectedAccount, + selectedJob: $scope.selectedJob, page: $scope.currentPageLogs, recordsToShow: $scope.recordsToShowLogs }; - dataurl = "/backup/fetchDriveLogs"; + dataurl = "/backup/fetchNormalLogs"; $http.post(dataurl, data, config).then(ListInitialDatas, cantLoadInitialDatas); diff --git a/backup/templates/backup/backupSchedule.html b/backup/templates/backup/backupSchedule.html index 909ae210a..5e0ef45e3 100755 --- a/backup/templates/backup/backupSchedule.html +++ b/backup/templates/backup/backupSchedule.html @@ -97,8 +97,6 @@ {% endfor %} -
@@ -121,8 +119,10 @@ {% endfor %}
- +
@@ -130,17 +130,26 @@ - - + + + + - - + + + + +
SitesActionLast RunAll SitesFrequency ({$ currently $})Current Status
{$ lastRun $}{$ allSites $} - + {$ currentStatus $}
diff --git a/backup/urls.py b/backup/urls.py index ec955eb65..043bb19c3 100755 --- a/backup/urls.py +++ b/backup/urls.py @@ -62,5 +62,8 @@ urlpatterns = [ url(r'^fetchNormalJobs$', views.fetchNormalJobs, name='fetchNormalJobs'), url(r'^addSiteNormal$', views.addSiteNormal, name='addSiteNormal'), url(r'^deleteSiteNormal$', views.deleteSiteNormal, name='deleteSiteNormal'), + url(r'^changeAccountFrequencyNormal$', views.changeAccountFrequencyNormal, name='changeAccountFrequencyNormal'), + url(r'^deleteAccountNormal$', views.deleteAccountNormal, name='deleteAccountNormal'), + url(r'^fetchNormalLogs$', views.fetchNormalLogs, name='fetchNormalLogs'), ] \ No newline at end of file diff --git a/backup/views.py b/backup/views.py index 17d100799..fe9701773 100755 --- a/backup/views.py +++ b/backup/views.py @@ -451,3 +451,27 @@ def deleteSiteNormal(request): return wm.deleteSiteNormal(request, userID) except KeyError: return redirect(loadLoginPage) + +def changeAccountFrequencyNormal(request): + try: + userID = request.session['userID'] + wm = BackupManager() + return wm.changeAccountFrequencyNormal(request, userID) + except KeyError: + return redirect(loadLoginPage) + +def deleteAccountNormal(request): + try: + userID = request.session['userID'] + wm = BackupManager() + return wm.deleteAccountNormal(request, userID) + except KeyError: + return redirect(loadLoginPage) + +def fetchNormalLogs(request): + try: + userID = request.session['userID'] + wm = BackupManager() + return wm.fetchNormalLogs(request, userID) + except KeyError: + return redirect(loadLoginPage) \ No newline at end of file diff --git a/static/backup/backup.js b/static/backup/backup.js index 9d2d49516..7998f52cf 100644 --- a/static/backup/backup.js +++ b/static/backup/backup.js @@ -1573,15 +1573,16 @@ app.controller('backupDestinations', function ($scope, $http) { // -app.controller('scheduleBackup', function ($scope, $http) { +app.controller('scheduleBackup', function ($scope, $http, $window) { $scope.cyberPanelLoading = true; $scope.driveHidden = true; + $scope.jobsHidden = true; $scope.currentPage = 1; $scope.recordsToShow = 10; - $scope.fetchWebsites = function () { + $scope.fetchJobs = function () { $scope.cyberPanelLoading = false; @@ -1593,27 +1594,23 @@ app.controller('scheduleBackup', function ($scope, $http) { var data = { selectedAccount: $scope.selectedAccount, - page: $scope.currentPage, - recordsToShow: $scope.recordsToShow }; - dataurl = "/backup/fetchgDriveSites"; + dataurl = "/backup/fetchNormalJobs"; $http.post(dataurl, data, config).then(ListInitialDatas, cantLoadInitialDatas); function ListInitialDatas(response) { $scope.cyberPanelLoading = true; if (response.data.status === 1) { - $scope.driveHidden = false; + $scope.jobsHidden = false; new PNotify({ title: 'Success', text: 'Successfully fetched.', type: 'success' }); - $scope.websites = JSON.parse(response.data.websites); - $scope.pagination = response.data.pagination; - $scope.currently = response.data.currently; + $scope.jobs = response.data.jobs; } else { new PNotify({ title: 'Operation Failed!', @@ -1636,7 +1633,114 @@ app.controller('scheduleBackup', function ($scope, $http) { }; - $scope.addSite = function () { + $scope.addSchedule = function () { + $scope.cyberPanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + var data = { + selectedAccount: $scope.selectedAccount, + name: $scope.name, + backupFrequency: $scope.backupFrequency + }; + + dataurl = "/backup/submitBackupSchedule"; + + $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); + + + function ListInitialData(response) { + $scope.cyberPanelLoading = true; + if (response.data.status === 1) { + new PNotify({ + title: 'Success', + text: 'Schedule successfully added.', + type: 'success' + }); + } 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: 'Could not connect to server, please refresh this page', + type: 'error' + }); + } + + + }; + + $scope.fetchWebsites = function () { + + $scope.cyberPanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + var data = { + selectedAccount: $scope.selectedJob, + page: $scope.currentPage, + recordsToShow: $scope.recordsToShow + }; + + + dataurl = "/backup/fetchgNormalSites"; + + $http.post(dataurl, data, config).then(ListInitialDatas, cantLoadInitialDatas); + + function ListInitialDatas(response) { + $scope.cyberPanelLoading = true; + if (response.data.status === 1) { + $scope.driveHidden = false; + new PNotify({ + title: 'Success', + text: 'Successfully fetched.', + type: 'success' + }); + $scope.websites = JSON.parse(response.data.websites); + $scope.pagination = response.data.pagination; + $scope.currently = response.data.currently; + $scope.allSites = response.data.allSites; + $scope.lastRun = response.data.lastRun; + $scope.currentStatus = response.data.currentStatus; + + } else { + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); + } + } + + function cantLoadInitialDatas(response) { + $scope.cyberPanelLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Could not connect to server, please refresh this page.', + type: 'error' + }); + + + } + + }; + + $scope.addSite = function (type) { $scope.cyberPanelLoading = false; var config = { @@ -1646,10 +1750,11 @@ app.controller('scheduleBackup', function ($scope, $http) { }; var data = { selectedWebsite: $scope.selectedWebsite, - selectedAccount: $scope.selectedAccount + selectedJob: $scope.selectedJob, + type: type }; - dataurl = "/backup/addSitegDrive"; + dataurl = "/backup/addSiteNormal"; $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); @@ -1693,10 +1798,10 @@ app.controller('scheduleBackup', function ($scope, $http) { } }; var data = { - selectedAccount: $scope.selectedAccount + selectedJob: $scope.selectedJob }; - dataurl = "/backup/deleteAccountgDrive"; + dataurl = "/backup/deleteAccountNormal"; $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); @@ -1709,6 +1814,7 @@ app.controller('scheduleBackup', function ($scope, $http) { text: 'Account successfully deleted.', type: 'success' }); + location.reload(); } else { new PNotify({ title: 'Operation Failed!', @@ -1739,11 +1845,11 @@ app.controller('scheduleBackup', function ($scope, $http) { } }; var data = { - selectedAccount: $scope.selectedAccount, + selectedJob: $scope.selectedJob, backupFrequency: $scope.backupFrequency }; - dataurl = "/backup/changeAccountFrequencygDrive"; + dataurl = "/backup/changeAccountFrequencyNormal"; $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); @@ -1787,11 +1893,11 @@ app.controller('scheduleBackup', function ($scope, $http) { } }; var data = { - selectedAccount: $scope.selectedAccount, + selectedJob: $scope.selectedJob, website: website }; - dataurl = "/backup/deleteSitegDrive"; + dataurl = "/backup/deleteSiteNormal"; $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); @@ -1840,13 +1946,13 @@ app.controller('scheduleBackup', function ($scope, $http) { }; var data = { - selectedAccount: $scope.selectedAccount, + selectedJob: $scope.selectedJob, page: $scope.currentPageLogs, recordsToShow: $scope.recordsToShowLogs }; - dataurl = "/backup/fetchDriveLogs"; + dataurl = "/backup/fetchNormalLogs"; $http.post(dataurl, data, config).then(ListInitialDatas, cantLoadInitialDatas); diff --git a/websiteFunctions/models.py b/websiteFunctions/models.py index 78a20d5ee..26f74956f 100755 --- a/websiteFunctions/models.py +++ b/websiteFunctions/models.py @@ -92,3 +92,9 @@ class NormalBackupJobs(models.Model): class NormalBackupSites(models.Model): owner = models.ForeignKey(NormalBackupJobs, on_delete=models.CASCADE) domain = models.ForeignKey(Websites, on_delete=models.CASCADE) + + +class NormalBackupJobLogs(models.Model): + owner = models.ForeignKey(NormalBackupJobs, on_delete=models.CASCADE) + status = models.IntegerField() + message = models.TextField()