def _create_account(self, request, email): copy = request.data.copy() copy['email'] = email serializer = AccountSerializer(data=copy) if serializer.is_valid(): try: user = User.objects.create_user(serializer.data['email'], serializer.data['password'], serializer.data['is_staff'], serializer.data['is_active']) except User.DoesNotExist as e: logger.error(e) return api_error(status.HTTP_520_OPERATION_FAILED, 'Failed to add user.') self._update_account_profile(request, user.username) resp = Response('success', status=status.HTTP_201_CREATED) resp['Location'] = reverse('api2-account', args=[email]) return resp else: return api_error(status.HTTP_400_BAD_REQUEST, serializer.errors)
def _create_account(self, request, email): copy = request.DATA.copy() copy['email'] = email serializer = AccountSerializer(data=copy) if serializer.is_valid(): try: user = User.objects.create_user(serializer.object['email'], serializer.object['password'], serializer.object['is_staff'], serializer.object['is_active']) except User.DoesNotExist as e: logger.error(e) return api_error(status.HTTP_403_FORBIDDEN, 'Fail to add user.') self._update_account_profile(request, user.username) resp = Response('success', status=status.HTTP_201_CREATED) resp['Location'] = reverse('api2-account', args=[email]) return resp else: return api_error(status.HTTP_400_BAD_REQUEST, serializer.errors)
def _create_account(self, request, email): copy = request.DATA.copy() copy["email"] = email serializer = AccountSerializer(data=copy) if serializer.is_valid(): try: user = User.objects.create_user( serializer.object["email"], serializer.object["password"], serializer.object["is_staff"], serializer.object["is_active"], ) except User.DoesNotExist as e: logger.error(e) return api_error(status.HTTP_403_FORBIDDEN, "Fail to add user.") self._update_account_profile(request, user.username) resp = Response("success", status=status.HTTP_201_CREATED) resp["Location"] = reverse("api2-account", args=[email]) return resp else: return api_error(status.HTTP_400_BAD_REQUEST, serializer.errors)
def put(self, request, email, format=None): # argument check for email if not is_valid_username(email): return api_error(status.HTTP_400_BAD_REQUEST, 'Email %s invalid.' % email) # argument check for name name = request.data.get("name", None) if name is not None: if len(name) > 64: return api_error( status.HTTP_400_BAD_REQUEST, _(u'Name is too long (maximum is 64 characters)')) if "/" in name: return api_error(status.HTTP_400_BAD_REQUEST, _(u"Name should not include '/'.")) # argument check for department department = request.data.get("department", None) if department is not None: if len(department) > 512: return api_error( status.HTTP_400_BAD_REQUEST, _(u'Department is too long (maximum is 512 characters)')) # argument check for storage space_quota_mb = request.data.get("storage", None) if space_quota_mb is not None: if space_quota_mb == '': return api_error(status.HTTP_400_BAD_REQUEST, _('Space quota can\'t be empty')) try: space_quota_mb = int(space_quota_mb) except ValueError: return api_error( status.HTTP_400_BAD_REQUEST, _('Must be an integer that is greater than or equal to 0.') ) if space_quota_mb < 0: return api_error( status.HTTP_400_BAD_REQUEST, _('Space quota is too low (minimum value is 0)')) if is_org_context(request): org_id = request.user.org.org_id org_quota_mb = seaserv.seafserv_threaded_rpc.get_org_quota(org_id) / \ get_file_size_unit('MB') if space_quota_mb > org_quota_mb: return api_error(status.HTTP_400_BAD_REQUEST, \ _(u'Failed to set quota: maximum quota is %d MB' % org_quota_mb)) # argument check for is_trial is_trial = request.data.get("is_trial", None) if is_trial is not None: try: is_trial = to_python_boolean(is_trial) except ValueError: return api_error(status.HTTP_400_BAD_REQUEST, 'is_trial invalid') try: # update account basic info user = User.objects.get(email=email) # argument check for is_staff is_staff = request.data.get("is_staff", None) if is_staff is not None: try: is_staff = to_python_boolean(is_staff) except ValueError: return api_error(status.HTTP_400_BAD_REQUEST, 'is_staff invalid.') user.is_staff = is_staff # argument check for is_active is_active = request.data.get("is_active", None) if is_active is not None: try: is_active = to_python_boolean(is_active) except ValueError: return api_error(status.HTTP_400_BAD_REQUEST, 'is_active invalid.') user.is_active = is_active # update password password = request.data.get("password", None) if password is not None: user.set_password(password) # save user result_code = user.save() if result_code == -1: return api_error(status.HTTP_520_OPERATION_FAILED, 'Failed to update user.') try: # update account additional info self._update_account_additional_info(request, email) except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') # get account info and return info = get_account_info(user) return Response(info) except User.DoesNotExist: # create user account copy = request.data.copy() copy['email'] = email serializer = AccountSerializer(data=copy) if not serializer.is_valid(): return api_error(status.HTTP_400_BAD_REQUEST, serializer.errors) try: user = User.objects.create_user(serializer.data['email'], serializer.data['password'], serializer.data['is_staff'], serializer.data['is_active']) except User.DoesNotExist as e: logger.error(e) return api_error(status.HTTP_520_OPERATION_FAILED, 'Failed to add user.') try: # update account additional info self._update_account_additional_info(request, email) except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') # get account info and return info = get_account_info(user) resp = Response(info, status=status.HTTP_201_CREATED) resp['Location'] = reverse('api2-account', args=[email]) return resp
def put(self, request, email, format=None): # argument check for email if not is_valid_username(email): return api_error(status.HTTP_400_BAD_REQUEST, 'Email %s invalid.' % email) # argument check for name name = request.data.get("name", None) if name is not None: if len(name) > 64: return api_error(status.HTTP_400_BAD_REQUEST, _(u'Name is too long (maximum is 64 characters)')) if "/" in name: return api_error(status.HTTP_400_BAD_REQUEST, _(u"Name should not include '/'.")) # argument check for list_in_address_book list_in_address_book = request.data.get("list_in_address_book", None) if list_in_address_book is not None: if list_in_address_book.lower() not in ('true', 'false'): return api_error(status.HTTP_400_BAD_REQUEST, 'list_in_address_book invalid') #argument check for loginid loginid = request.data.get("login_id", None) if loginid is not None: loginid = loginid.strip() if loginid == "": return api_error(status.HTTP_400_BAD_REQUEST, _(u"Login id can't be empty")) usernamebyloginid = Profile.objects.get_username_by_login_id(loginid) if usernamebyloginid is not None: return api_error(status.HTTP_400_BAD_REQUEST, _(u"Login id %s already exists." % loginid)) # argument check for department department = request.data.get("department", None) if department is not None: if len(department) > 512: return api_error(status.HTTP_400_BAD_REQUEST, _(u'Department is too long (maximum is 512 characters)')) # argument check for institution institution = request.data.get("institution", None) if institution is not None and institution != '': try: obj_insti = Institution.objects.get(name=institution) except Institution.DoesNotExist: return api_error(status.HTTP_400_BAD_REQUEST, "Institution %s does not exist" % institution) # argument check for storage space_quota_mb = request.data.get("storage", None) if space_quota_mb is not None: if space_quota_mb == '': return api_error(status.HTTP_400_BAD_REQUEST, _('Space quota can\'t be empty')) try: space_quota_mb = int(space_quota_mb) except ValueError: return api_error(status.HTTP_400_BAD_REQUEST, _('Must be an integer that is greater than or equal to 0.')) if space_quota_mb < 0: return api_error(status.HTTP_400_BAD_REQUEST, _('Space quota is too low (minimum value is 0)')) if is_org_context(request): org_id = request.user.org.org_id org_quota_mb = seaserv.seafserv_threaded_rpc.get_org_quota(org_id) / \ get_file_size_unit('MB') if space_quota_mb > org_quota_mb: return api_error(status.HTTP_400_BAD_REQUEST, \ _(u'Failed to set quota: maximum quota is %d MB' % org_quota_mb)) # argument check for is_trial is_trial = request.data.get("is_trial", None) if is_trial is not None: try: is_trial = to_python_boolean(is_trial) except ValueError: return api_error(status.HTTP_400_BAD_REQUEST, 'is_trial invalid') try: # update account basic info user = User.objects.get(email=email) # argument check for is_staff is_staff = request.data.get("is_staff", None) if is_staff is not None: try: is_staff = to_python_boolean(is_staff) except ValueError: return api_error(status.HTTP_400_BAD_REQUEST, 'is_staff invalid.') user.is_staff = is_staff # argument check for is_active is_active = request.data.get("is_active", None) if is_active is not None: try: is_active = to_python_boolean(is_active) except ValueError: return api_error(status.HTTP_400_BAD_REQUEST, 'is_active invalid.') user.is_active = is_active # update password password = request.data.get("password", None) if password is not None: user.set_password(password) # save user result_code = user.save() if result_code == -1: return api_error(status.HTTP_520_OPERATION_FAILED, 'Failed to update user.') try: # update account additional info self._update_account_additional_info(request, email) except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') # get account info and return info = get_account_info(user) return Response(info) except User.DoesNotExist: # create user account copy = request.data.copy() copy['email'] = email serializer = AccountSerializer(data=copy) if not serializer.is_valid(): return api_error(status.HTTP_400_BAD_REQUEST, serializer.errors) try: user = User.objects.create_user(serializer.data['email'], serializer.data['password'], serializer.data['is_staff'], serializer.data['is_active']) except User.DoesNotExist as e: logger.error(e) return api_error(status.HTTP_520_OPERATION_FAILED, 'Failed to add user.') try: # update account additional info self._update_account_additional_info(request, email) except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') # get account info and return info = get_account_info(user) resp = Response(info, status=status.HTTP_201_CREATED) resp['Location'] = reverse('api2-account', args=[email]) return resp