# Path: apps/web/views/notice.py
# class NoticeViewSet(viewsets.ViewSet):
#     queryset = Notice.objects
#     serializer_class = NoticeSerializer
# 
#     def list(self, request):
#         current_time = timezone.now()
#         notices = self.queryset.queryset.filter(
#             valid_from__lte=current_time,
#             valid_to__gt=current_time,
#         ).order_by('-valid_from')
#         serializer = self.serializer_class(notices, many=True)
#         return Response(serializer.data)
# 
#     @decorators.action(methods=['GET'], detail=False)
#     def latest(self, request):
#         current_time = timezone.now()
#         notices = self.queryset.filter(
#             valid_from__lte=current_time,
#             valid_to__gt=current_time,
#         ).order_by('-valid_from').first()
#         serializer = self.serializer_class([notices], many=True)
#         return Response(serializer.data)
# 
# Path: apps/web/views/profile.py
# class ProfileView(APIView):
#     permission_classes = [IsAuthenticated]
# 
#     def get(self, request):
#         serializer = UserViewSerializer(request.user)
#         return Response(serializer.data)
# 
#     def post(self, request):
#         serializer = UserProfileEditSerializer(data=request.data)
#         serializer.is_valid(raise_exception=True)
#         pass
# 
# Path: apps/web/views/sudo.py
# class SudoView(APIView):
#     permission_classes = [IsAuthenticated]
# 
#     def get(self, request):
#         valid = False
#         expires_at: Optional[datetime] = None
# 
#         if not request.user.has_usable_password():
#             valid = True
#         elif not sudo_password_needed(request.session):
#             valid = True
#             expires_at = sudo_password_expires_at(request.session)
# 
#         serializer = SudoStatusSerializer({
#             "valid": valid,
#             "expires_at": expires_at,
#         })
#         return Response(serializer.data, status=status.HTTP_200_OK)
# 
#     def put(self, request):
#         if not request.user.has_usable_password():
#             sudo_renew(request)
#             return Response({}, status=status.HTTP_200_OK)
# 
#         sudo_remove(request)
#         body = SudoRenewSerializer(data=request.data)
#         body.is_valid(raise_exception=True)
# 
#         logger = logging.getLogger('sso.auth')
#         password = body.validated_data.get("password")
#         success = check_password(password, request.user.password)
# 
#         log_msg = 'success' if success else 'fail'
#         (logger.info if success else logger.warning)(f'sudo.{log_msg}', {
#             'r': request,
#             'extra': [('path', request.path)],
#         })
#         if success:
#             sudo_renew(request)
#             return Response({}, status=status.HTTP_200_OK)
#         return Response({}, status=status.HTTP_401_UNAUTHORIZED)
# 
#     def delete(self, request):
#         sudo_remove(request)


from django.urls import path
from rest_framework import routers
from apps.web.views.notice import NoticeViewSet
from apps.web.views.profile import ProfileView
from apps.web.views.sudo import SudoView


router = routers.SimpleRouter()
router.register(r'notice',  NoticeViewSet)


urlpatterns = [

# Path: apps/web/views/notice.py
# class NoticeViewSet(viewsets.ViewSet):
#     queryset = Notice.objects
#     serializer_class = NoticeSerializer
# 
#     def list(self, request):
#         current_time = timezone.now()
#         notices = self.queryset.queryset.filter(
#             valid_from__lte=current_time,
#             valid_to__gt=current_time,
#         ).order_by('-valid_from')
#         serializer = self.serializer_class(notices, many=True)
#         return Response(serializer.data)
# 
#     @decorators.action(methods=['GET'], detail=False)
#     def latest(self, request):
#         current_time = timezone.now()
#         notices = self.queryset.filter(
#             valid_from__lte=current_time,
#             valid_to__gt=current_time,
#         ).order_by('-valid_from').first()
#         serializer = self.serializer_class([notices], many=True)
#         return Response(serializer.data)
# 
# Path: apps/web/views/profile.py
# class ProfileView(APIView):
#     permission_classes = [IsAuthenticated]
# 
#     def get(self, request):
#         serializer = UserViewSerializer(request.user)
#         return Response(serializer.data)
# 
#     def post(self, request):
#         serializer = UserProfileEditSerializer(data=request.data)
#         serializer.is_valid(raise_exception=True)
#         pass
# 
# Path: apps/web/views/sudo.py
# class SudoView(APIView):
#     permission_classes = [IsAuthenticated]
# 
#     def get(self, request):
#         valid = False
#         expires_at: Optional[datetime] = None
# 
#         if not request.user.has_usable_password():
#             valid = True
#         elif not sudo_password_needed(request.session):
#             valid = True
#             expires_at = sudo_password_expires_at(request.session)
# 
#         serializer = SudoStatusSerializer({
#             "valid": valid,
#             "expires_at": expires_at,
#         })
#         return Response(serializer.data, status=status.HTTP_200_OK)
# 
#     def put(self, request):
#         if not request.user.has_usable_password():
#             sudo_renew(request)
#             return Response({}, status=status.HTTP_200_OK)
# 
#         sudo_remove(request)
#         body = SudoRenewSerializer(data=request.data)
#         body.is_valid(raise_exception=True)
# 
#         logger = logging.getLogger('sso.auth')
#         password = body.validated_data.get("password")
#         success = check_password(password, request.user.password)
# 
#         log_msg = 'success' if success else 'fail'
#         (logger.info if success else logger.warning)(f'sudo.{log_msg}', {
#             'r': request,
#             'extra': [('path', request.path)],
#         })
#         if success:
#             sudo_renew(request)
#             return Response({}, status=status.HTTP_200_OK)
#         return Response({}, status=status.HTTP_401_UNAUTHORIZED)
# 
#     def delete(self, request):
#         sudo_remove(request)


# Path: apps/web/urls.py
from django.urls import path
from rest_framework import routers
from apps.web.views.notice import NoticeViewSet
from apps.web.views.profile import ProfileView
from apps.web.views.sudo import SudoView



router = routers.SimpleRouter()
router.register(r'notice',  NoticeViewSet)


urlpatterns = [

    *router.urls,