diff --git a/seahub/api2/endpoints/shared_upload_links.py b/seahub/api2/endpoints/shared_upload_links.py new file mode 100644 index 0000000000..4f2d3d5745 --- /dev/null +++ b/seahub/api2/endpoints/shared_upload_links.py @@ -0,0 +1,63 @@ +import json +import os + +from django.http import HttpResponse +from django.utils.dateformat import DateFormat +from rest_framework.authentication import SessionAuthentication +from rest_framework.permissions import IsAuthenticated +from rest_framework.throttling import UserRateThrottle +from rest_framework.views import APIView + +from seaserv import seafile_api + +from seahub.api2.authentication import TokenAuthentication +from seahub.share.models import UploadLinkShare +from seahub.utils import gen_shared_upload_link + +json_content_type = 'application/json; charset=utf-8' + +class SharedUploadLinksView(APIView): + authentication_classes = (TokenAuthentication, SessionAuthentication ) + permission_classes = (IsAuthenticated,) + throttle_classes = (UserRateThrottle, ) + + def get(self, request, format=None): + username = request.user.username + + uploadlinks = UploadLinkShare.objects.filter(username=username) + p_uploadlinks = [] + for link in uploadlinks: + r = seafile_api.get_repo(link.repo_id) + if not r: + link.delete() + continue + + if seafile_api.get_dir_id_by_path(r.id, link.path) is None: + link.delete() + continue + + if link.path != '/': + link.dir_name = os.path.basename(link.path.rstrip('/')) + else: + link.dir_name = link.path + + link.shared_link = gen_shared_upload_link(link.token) + link.repo = r + + if link.expire_date: + expire_date = link.expire_date.strftime("%Y-%m-%dT%H:%M:%S") + DateFormat(link.expire_date).format('O') + else: + expire_date = "" + + p_uploadlinks.append({ + "username": link.username, + "repo_id": link.repo_id, + "path": link.path, + "token": link.token, + "ctime": link.ctime.strftime("%Y-%m-%dT%H:%M:%S") + DateFormat(link.ctime).format('O'), + "view_cnt": link.view_cnt, + "expire_date": expire_date, + }) + + return HttpResponse(json.dumps(p_uploadlinks), + status=200, content_type=json_content_type) diff --git a/seahub/api2/urls.py b/seahub/api2/urls.py index 2b128f1886..3fba174fc6 100644 --- a/seahub/api2/urls.py +++ b/seahub/api2/urls.py @@ -5,6 +5,7 @@ from .views_misc import ServerInfoView from .views_auth import LogoutDeviceView, ClientLoginTokenView from .endpoints.dir_shared_items import DirSharedItemsEndpoint from .endpoints.account import Account +from .endpoints.shared_upload_links import SharedUploadLinksView urlpatterns = patterns('', url(r'^ping/$', Ping.as_view()), @@ -50,6 +51,7 @@ urlpatterns = patterns('', url(r'^beshared-repos/$', BeShared.as_view(), name='beshared'), url(r'^default-repo/$', DefaultRepoView.as_view(), name='api2-defaultrepo'), url(r'^shared-links/$', SharedLinksView.as_view()), + url(r'^shared-upload-links/$', SharedUploadLinksView.as_view()), url(r'^shared-files/$', SharedFilesView.as_view()), url(r'^virtual-repos/$', VirtualRepos.as_view()), url(r'^repo-tokens/$', RepoTokensView.as_view()), diff --git a/tests/api/endpoints/test_shared_upload_links.py b/tests/api/endpoints/test_shared_upload_links.py new file mode 100644 index 0000000000..ef1419f054 --- /dev/null +++ b/tests/api/endpoints/test_shared_upload_links.py @@ -0,0 +1,35 @@ +import json + +from django.utils.dateformat import DateFormat + +from seahub.share.models import UploadLinkShare +from seahub.test_utils import BaseTestCase + +class SharedUploadLinksTest(BaseTestCase): + def tearDown(self): + self.remove_repo() + + def test_can_list(self): + ls = UploadLinkShare.objects.create_upload_link_share( + self.user.username, + self.repo.id, self.folder) + + enc_ls = UploadLinkShare.objects.create_upload_link_share( + self.user.username, + self.repo.id, self.folder, password='123', expire_date=None) + + self.login_as(self.user) + resp = self.client.get( + '/api2/shared-upload-links/', + ) + self.assertEqual(200, resp.status_code) + json_resp = json.loads(resp.content) + assert len(json_resp) == 2 + + assert json_resp[0]['username'] == ls.username + assert json_resp[0]['repo_id'] == ls.repo_id + assert json_resp[0]['ctime'].startswith( + ls.ctime.strftime("%Y-%m-%dT%H:%M:%S")) + assert json_resp[0]['token'] == ls.token + assert json_resp[0]['view_cnt'] == ls.view_cnt + assert json_resp[0]['path'] == ls.path