Skip to content

Commit

Permalink
Merge pull request EGCETSII#32 from Full-Tortuga/feature/16-interfaz_…
Browse files Browse the repository at this point in the history
…crear_backup

Feature/16 interfaz crear backup
  • Loading branch information
Javitoox authored Jan 2, 2022
2 parents 0647f7b + 9a6d62e commit 1b276ae
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
22 changes: 17 additions & 5 deletions decide/backups/backups.py
Original file line number Diff line number Diff line change
@@ -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)




Expand Down
14 changes: 5 additions & 9 deletions decide/backups/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions decide/backups/urls.py
Original file line number Diff line number Diff line change
@@ -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'),
]
1 change: 1 addition & 0 deletions decide/decide/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
'postproc',
'store',
'visualizer',
'backups',
]


Expand Down
4 changes: 4 additions & 0 deletions decide_panel/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -18,6 +19,9 @@ function App() {
{/*Componente personalizado de gráficos*/}
<p>Estadísticas</p>
</TabPanel>
<TabPanel header="Backups" rightIcon="pi pi-folder-open">
<Backups />
</TabPanel>
<TabPanel header="Prueba de conexión" leftIcon="pi pi-wifi">
<ConnectionTest></ConnectionTest>
</TabPanel>
Expand Down
46 changes: 46 additions & 0 deletions decide_panel/src/components/Backups.js
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<Messages ref={messages}></Messages>
<Card
id="card-connection-test"
color="gradient"
textColor="white"
width="50%"
hoverable="true"
onClick={connect}
clickable="true"
>
Generar nuevo backup(Copia de seguridad)
</Card>
</div>
);
};

export default Backups;
6 changes: 6 additions & 0 deletions decide_panel/src/services/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 1b276ae

Please sign in to comment.