2022-04-13 12:24:56 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
|
|
|
|
from common.drf.api import JMSModelViewSet
|
|
|
|
from ..models import TempToken
|
|
|
|
from ..serializers import TempTokenSerializer
|
2022-04-19 11:10:36 +00:00
|
|
|
from rbac.permissions import RBACPermission
|
2022-04-13 12:24:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TempTokenViewSet(JMSModelViewSet):
|
|
|
|
serializer_class = TempTokenSerializer
|
2022-04-19 11:10:36 +00:00
|
|
|
permission_classes = [RBACPermission]
|
2022-04-13 12:24:56 +00:00
|
|
|
http_method_names = ['post', 'get', 'options', 'patch']
|
2022-04-19 11:10:36 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'expire': 'authentication.change_temptoken',
|
|
|
|
}
|
2022-04-13 12:24:56 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
username = self.request.user.username
|
2022-04-18 11:50:51 +00:00
|
|
|
return TempToken.objects.filter(username=username).order_by('-date_created')
|
2022-04-13 12:24:56 +00:00
|
|
|
|
|
|
|
@action(methods=['PATCH'], detail=True, url_path='expire')
|
|
|
|
def expire(self, *args, **kwargs):
|
|
|
|
instance = self.get_object()
|
|
|
|
instance.date_expired = timezone.now()
|
|
|
|
instance.save()
|
|
|
|
serializer = self.get_serializer(instance)
|
|
|
|
return Response(serializer.data)
|