mirror of
https://github.com/haiwen/seahub.git
synced 2025-08-31 22:54:11 +00:00
modify batch add admin user api (#4190)
This commit is contained in:
@@ -19,13 +19,13 @@ from seahub.base.accounts import User
|
|||||||
from seahub.profile.models import Profile
|
from seahub.profile.models import Profile
|
||||||
from seahub.institutions.models import Institution
|
from seahub.institutions.models import Institution
|
||||||
from seahub.utils.file_size import get_file_size_unit
|
from seahub.utils.file_size import get_file_size_unit
|
||||||
from seahub.utils import string2list, is_valid_username
|
from seahub.utils import is_valid_username
|
||||||
from seahub.admin_log.models import USER_DELETE
|
from seahub.admin_log.models import USER_DELETE
|
||||||
from seahub.admin_log.signals import admin_operation
|
from seahub.admin_log.signals import admin_operation
|
||||||
from seahub.utils.timeutils import timestamp_to_isoformat_timestr
|
from seahub.utils.timeutils import timestamp_to_isoformat_timestr, datetime_to_isoformat_timestr
|
||||||
from seahub.constants import DEFAULT_ADMIN
|
from seahub.constants import DEFAULT_ADMIN
|
||||||
from seahub.role_permissions.models import AdminRole
|
from seahub.role_permissions.models import AdminRole
|
||||||
from seahub.base.templatetags.seahub_tags import email2nickname
|
from seahub.base.templatetags.seahub_tags import email2nickname, email2contact_email
|
||||||
from seahub.base.models import UserLastLogin
|
from seahub.base.models import UserLastLogin
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -39,15 +39,15 @@ class AdminAdminUsersBatch(APIView):
|
|||||||
def post(self, request):
|
def post(self, request):
|
||||||
""" Add admin in batch
|
""" Add admin in batch
|
||||||
"""
|
"""
|
||||||
|
# argument check
|
||||||
emails = request.data.get('emails', None)
|
emails = request.POST.getlist('email', None)
|
||||||
if not emails:
|
if not emails:
|
||||||
error_msg = 'emails invalid.'
|
error_msg = 'email invalid.'
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
emails = string2list(emails)
|
result = {}
|
||||||
new_admin_info = []
|
result['failed'] = []
|
||||||
|
result['success'] = []
|
||||||
for email in emails:
|
for email in emails:
|
||||||
if not is_valid_username(email):
|
if not is_valid_username(email):
|
||||||
error_msg = 'email %s invalid.' % email
|
error_msg = 'email %s invalid.' % email
|
||||||
@@ -60,13 +60,21 @@ class AdminAdminUsersBatch(APIView):
|
|||||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
user.is_staff = True
|
user.is_staff = True
|
||||||
user.save()
|
try:
|
||||||
|
user.save()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
result['failed'].append({
|
||||||
|
'email': email,
|
||||||
|
'error_msg': 'Internal Server Error'
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
|
||||||
profile = Profile.objects.get_profile_by_user(user.email)
|
profile = Profile.objects.get_profile_by_user(user.email)
|
||||||
user_info = {}
|
user_info = {}
|
||||||
user_info['email'] = user.email
|
user_info['email'] = user.email
|
||||||
user_info['name'] = email2nickname(user.email)
|
user_info['name'] = email2nickname(user.email)
|
||||||
user_info['contact_email'] = profile.contact_email if profile and profile.contact_email else ''
|
user_info['contact_email'] = email2contact_email(user.email)
|
||||||
user_info['login_id'] = profile.login_id if profile and profile.login_id else ''
|
user_info['login_id'] = profile.login_id if profile and profile.login_id else ''
|
||||||
|
|
||||||
user_info['is_staff'] = user.is_staff
|
user_info['is_staff'] = user.is_staff
|
||||||
@@ -76,16 +84,17 @@ class AdminAdminUsersBatch(APIView):
|
|||||||
user_info['quota_usage'] = seafile_api.get_user_self_usage(user.email)
|
user_info['quota_usage'] = seafile_api.get_user_self_usage(user.email)
|
||||||
|
|
||||||
user_info['create_time'] = timestamp_to_isoformat_timestr(user.ctime)
|
user_info['create_time'] = timestamp_to_isoformat_timestr(user.ctime)
|
||||||
user_info['last_login'] = UserLastLogin.objects.get_by_username(user.email).last_login if UserLastLogin.objects.get_by_username(user.email) else ''
|
last_login_obj = UserLastLogin.objects.get_by_username(user.email)
|
||||||
|
user_info['last_login'] = datetime_to_isoformat_timestr(last_login_obj.last_login) if last_login_obj else ''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
admin_role = AdminRole.objects.get_admin_role(user.email)
|
admin_role = AdminRole.objects.get_admin_role(user.email)
|
||||||
user_info['admin_role'] = admin_role.role
|
user_info['admin_role'] = admin_role.role
|
||||||
except AdminRole.DoesNotExist:
|
except AdminRole.DoesNotExist:
|
||||||
user_info['admin_role'] = DEFAULT_ADMIN
|
user_info['admin_role'] = DEFAULT_ADMIN
|
||||||
new_admin_info.append(user_info)
|
result['success'].append(user_info)
|
||||||
|
|
||||||
return Response({'new_admin_user_list': new_admin_info})
|
return Response(result)
|
||||||
|
|
||||||
|
|
||||||
class AdminUsersBatch(APIView):
|
class AdminUsersBatch(APIView):
|
||||||
|
Reference in New Issue
Block a user