Skip to content

Commit

Permalink
set user is_active
Browse files Browse the repository at this point in the history
  • Loading branch information
imwhatiam committed May 17, 2024
1 parent efd49b8 commit 758886c
Showing 1 changed file with 46 additions and 21 deletions.
67 changes: 46 additions & 21 deletions seahub/institutions/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
from seahub.profile.models import Profile
from seahub.institutions.models import InstitutionAdmin
from seahub.institutions.utils import get_institution_available_quota
from seahub.utils import inactive_user
from seahub.utils.timeutils import datetime_to_isoformat_timestr
from seahub.utils.timeutils import timestamp_to_isoformat_timestr
from seahub.utils.file_size import get_file_size_unit
from seahub.utils.licenseparse import user_number_over_limit
from seahub.avatar.templatetags.avatar_tags import api_avatar_url


Expand Down Expand Up @@ -144,15 +146,15 @@ class InstAdminUser(APIView):

def put(self, request, email):

""" Set user info in institution.
""" Update user info in institution.
"""

username = request.user.username
inst_admin = InstitutionAdmin.objects.get(user=username)
inst = inst_admin.institution

try:
User.objects.get(email=email)
user_obj = User.objects.get(email=email)
except User.DoesNotExist:
error_msg = f'User {email} not found.'
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
Expand All @@ -165,29 +167,51 @@ def put(self, request, email):

# set user quota
quota_total_mb = request.data.get("quota_total", None)
if not quota_total_mb:
error_msg = 'quota_total invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
if quota_total_mb is not None:
try:
quota_total_mb = int(quota_total_mb)
except ValueError:
error_msg = _("Must be an integer that is greater than or equal to 0.")
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

try:
quota_total_mb = int(quota_total_mb)
except ValueError:
error_msg = _("Must be an integer that is greater than or equal to 0.")
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
if quota_total_mb < 0:
error_msg = _("Space quota is too low (minimum value is 0).")
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

if quota_total_mb < 0:
error_msg = _("Space quota is too low (minimum value is 0).")
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
quota = quota_total_mb * get_file_size_unit('MB')
available_quota = get_institution_available_quota(inst)
if available_quota is not None:
# None means has unlimit quota
if available_quota == 0 or available_quota < quota:
error_msg = _(f"Failed to set quota: maximum quota is {available_quota} MB")
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

seafile_api.set_user_quota(email, quota)

is_active = request.data.get("is_active", None)
if is_active is not None:

quota = quota_total_mb * get_file_size_unit('MB')
available_quota = get_institution_available_quota(inst)
if available_quota is not None:
# None means has unlimit quota
if available_quota == 0 or available_quota < quota:
error_msg = _(f"Failed to set quota: maximum quota is {available_quota} MB")
is_active = is_active.lower()
if is_active not in ('true', 'false'):
error_msg = "is_active invalid."
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

seafile_api.set_user_quota(email, quota)
if is_active == 'true':

if user_number_over_limit(new_users=1):
error_msg = _("The number of users exceeds the limit.")
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
else:
# del tokens and personal repo api tokens
try:
inactive_user(email)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

user_obj.is_active = is_active == 'true'
user_obj.save()

return Response({'success': True})

Expand All @@ -202,7 +226,7 @@ def get(self, request, email):

# get user info
try:
User.objects.get(email=email)
user_obj = User.objects.get(email=email)
except User.DoesNotExist:
error_msg = f'User {email} not found.'
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
Expand All @@ -217,6 +241,7 @@ def get(self, request, email):
user_info['email'] = email
user_info['name'] = email2nickname(email)
user_info['contact_email'] = email2contact_email(email)
user_info['is_active'] = user_obj.is_active
user_info['avatar_url'], _, _ = api_avatar_url(email, 72)
try:
user_info['quota_total'] = seafile_api.get_user_quota(email)
Expand Down

0 comments on commit 758886c

Please sign in to comment.