1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-05-13 10:25:46 +00:00

Merge pull request from haiwen/groups-for-repo-share

add api for getting groups that user can share repo to
This commit is contained in:
xiez 2018-01-17 19:04:14 +08:00 committed by GitHub
commit 14f143fe47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 123 additions and 77 deletions

View File

@ -1,37 +0,0 @@
# Copyright (c) 2011-2016 Seafile Ltd.
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from seaserv import ccnet_api
from seahub.api2.authentication import TokenAuthentication
from seahub.api2.throttling import UserRateThrottle
from seahub.api2.endpoints.groups import get_group_info
from seahub.avatar.settings import GROUP_AVATAR_DEFAULT_SIZE
from constance import config
class AllGroupsView(APIView):
authentication_classes = (TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle, )
def get(self, request):
"""List all groups
"""
if config.ENABLE_SHARE_TO_ALL_GROUPS:
groups_list = ccnet_api.get_all_groups(-1, -1)
else:
return Response([])
try:
avatar_size = int(request.GET.get('avatar_size',
GROUP_AVATAR_DEFAULT_SIZE))
except ValueError:
avatar_size = GROUP_AVATAR_DEFAULT_SIZE
groups = [get_group_info(request, g.id, avatar_size)
for g in groups_list]
return Response(groups)

View File

@ -0,0 +1,59 @@
# Copyright (c) 2011-2016 Seafile Ltd.
import os
import sys
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from seaserv import ccnet_api
from seahub.utils import is_org_context
from seahub.api2.authentication import TokenAuthentication
from seahub.api2.throttling import UserRateThrottle
from seahub.api2.endpoints.groups import get_group_info
from seahub.avatar.settings import GROUP_AVATAR_DEFAULT_SIZE
from constance import config
try:
current_path = os.path.dirname(os.path.abspath(__file__))
seafile_conf_dir = os.path.join(current_path, \
'../../../../../conf')
sys.path.append(seafile_conf_dir)
from seahub_custom_functions import custom_get_groups
CUSTOM_GET_GROUPS = True
except ImportError as e:
CUSTOM_GET_GROUPS = False
class ShareableGroups(APIView):
authentication_classes = (TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle, )
def get(self, request):
""" List groups that user can share a library to.
"""
if config.ENABLE_SHARE_TO_ALL_GROUPS:
if CUSTOM_GET_GROUPS:
groups = custom_get_groups(request)
else:
groups = ccnet_api.get_all_groups(-1, -1)
else:
username = request.user.username
if is_org_context(request):
org_id = request.user.org.org_id
groups = ccnet_api.get_org_groups_by_user(org_id, username)
else:
groups = ccnet_api.get_groups(username)
try:
avatar_size = int(request.GET.get('avatar_size',
GROUP_AVATAR_DEFAULT_SIZE))
except ValueError:
avatar_size = GROUP_AVATAR_DEFAULT_SIZE
result = [get_group_info(request, g.id, avatar_size)
for g in groups]
return Response(result)

View File

