diff --git a/backup/backupManager.py b/backup/backupManager.py index 52d731ee7..e33c6d80f 100755 --- a/backup/backupManager.py +++ b/backup/backupManager.py @@ -64,7 +64,9 @@ class BackupManager: for items in gDriveAccts: gDriveAcctsList.append(items.name) - return render(request, 'backup/googleDrive.html', {'accounts': gDriveAcctsList}) + websitesName = ACLManager.findAllSites(currentACL, userID) + + return render(request, 'backup/googleDrive.html', {'accounts': gDriveAcctsList, 'websites': websitesName}) except BaseException as msg: return HttpResponse(str(msg)) @@ -88,6 +90,7 @@ class BackupManager: gD = GDrive(owner=admin, name=gDriveData['name'],auth=json.dumps(gDriveData)) gD.save() + final_json = json.dumps({'status': 1, 'message': 'Successfully saved.'}) return HttpResponse(final_json) except BaseException as msg: @@ -95,6 +98,164 @@ class BackupManager: final_json = json.dumps(final_dic) return HttpResponse(final_json) + def fetchgDriveSites(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) + + selectedAccount = data['selectedAccount'] + recordsToShow = int(data['recordsToShow']) + page = int(str(data['page']).strip('\n')) + + gD = GDrive.objects.get(name=selectedAccount) + + websites = gD.gdrivesites_set.all() + + from s3Backups.s3Backups import S3Backups + + pagination = S3Backups.getPagination(len(websites), recordsToShow) + endPageNumber, finalPageNumber = S3Backups.recordsPointer(page, recordsToShow) + finalWebsites = websites[finalPageNumber:endPageNumber] + + json_data = "[" + checker = 0 + counter = 0 + + from plogical.backupSchedule import backupSchedule + + for website in finalWebsites: + + dic = { + 'name': website.domain + } + + 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 + ']' + + currently = gD.runTime + + data_ret = {'status': 1, 'websites': json_data, 'pagination': pagination, 'currently': currently} + 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 addSitegDrive(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) + + selectedAccount = data['selectedAccount'] + selectedWebsite = data['selectedWebsite'] + + gD = GDrive.objects.get(name=selectedAccount) + + gdSite = GDriveSites(owner=gD, domain=selectedWebsite) + gdSite.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 deleteAccountgDrive(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) + + selectedAccount = data['selectedAccount'] + + gD = GDrive.objects.get(name=selectedAccount) + + gD.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 changeAccountFrequencygDrive(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) + + selectedAccount = data['selectedAccount'] + backupFrequency = data['backupFrequency'] + + gD = GDrive.objects.get(name=selectedAccount) + gD.runTime = backupFrequency + + gD.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 deleteSitegDrive(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) + + selectedAccount = data['selectedAccount'] + website = data['website'] + + gD = GDrive.objects.get(name=selectedAccount) + gDSite = GDriveSites.objects.get(owner=gD, domain=website) + gDSite.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 restoreSite(self, request = None, userID = None, data = None): try: currentACL = ACLManager.loadedACL(userID) diff --git a/backup/static/backup/backup.js b/backup/static/backup/backup.js index b8336dd87..1c8142829 100755 --- a/backup/static/backup/backup.js +++ b/backup/static/backup/backup.js @@ -1600,13 +1600,18 @@ app.controller('backupLogsScheduled', function ($scope, $http, $timeout) { app.controller('googleDrive', function ($scope, $http) { $scope.cyberPanelLoading = true; + $scope.driveHidden = true; $scope.setupAccount = function(){ window.open("https://platform.cyberpanel.net/gDrive?name=" + $scope.accountName + '&server=' + window.location.href + 'Setup'); }; - $scope.fetchPackageas = function () { - $scope.cyberPanelLoading = false; + $scope.currentPage = 1; + $scope.recordsToShow = 10; + + $scope.fetchWebsites = function () { + + $scope.cyberpanelLoading = false; var config = { headers: { @@ -1614,10 +1619,65 @@ app.controller('googleDrive', function ($scope, $http) { } }; - var data = {}; + var data = { + selectedAccount: $scope.selectedAccount, + page: $scope.currentPage, + recordsToShow: $scope.recordsToShow + }; - dataurl = "/CloudLinux/fetchPackages"; + dataurl = "/backup/fetchgDriveSites"; + + $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; + } 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 () { + $scope.cyberPanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + var data = { + selectedWebsite: $scope.selectedWebsite, + selectedAccount: $scope.selectedAccount + }; + + dataurl = "/backup/addSitegDrive"; $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); @@ -1625,7 +1685,12 @@ app.controller('googleDrive', function ($scope, $http) { function ListInitialData(response) { $scope.cyberPanelLoading = true; if (response.data.status === 1) { - $scope.packages = JSON.parse(response.data.data); + new PNotify({ + title: 'Success', + text: 'Site successfully added.', + type: 'success' + }); + $scope.fetchWebsites(); } else { new PNotify({ title: 'Operation Failed!', @@ -1647,5 +1712,146 @@ app.controller('googleDrive', function ($scope, $http) { }; + $scope.deleteAccount = function () { + $scope.cyberPanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + var data = { + selectedAccount: $scope.selectedAccount + }; + + dataurl = "/backup/deleteAccountgDrive"; + + $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); + + + function ListInitialData(response) { + $scope.cyberPanelLoading = true; + if (response.data.status === 1) { + new PNotify({ + title: 'Success', + text: 'Account successfully deleted.', + 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.changeFrequency = function () { + $scope.cyberPanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + var data = { + selectedAccount: $scope.selectedAccount, + backupFrequency: $scope.backupFrequency + }; + + dataurl = "/backup/changeAccountFrequencygDrive"; + + $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); + + + function ListInitialData(response) { + $scope.cyberPanelLoading = true; + if (response.data.status === 1) { + new PNotify({ + title: 'Success', + text: 'Changes successfully applied', + type: 'success' + }); + $scope.fetchWebsites(); + } 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.deleteSite = function (website) { + $scope.cyberPanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + var data = { + selectedAccount: $scope.selectedAccount, + website: website + }; + + dataurl = "/backup/deleteSitegDrive"; + + $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); + + + function ListInitialData(response) { + $scope.cyberPanelLoading = true; + if (response.data.status === 1) { + new PNotify({ + title: 'Success', + text: 'Website Deleted.', + type: 'success' + }); + $scope.fetchWebsites(); + } 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' + }); + } + + + }; }); diff --git a/backup/templates/backup/googleDrive.html b/backup/templates/backup/googleDrive.html index fb3577434..0304e63ce 100755 --- a/backup/templates/backup/googleDrive.html +++ b/backup/templates/backup/googleDrive.html @@ -37,7 +37,7 @@ + + + +
+ +
+ +
+ Currently: {$ currently $} +
+ +
+ +
+ +
+ +
+ +
+
+
+
+
+
+ +
+
+
+ + + + + + + + + + + + + + +
SitesAction
+ +
+
+
+
+
+
+
+ +
+
+
+
+
diff --git a/backup/urls.py b/backup/urls.py index 866f780fb..c93559061 100755 --- a/backup/urls.py +++ b/backup/urls.py @@ -8,6 +8,11 @@ urlpatterns = [ url(r'^restoreSite', views.restoreSite, name='restoreSite'), url(r'^gDrive$', views.gDrive, name='gDrive'), url(r'^gDriveSetup$', views.gDriveSetup, name='gDriveSetup'), + url(r'^fetchgDriveSites$', views.fetchgDriveSites, name='fetchgDriveSites'), + url(r'^addSitegDrive$', views.addSitegDrive, name='addSitegDrive'), + url(r'^deleteAccountgDrive$', views.deleteAccountgDrive, name='deleteAccountgDrive'), + url(r'^changeAccountFrequencygDrive$', views.changeAccountFrequencygDrive, name='changeAccountFrequencygDrive'), + url(r'^deleteSitegDrive$', views.deleteSitegDrive, name='deleteSitegDrive'), url(r'^submitBackupCreation', views.submitBackupCreation, name='submitBackupCreation'), diff --git a/backup/views.py b/backup/views.py index d176e25df..08042202f 100755 --- a/backup/views.py +++ b/backup/views.py @@ -46,6 +46,46 @@ def gDriveSetup(request): except KeyError: return redirect(loadLoginPage) +def fetchgDriveSites(request): + try: + userID = request.session['userID'] + wm = BackupManager() + return wm.fetchgDriveSites(request, userID) + except KeyError: + return redirect(loadLoginPage) + +def addSitegDrive(request): + try: + userID = request.session['userID'] + wm = BackupManager() + return wm.addSitegDrive(request, userID) + except KeyError: + return redirect(loadLoginPage) + +def deleteAccountgDrive(request): + try: + userID = request.session['userID'] + wm = BackupManager() + return wm.deleteAccountgDrive(request, userID) + except KeyError: + return redirect(loadLoginPage) + +def changeAccountFrequencygDrive(request): + try: + userID = request.session['userID'] + wm = BackupManager() + return wm.changeAccountFrequencygDrive(request, userID) + except KeyError: + return redirect(loadLoginPage) + +def deleteSitegDrive(request): + try: + userID = request.session['userID'] + wm = BackupManager() + return wm.deleteSitegDrive(request, userID) + except KeyError: + return redirect(loadLoginPage) + def restoreSite(request): try: userID = request.session['userID'] diff --git a/loginSystem/models.py b/loginSystem/models.py index 459b9367b..a4de5652e 100755 --- a/loginSystem/models.py +++ b/loginSystem/models.py @@ -87,8 +87,6 @@ class Administrator(models.Model): api = models.IntegerField(default=0) securityLevel = models.IntegerField(default=0) state = models.CharField(max_length=10, default='ACTIVE') - #gDrive = models.TextField(max_length=65532, default='Inactive') - initWebsitesLimit = models.IntegerField(default=0) acl = models.ForeignKey(ACL, default=1, on_delete=models.PROTECT) diff --git a/websiteFunctions/models.py b/websiteFunctions/models.py index 80ed1dc4a..3c0b99f90 100755 --- a/websiteFunctions/models.py +++ b/websiteFunctions/models.py @@ -64,12 +64,13 @@ class BackupJobLogs(models.Model): class GDrive(models.Model): owner = models.ForeignKey(Administrator, on_delete=models.CASCADE) - name = models.CharField(max_length=50) + name = models.CharField(max_length=50, unique=True) auth = models.TextField(max_length=65532, default='Inactive') + runTime = models.CharField(max_length=20, default='NEVER') class GDriveSites(models.Model): owner = models.ForeignKey(GDrive, on_delete=models.CASCADE) - domain = models.CharField(max_length=200) + domain = models.CharField(max_length=200, unique=True) class GDriveJobLogs(models.Model): owner = models.ForeignKey(GDrive, on_delete=models.CASCADE)