def validate_string_length(s, min_len=0, max_len=None): """Check whter len(s) in [min_length, max_length] min_length = 0, max_length = infinite if undefined Return True is valid else False """ if min_len > string_len(s): raise ValueError(_('length less than {}').format(min_len)) if max_len and max_len < string_len(s): raise ValueError(_('length more than {}').format(max_len))
def check_common_params(self, post_vars): required_post_vars = ['username', 'password'] for k in required_post_vars: if k not in post_vars: raise error.Error(error.MISSING_PARAMETER, u'缺少参数{}'.format(k)) # Check paremeters for k in required_post_vars: if len(post_vars[k]) < 2: error_str = { 'username': (error.USERNAME_LENGHT_TOO_SHORT, _(u'用户名至少需要2个字符')), 'password': (error.PASSWORD_LENGHT_TOO_SHORT, _(u'密码长度不能小于2')), } raise error.Error(error_str[k][0], error_str[k][1]) if string_len(post_vars['username']) > 30: raise error.Error(error.USERNAME_LENGHT_TOO_LONG, _(u'用户名最多30个字符')) if ALL_NUMBER_RE.match(post_vars['username']): raise error.Error(error.USERNAME_CANT_ALL_NUMBER, _(u'用户名不能都为数字')) if not USERNAME_RE.match(post_vars['username']): raise error.Error(error.USERNAME_FORMAT_ERROR, _(u'用户名只能包含中文字符、英文字母、数字、"_"及"-",不能包含空格'))
def post(self, request, format=None): post_vars = json.loads(request.POST.keys()[0]) required_post_vars = ['email', 'username', 'password'] err = {} # Confirm we have a properly formed request for k in required_post_vars: if k not in post_vars: err['err_type'] = 'MissingParameter' err['err_msg'] = "Missing Parameter %s" % k return Response(err, status=status.HTTP_400_BAD_REQUEST) # Check paremeters for k in required_post_vars: if len(post_vars[k]) < 2: error_str = { 'username': _('Username must be minimum of two characters long.'), 'email': _('A properly formatted e-mail is required.'), 'password': _('A valid password is required.') } err['err_type'] = 'InvalidParameter' err['err_msg'] = error_str[k] return Response(err, status=status.HTTP_400_BAD_REQUEST) # Validate email try: validate_email(post_vars['email']) except ValidationError: err['err_type'] = 'InvalidParameter' err['err_msg'] = _("Valid e-mail is required.") return Response(err, status=status.HTTP_400_BAD_REQUEST) # Validate username if string_len(post_vars['username']) > 16: error_str = { 'username': _('Username must be maximum of eight characters long.'), } err['err_type'] = 'InvalidParameter' err['err_msg'] = error_str['username'] return Response(err, status=status.HTTP_400_BAD_REQUEST) if ALL_NUMBER_RE.match(post_vars['username']): err['err_type'] = 'InvalidParameter' err['err_msg'] = _('Username cannot be all Numbers.') return Response(err, status=status.HTTP_400_BAD_REQUEST) if not USERNAME_RE.match(post_vars['username']): err['err_type'] = 'InvalidParameter' err['err_msg'] = _('Username should only consist of A-Z and 0-9 and chinese character and "_" and "-", with no spaces.') return Response(err, status=status.HTTP_400_BAD_REQUEST) # Ok, looks like everything is legit. Create the account. post_vars['name'] = '' ret = _do_create_account(post_vars, True, request=request) # if there was an error then return that if isinstance(ret, HttpResponse): json_obj = json.loads(ret.content) err['err_type'] = '%sAlreadyExists' % json_obj.get('field').capitalize() err['err_msg'] = json_obj.get('value') return Response(err, status=status.HTTP_400_BAD_REQUEST) user, profile, registration = ret server_track(request, 'api.user.register', { 'uid': user.id, 'username': user.username, }) return Response()