1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-17 15:53:28 +00:00
Files
seahub/seahub/api2/endpoints/departments.py

79 lines
2.7 KiB
Python
Raw Normal View History

2019-05-09 21:26:20 +08:00
# -*- coding: utf-8 -*-
import logging
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 rest_framework import status
import seaserv
from seaserv import ccnet_api
from seahub.api2.utils import api_error
from seahub.api2.authentication import TokenAuthentication
from seahub.api2.throttling import UserRateThrottle
from seahub.avatar.templatetags.group_avatar_tags import api_grp_avatar_url, get_default_group_avatar_url
from seahub.utils import is_pro_version
2019-05-09 21:26:20 +08:00
from seahub.utils.timeutils import timestamp_to_isoformat_timestr
2019-05-10 12:10:10 +08:00
from seahub.group.utils import is_group_member
2019-05-09 21:26:20 +08:00
from seahub.avatar.settings import GROUP_AVATAR_DEFAULT_SIZE
logger = logging.getLogger(__name__)
2019-05-09 22:09:22 +08:00
class Departments(APIView):
2019-05-09 21:26:20 +08:00
authentication_classes = (TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle, )
def get(self, request):
"""list all departments
"""
if not is_pro_version():
return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.')
2019-05-09 21:26:20 +08:00
try:
departments = ccnet_api.list_all_departments()
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
try:
avatar_size = int(request.GET.get('avatar_size', GROUP_AVATAR_DEFAULT_SIZE))
except ValueError:
avatar_size = GROUP_AVATAR_DEFAULT_SIZE
result = []
for department in departments:
department = seaserv.get_group(department.id)
2019-05-10 12:10:10 +08:00
username = request.user.username
if not is_group_member(department.id, username):
continue
2019-05-09 21:26:20 +08:00
try:
avatar_url, is_default, date_uploaded = api_grp_avatar_url(department.id, avatar_size)
except Exception as e:
logger.error(e)
avatar_url = get_default_group_avatar_url()
created_at = timestamp_to_isoformat_timestr(department.timestamp)
department_info = {
"id": department.id,
"email": '%s@seafile_group' % str(department.id),
"parent_group_id": department.parent_group_id,
"name": department.group_name,
"owner": department.creator_name,
"created_at": created_at,
"avatar_url": request.build_absolute_uri(avatar_url),
}
result.append(department_info)
return Response(result)