1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-23 07:14:21 +00:00

Add sync action to DataSource viewset

This commit is contained in:
Steffo 2021-04-07 20:39:15 +02:00
parent 8aa3c324a6
commit 111e22d63b
Signed by: steffo
GPG key ID: 6965406171929D01
2 changed files with 44 additions and 1 deletions

View file

@ -6,6 +6,7 @@ from . import views
router = DefaultRouter() router = DefaultRouter()
router.register("projects", views.ProjectViewSet) router.register("projects", views.ProjectViewSet)
router.register("datasources", views.DataSourceViewSet) router.register("datasources", views.DataSourceViewSet)
router.register("dataflows", views.DataFlowViewSet)
urlpatterns = [ urlpatterns = [

View file

@ -1,5 +1,9 @@
from rest_framework import viewsets, permissions from rest_framework import viewsets, decorators, response
from . import models, serializers from . import models, serializers
from datetime import datetime
from logging import getLogger
log = getLogger(__name__)
class ProjectViewSet(viewsets.ModelViewSet): class ProjectViewSet(viewsets.ModelViewSet):
@ -27,3 +31,41 @@ class DataSourceViewSet(viewsets.ModelViewSet):
queryset = models.DataSource.objects.all() queryset = models.DataSource.objects.all()
serializer_class = serializers.DataSourceSerializer serializer_class = serializers.DataSourceSerializer
permission_classes = [] 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)
})