mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
from django.shortcuts import get_object_or_404
|
|
from django.utils.translation import gettext_lazy as _
|
|
from rest_framework.decorators import action
|
|
from rest_framework.exceptions import PermissionDenied
|
|
from rest_framework.response import Response
|
|
from rest_framework.routers import APIRootView
|
|
from rest_framework.viewsets import ReadOnlyModelViewSet
|
|
|
|
from core import filtersets
|
|
from core.models import *
|
|
from netbox.api.viewsets import NetBoxModelViewSet, NetBoxReadOnlyModelViewSet
|
|
from . import serializers
|
|
|
|
|
|
class CoreRootView(APIRootView):
|
|
"""
|
|
Core API root view
|
|
"""
|
|
def get_view_name(self):
|
|
return 'Core'
|
|
|
|
|
|
class DataSourceViewSet(NetBoxModelViewSet):
|
|
queryset = DataSource.objects.all()
|
|
serializer_class = serializers.DataSourceSerializer
|
|
filterset_class = filtersets.DataSourceFilterSet
|
|
|
|
@action(detail=True, methods=['post'])
|
|
def sync(self, request, pk):
|
|
"""
|
|
Enqueue a job to synchronize the DataSource.
|
|
"""
|
|
datasource = get_object_or_404(DataSource, pk=pk)
|
|
|
|
if not request.user.has_perm('core.sync_datasource', obj=datasource):
|
|
raise PermissionDenied(_("This user does not have permission to synchronize this data source."))
|
|
|
|
datasource.enqueue_sync_job(request)
|
|
serializer = serializers.DataSourceSerializer(datasource, context={'request': request})
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
class DataFileViewSet(NetBoxReadOnlyModelViewSet):
|
|
queryset = DataFile.objects.defer('data')
|
|
serializer_class = serializers.DataFileSerializer
|
|
filterset_class = filtersets.DataFileFilterSet
|
|
|
|
|
|
class JobViewSet(ReadOnlyModelViewSet):
|
|
"""
|
|
Retrieve a list of job results
|
|
"""
|
|
queryset = Job.objects.all()
|
|
serializer_class = serializers.JobSerializer
|
|
filterset_class = filtersets.JobFilterSet
|