sources/*: switch API to use slug in URL

This commit is contained in:
Jens Langhammer
2021-02-09 16:08:24 +01:00
parent 101f916247
commit 552f8c6a9a
6 changed files with 36 additions and 29 deletions

View File

@ -1,4 +1,6 @@
"""Source API Views"""
from datetime import datetime
from django.core.cache import cache
from django.db.models.base import Model
from drf_yasg2.utils import swagger_auto_schema
@ -58,14 +60,19 @@ class LDAPSourceViewSet(ModelViewSet):
queryset = LDAPSource.objects.all()
serializer_class = LDAPSourceSerializer
lookup_field = "slug"
@swagger_auto_schema(responses={200: LDAPSourceSyncStatusSerializer(many=False)})
@action(methods=["GET"], detail=True)
# pylint: disable=invalid-name
def sync_status(self, request: Request, pk: int) -> Response:
def sync_status(self, request: Request, slug: str) -> Response:
source = self.get_object()
last_sync = cache.get(source.state_cache_prefix("last_sync"), None)
return Response(LDAPSourceSyncStatusSerializer({"last_sync": last_sync}).data)
return Response(
LDAPSourceSyncStatusSerializer(
{"last_sync": datetime.fromtimestamp(last_sync)}
).data
)
class LDAPPropertyMappingSerializer(ModelSerializer, MetaNameSerializer):