From 111e22d63b68d788cafa5568110361a89ccd6470 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 7 Apr 2021 20:39:15 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20sync=20action=20to=20DataSour?= =?UTF-8?q?ce=20viewset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sophon/core/urls.py | 1 + sophon/core/views.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/sophon/core/urls.py b/sophon/core/urls.py index 8d549b4..f265484 100644 --- a/sophon/core/urls.py +++ b/sophon/core/urls.py @@ -6,6 +6,7 @@ from . import views router = DefaultRouter() router.register("projects", views.ProjectViewSet) router.register("datasources", views.DataSourceViewSet) +router.register("dataflows", views.DataFlowViewSet) urlpatterns = [ diff --git a/sophon/core/views.py b/sophon/core/views.py index 13787ac..abcbb1c 100644 --- a/sophon/core/views.py +++ b/sophon/core/views.py @@ -1,5 +1,9 @@ -from rest_framework import viewsets, permissions +from rest_framework import viewsets, decorators, response from . import models, serializers +from datetime import datetime +from logging import getLogger + +log = getLogger(__name__) class ProjectViewSet(viewsets.ModelViewSet): @@ -27,3 +31,41 @@ class DataSourceViewSet(viewsets.ModelViewSet): queryset = models.DataSource.objects.all() serializer_class = serializers.DataSourceSerializer permission_classes = [] + + @decorators.action(methods=["post"], detail=True) + def sync(self, request, pk): + """ + Syncronize the :class:`.models.DataFlow`\\ s with the ones stored in the server of the + :class:`.models.DataSource`\\ . + """ + + log.debug(f"Getting DataSource from the database...") + db_datasource: models.DataSource = self.get_object() + + log.debug(f"Requesting dataflows of {db_datasource!r}...") + flows, structs = db_datasource.request_flows() + + log.info(f"Syncing DataFlows of {db_datasource!r}...") + for description, sdmx_id in zip(flows, flows.index): + + log.debug(f"Searching in the database for: {db_datasource!r} | {sdmx_id!r}") + try: + db_flow = models.DataFlow.objects.get(datasource_id=db_datasource, sdmx_id=sdmx_id) + except models.DataFlow.DoesNotExist: + db_flow = models.DataFlow( + datasource_id=db_datasource, + sdmx_id=sdmx_id, + last_update=datetime.now(), + description=description, + ) + log.info(f"Created new DataFlow: {db_flow!r}") + else: + db_flow.last_update = datetime.now() + db_flow.description = description + log.debug(f"Updated DataFlow: {db_flow!r}") + + db_flow.save() + + return response.Response({ + "updated": len(flows) + })