import logging from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import IsAdminUser from rest_framework.response import Response from rest_framework.views import APIView from rest_framework import status from seaserv import seafile_api from pysearpc import SearpcError from seahub.utils.timeutils import timestamp_to_isoformat_timestr from seahub.api2.authentication import TokenAuthentication from seahub.api2.throttling import UserRateThrottle from seahub.api2.utils import api_error logger = logging.getLogger(__name__) class AdminTrashLibraries(APIView): authentication_classes = (TokenAuthentication, SessionAuthentication) throttle_classes = (UserRateThrottle,) permission_classes = (IsAdminUser,) def get(self, request, format=None): """ get all deleted libraries """ repos_all = seafile_api.get_trash_repo_list(-1, -1) return_results = [] for repo in repos_all: result = {} result['name'] = repo.repo_name result['id'] = repo.repo_id result['owner'] = repo.owner_id result['delete_time'] = timestamp_to_isoformat_timestr(repo.del_time) return_results.append(result) return Response(return_results) def delete(self, request, format=None): """ clean all deleted libraries """ try: seafile_api.empty_repo_trash() except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True}) class AdminTrashLibrary(APIView): authentication_classes = (TokenAuthentication, SessionAuthentication) throttle_classes = (UserRateThrottle,) permission_classes = (IsAdminUser,) def put(self, request, repo_id, format=None): """ restore a deleted library """ try: seafile_api.restore_repo_from_trash(repo_id) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True}) def delete(self, request, repo_id, format=None): """ permanently delete a deleted library """ try: seafile_api.del_repo_from_trash(repo_id) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})