@ -20,7 +20,7 @@ from seahub.views.wiki import personal_wiki, personal_wiki_pages, \
personal_wiki_create, personal_wiki_page_new, personal_wiki_page_edit, \
personal_wiki_page_delete, personal_wiki_use_lib
from seahub.api2.endpoints.groups import Groups, Group
from seahub.api2.endpoints.all_groups import AllGroupsView
from seahub.api2.endpoints.shareable_groups import ShareableGroups
from seahub.api2.endpoints.group_libraries import GroupLibraries, GroupLibrary
from seahub.api2.endpoints.group_members import GroupMembers, GroupMembersBulk, GroupMember
from seahub.api2.endpoints.search_group import SearchGroup
@ -210,8 +210,9 @@ urlpatterns = patterns(
url(r'^api/v2.1/user/$', User.as_view(), name="api-v2.1-user"),
## user::groups
url(r'^api/v2.1/shareable-groups/$', ShareableGroups.as_view(), name='api-v2.1-shareable-groups'),
url(r'^api/v2.1/groups/$', Groups.as_view(), name='api-v2.1-groups'),
url(r'^api/v2.1/groups/all/$', AllGroupsView.as_view(), name='api-v2.1-all-groups'),
url(r'^api/v2.1/groups/(?P<group_id>\d+)/$', Group.as_view(), name='api-v2.1-group'),
url(r'^api/v2.1/groups/(?P<group_id>\d+)/libraries/$', GroupLibraries.as_view(), name='api-v2.1-group-libraries'),

View File

@ -677,7 +677,7 @@ define([
if (app.pageOptions.enable_share_to_all_groups) {
$.ajax({
url: Common.getUrl({
name: 'all_groups'
name: 'shareable_groups'
}),
type: 'GET',
dataType: 'json',

View File

@ -143,7 +143,7 @@ define([
// Group
case 'groups': return siteRoot + 'api/v2.1/groups/';
case 'all_groups': return siteRoot + 'api/v2.1/groups/all/';
case 'shareable_groups': return siteRoot + 'api/v2.1/shareable-groups/';
case 'search_group': return siteRoot + 'api/v2.1/search-group/';
case 'group': return siteRoot + 'api/v2.1/groups/' + options.group_id + '/';
case 'group_members': return siteRoot + 'api/v2.1/groups/' + options.group_id + '/members/';

View File

@ -1,36 +0,0 @@
# Copyright (c) 2011-2016 Seafile Ltd.
# -*- coding: utf-8 -*-
import json
from django.core.urlresolvers import reverse
from seahub.test_utils import BaseTestCase
from constance import config
class GroupsShareTest(BaseTestCase):
def test_can_get(self):
config.ENABLE_SHARE_TO_ALL_GROUPS = 1
self.login_as(self.admin)
self.admin_group = self.create_group(group_name='test_group',
username=self.admin.username)
self.logout()
self.login_as(self.user)
resp = [group["name"] for group in
json.loads(self.client.get(reverse('api-v2.1-all-groups')).content)]
self.assertIn('test_group', resp)
self.clear_cache()
def test_can_get_with_disable_config(self):
config.ENABLE_SHARE_TO_ALL_GROUPS = 0
self.login_as(self.admin)
self.admin_group = self.create_group(group_name='test_disable_config',
username=self.admin.username)
resp = json.loads(self.client.get(reverse('api-v2.1-all-groups')).content)
self.assertEqual([], resp)
self.clear_cache()
def tearDown(self):
self.remove_group(self.admin_group.id)

View File

@ -0,0 +1,59 @@
# Copyright (c) 2011-2016 Seafile Ltd.
# -*- coding: utf-8 -*-
import json
from django.core.urlresolvers import reverse
from seahub.test_utils import BaseTestCase
from tests.common.utils import randstring
from constance import config
class ShareableGroupsTest(BaseTestCase):
def setUp(self):
self.login_as(self.user)
self.group_id = self.group.id
self.group_name = self.group.group_name
self.admin_user = self.admin.username
self.url = reverse('api-v2.1-shareable-groups')
def tearDown(self):
self.remove_group()
self.remove_repo()
def test_can_get(self):
config.ENABLE_SHARE_TO_ALL_GROUPS = 1
admin_group_name = randstring(10)
admin_group = self.create_group(group_name=admin_group_name,
username=self.admin_user)
resp = self.client.get(self.url)
self.assertEqual(200, resp.status_code)
json_resp = json.loads(resp.content)
group_names = [g["name"] for g in json_resp]
self.assertIn(admin_group_name, group_names)
self.assertIn(self.group.group_name, group_names)
self.remove_group(admin_group.group_id)
self.clear_cache()
def test_can_get_with_disable_config(self):
config.ENABLE_SHARE_TO_ALL_GROUPS = 0
admin_group_name = randstring(10)
admin_group = self.create_group(group_name=admin_group_name,
username=self.admin_user)
resp = self.client.get(self.url)
self.assertEqual(200, resp.status_code)
json_resp = json.loads(resp.content)
group_names = [g["name"] for g in json_resp]
self.assertIn(self.group.group_name, group_names)
self.assertNotIn(admin_group_name, group_names)
self.remove_group(admin_group.group_id)
self.clear_cache()