mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-10 11:21:29 +00:00
[api] admin library history limit
This commit is contained in:
88
seahub/api2/endpoints/admin/library_history.py
Normal file
88
seahub/api2/endpoints/admin/library_history.py
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
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 django.utils.translation import ugettext as _
|
||||||
|
|
||||||
|
from pysearpc import SearpcError
|
||||||
|
from seaserv import seafile_api
|
||||||
|
|
||||||
|
from seahub.api2.authentication import TokenAuthentication
|
||||||
|
from seahub.api2.throttling import UserRateThrottle
|
||||||
|
from seahub.api2.utils import api_error
|
||||||
|
|
||||||
|
from constance import config
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class AdminLibraryHistoryLimit(APIView):
|
||||||
|
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
||||||
|
permission_classes = (IsAdminUser, )
|
||||||
|
throttle_classes = (UserRateThrottle, )
|
||||||
|
|
||||||
|
def get(self, request, repo_id, format=None):
|
||||||
|
|
||||||
|
repo = seafile_api.get_repo(repo_id)
|
||||||
|
if not repo:
|
||||||
|
error_msg = 'Library %s not found.' % repo_id
|
||||||
|
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
|
||||||
|
|
||||||
|
# no settings for virtual repo
|
||||||
|
if repo.is_virtual:
|
||||||
|
error_msg = 'Permission denied.'
|
||||||
|
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
|
||||||
|
|
||||||
|
try:
|
||||||
|
keep_days = seafile_api.get_repo_history_limit(repo_id)
|
||||||
|
return Response({'keep_days': keep_days})
|
||||||
|
except SearpcError as e:
|
||||||
|
logger.error(e)
|
||||||
|
error_msg = 'Internal Server Error'
|
||||||
|
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
|
||||||
|
|
||||||
|
def put(self, request, repo_id, format=None):
|
||||||
|
|
||||||
|
repo = seafile_api.get_repo(repo_id)
|
||||||
|
if not repo:
|
||||||
|
error_msg = 'Library %s not found.' % repo_id
|
||||||
|
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
|
||||||
|
|
||||||
|
# no settings for virtual repo
|
||||||
|
if repo.is_virtual or \
|
||||||
|
not config.ENABLE_REPO_HISTORY_SETTING:
|
||||||
|
|
||||||
|
error_msg = 'Permission denied.'
|
||||||
|
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
|
||||||
|
|
||||||
|
# check arg validation
|
||||||
|
keep_days = request.data.get('keep_days', None)
|
||||||
|
if not keep_days:
|
||||||
|
error_msg = 'keep_days invalid.'
|
||||||
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
|
try:
|
||||||
|
keep_days = int(keep_days)
|
||||||
|
except ValueError:
|
||||||
|
error_msg = 'keep_days invalid.'
|
||||||
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# days <= -1, keep full history
|
||||||
|
# days = 0, not keep history
|
||||||
|
# days > 0, keep a period of days
|
||||||
|
res = seafile_api.set_repo_history_limit(repo_id, keep_days)
|
||||||
|
except SearpcError as e:
|
||||||
|
logger.error(e)
|
||||||
|
error_msg = 'Internal Server Error'
|
||||||
|
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
|
||||||
|
|
||||||
|
if res == 0:
|
||||||
|
new_limit = seafile_api.get_repo_history_limit(repo_id)
|
||||||
|
return Response({'keep_days': new_limit})
|
||||||
|
else:
|
||||||
|
error_msg = 'Failed to set library history limit.'
|
||||||
|
return api_error(status.HTTP_520_OPERATION_FAILED, error_msg)
|
@@ -78,6 +78,7 @@ from seahub.api2.endpoints.admin.logs import AdminLogs
|
|||||||
from seahub.api2.endpoints.admin.org_users import AdminOrgUsers, AdminOrgUser
|
from seahub.api2.endpoints.admin.org_users import AdminOrgUsers, AdminOrgUser
|
||||||
from seahub.api2.endpoints.admin.logo import AdminLogo
|
from seahub.api2.endpoints.admin.logo import AdminLogo
|
||||||
from seahub.api2.endpoints.admin.favicon import AdminFavicon
|
from seahub.api2.endpoints.admin.favicon import AdminFavicon
|
||||||
|
from seahub.api2.endpoints.admin.library_history import AdminLibraryHistoryLimit
|
||||||
|
|
||||||
# Uncomment the next two lines to enable the admin:
|
# Uncomment the next two lines to enable the admin:
|
||||||
#from django.contrib import admin
|
#from django.contrib import admin
|
||||||
@@ -262,6 +263,7 @@ urlpatterns = patterns(
|
|||||||
## admin::libraries
|
## admin::libraries
|
||||||
url(r'^api/v2.1/admin/libraries/$', AdminLibraries.as_view(), name='api-v2.1-admin-libraries'),
|
url(r'^api/v2.1/admin/libraries/$', AdminLibraries.as_view(), name='api-v2.1-admin-libraries'),
|
||||||
url(r'^api/v2.1/admin/libraries/(?P<repo_id>[-0-9a-f]{36})/$', AdminLibrary.as_view(), name='api-v2.1-admin-library'),
|
url(r'^api/v2.1/admin/libraries/(?P<repo_id>[-0-9a-f]{36})/$', AdminLibrary.as_view(), name='api-v2.1-admin-library'),
|
||||||
|
url(r'^api/v2.1/admin/libraries/(?P<repo_id>[-0-9a-f]{36})/history-limit/$', AdminLibraryHistoryLimit.as_view(), name="api-v2.1-admin-library-history-limit"),
|
||||||
url(r'^api/v2.1/admin/libraries/(?P<repo_id>[-0-9a-f]{36})/dirents/$', AdminLibraryDirents.as_view(), name='api-v2.1-admin-library-dirents'),
|
url(r'^api/v2.1/admin/libraries/(?P<repo_id>[-0-9a-f]{36})/dirents/$', AdminLibraryDirents.as_view(), name='api-v2.1-admin-library-dirents'),
|
||||||
url(r'^api/v2.1/admin/libraries/(?P<repo_id>[-0-9a-f]{36})/dirent/$', AdminLibraryDirent.as_view(), name='api-v2.1-admin-library-dirent'),
|
url(r'^api/v2.1/admin/libraries/(?P<repo_id>[-0-9a-f]{36})/dirent/$', AdminLibraryDirent.as_view(), name='api-v2.1-admin-library-dirent'),
|
||||||
|
|
||||||
|
37
tests/api/endpoints/admin/test_library_history.py
Normal file
37
tests/api/endpoints/admin/test_library_history.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
from django.core.urlresolvers import reverse
|
||||||
|
from seahub.test_utils import BaseTestCase
|
||||||
|
|
||||||
|
class LibraryHistory(BaseTestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.login_as(self.admin)
|
||||||
|
self.repo_id = self.repo.id
|
||||||
|
self.url = reverse('api-v2.1-admin-library-history-limit', kwargs=dict(repo_id=self.repo_id))
|
||||||
|
|
||||||
|
def test_can_get(self):
|
||||||
|
resp = self.client.get(self.url)
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
|
||||||
|
def test_can_put(self):
|
||||||
|
data = "keep_days=-1"
|
||||||
|
resp = self.client.put(self.url, data, 'application/x-www-form-urlencoded')
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
json_resp = json.loads(resp.content)
|
||||||
|
self.assertEqual(-1, json_resp['keep_days'])
|
||||||
|
|
||||||
|
data = "keep_days=0"
|
||||||
|
resp = self.client.put(self.url, data, 'application/x-www-form-urlencoded')
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
json_resp = json.loads(resp.content)
|
||||||
|
self.assertEqual(0, json_resp['keep_days'])
|
||||||
|
|
||||||
|
data = "keep_days=8"
|
||||||
|
resp = self.client.put(self.url, data, 'application/x-www-form-urlencoded')
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
json_resp = json.loads(resp.content)
|
||||||
|
self.assertEqual(8, json_resp['keep_days'])
|
||||||
|
|
||||||
|
data = "keep_days=q"
|
||||||
|
resp = self.client.put(self.url, data, 'application/x-www-form-urlencoded')
|
||||||
|
self.assertEqual(400, resp.status_code)
|
Reference in New Issue
Block a user