mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-22 14:54:22 +00:00
✨ Add sync action to DataSource viewset
This commit is contained in:
parent
d2e42609ce
commit
89025324af
2 changed files with 44 additions and 1 deletions
|
@ -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 = [
|
||||||
|
|
|
@ -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)
|
||||||
|
})
|
||||||
|
|
Loading…
Reference in a new issue