diff --git a/decide/backups/backups.py b/decide/backups/backups.py index 0df52b02b6..2264b7f111 100644 --- a/decide/backups/backups.py +++ b/decide/backups/backups.py @@ -1,13 +1,25 @@ import os import time +from rest_framework import generics +from rest_framework.response import Response +from rest_framework.status import ( + HTTP_201_CREATED as ST_201, + HTTP_400_BAD_REQUEST as ST_400, +) def location(dir): - return dir + time.strftime("%d-%m-%Y-%H:%M:%S") + return dir + str(time.strftime("%d-%m-%Y-%H:%M:%S")) -def run_backup(dir): - print("generating mongo backup...") - run_backup = "mongodump --out " + location(dir) - os.system(run_backup) +class CreateBackup(generics.CreateAPIView): + def post(self, request, *args, **kwargs): + try: + print("generating mongo backup...") + run_backup = "mongodump --out " + location("./backups/backups/") + os.system(run_backup) + return Response({"name": location("./backups/backups/")}, status=ST_201) + except Exception as e: + return Response({"error": str(e)}, status=ST_400) + diff --git a/decide/backups/tests.py b/decide/backups/tests.py index e6218a8320..a6461357b8 100644 --- a/decide/backups/tests.py +++ b/decide/backups/tests.py @@ -4,12 +4,8 @@ # Create your tests here. class TestBackups(BaseTestCase): def test_create_backups(self): - run_backup("./backups/backups_test/") - path = time.strftime("%d-%m-%Y") - ls = os.listdir("./backups/backups_test/") - cont = 0 - for i in ls: - if path in i: - cont += 1 - self.assertTrue(cont>0) - os.system("rm -rf ./backups/backups_test") + response = self.client.post('/backups/create', "", format='json') + self.assertEqual(response.status_code, 201) + ls = os.listdir("./backups/backups") + backup_test = ls[-1] + os.system("rm -rf ./backups/backups/" + backup_test) diff --git a/decide/backups/urls.py b/decide/backups/urls.py index c50996d637..5742c85aea 100644 --- a/decide/backups/urls.py +++ b/decide/backups/urls.py @@ -1,7 +1,9 @@ from django.urls import path, include +from . import backups from . import views urlpatterns = [ #It isn already implemented in the views.py path('', views.index), + path('create', backups.CreateBackup.as_view(), name='create_backup'), ] \ No newline at end of file diff --git a/decide/decide/test_settings.py b/decide/decide/test_settings.py index 50d62be08d..140efb9469 100644 --- a/decide/decide/test_settings.py +++ b/decide/decide/test_settings.py @@ -70,6 +70,7 @@ 'postproc', 'store', 'visualizer', + 'backups', ] diff --git a/decide_panel/src/App.js b/decide_panel/src/App.js index 1df5dfca66..6810c34474 100644 --- a/decide_panel/src/App.js +++ b/decide_panel/src/App.js @@ -3,6 +3,7 @@ import "./css/App.css"; import { TabPanel } from "primereact/tabview"; import ConnectionTest from "./components/ConnectionTest"; import Voting from "./components/Voting"; +import Backups from "./components/Backups"; function App() { return ( @@ -18,6 +19,9 @@ function App() { {/*Componente personalizado de gráficos*/}

Estadísticas

+ + + diff --git a/decide_panel/src/components/Backups.js b/decide_panel/src/components/Backups.js new file mode 100644 index 0000000000..db063c38b4 --- /dev/null +++ b/decide_panel/src/components/Backups.js @@ -0,0 +1,46 @@ +import { Card } from "@nextui-org/react"; +import { useRef } from "react"; +import { Messages } from "primereact/messages"; +import Api from "../services/backend"; +import "../css/ConnectionTest.css"; + +const Backups = () => { + const messages = useRef(null); + + function connect() { + Api.create_backup() + .then((status) => { + if (status === 201) { + messages.current.show({ + severity: "success", + summary: "Se ha creado correctamente el backup", + }); + } + }) + .catch((err) => { + messages.current.show({ + severity: "warn", + summary: "Error generando el backup", + }); + }); + } + + return ( +
+ + + Generar nuevo backup(Copia de seguridad) + +
+ ); +}; + +export default Backups; \ No newline at end of file diff --git a/decide_panel/src/services/backend.js b/decide_panel/src/services/backend.js index 2a16bcdbd6..784a388745 100644 --- a/decide_panel/src/services/backend.js +++ b/decide_panel/src/services/backend.js @@ -12,6 +12,12 @@ const Api = { .get(URI_BACKEND + "census/" + voting_id + "/") .then((res) => res.data); }, + + create_backup() { + return axios + .post(URI_BACKEND + "backups/create") + .then((res) => res.status); + }, }; export default Api;