Coverage for netbox/api/features.py: 91%
18 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
1from django.shortcuts import get_object_or_404
2from rest_framework.decorators import action
3from rest_framework.response import Response
5from netbox.api.authentication import TokenSyncPermission
7__all__ = (
8 'SyncedDataMixin',
9)
12class SyncedDataMixin:
14 def get_permissions(self):
15 if self.action == 'sync':
16 return [TokenSyncPermission()]
17 return super().get_permissions()
19 @action(detail=True, methods=['post'])
20 def sync(self, request, pk):
21 """
22 Provide a /sync API endpoint to synchronize an object's data from its associated DataFile (if any).
23 """
24 # Rebuild from the default manager: initial() has narrowed self.queryset to the add permission,
25 # which does not govern this action.
26 queryset = self.queryset.model.objects.restrict(request.user, 'sync')
27 obj = get_object_or_404(queryset, pk=pk)
28 if obj.data_file: 28 ↛ 29line 28 didn't jump to line 29 because the condition on line 28 was never true
29 obj.sync(save=True)
30 serializer = self.serializer_class(obj, context={'request': request})
32 return Response(serializer.data)