示例#1
0
 def add_user(self, name, public_key=None, override=True):
   if (name in self.users) and (not override):
     return
   
   validations.validate_username(name)
   
   if public_key:
     validations.validate_public_key(public_key)
   
   self.users[name] = {'public_key': public_key}
 def add_user(self, name, public_key=None, override=True):
   if (name in self.users) and (not override):
     return
   
   validations.validate_username(name)
   
   if public_key:
     validations.validate_public_key(public_key)
   
   self.users[name] = {'public_key': public_key}
def add_user(request):
    """
  <Purpose>
    Reponds to an HTML <form> submission to create a new user. Designed for use
    in a hidden <iframe>, due to browser limitations on file uploading.
  <Arguments>
    request:
      A Django request with user info in the POST data.
  <Exceptions>
    None.
  <Side Effects>
    None.
  <Returns>
    A Django response containing a JSON representation of the new user, or an
    error.
  """
    def ErrorResponse(message=''):
        return TextResponse(json.dumps({'error': message}))

    if 'name' not in request.POST:
        return ErrorResponse('User name not specified.')

    name = request.POST['name']
    public_key = None

    if 'public_key' in request.FILES:
        public_file = request.FILES['public_key']
        public_key = public_file.read().strip()

    try:
        validations.validate_username(name)

        if public_key is not None:
            validations.validate_public_key(public_key)

    except validations.ValidationError as e:
        return ErrorResponse(e)
    except:
        log_exception(request)
        return ErrorResponse('Unknown error occured while trying to add user.')
    else:
        return TextResponse(
            json.dumps({'user': {
                'name': name,
                'public_key': public_key
            }}))
def add_user(request):
  """
  <Purpose>
    Reponds to an HTML <form> submission to create a new user. Designed for use
    in a hidden <iframe>, due to browser limitations on file uploading.
  <Arguments>
    request:
      A Django request with user info in the POST data.
  <Exceptions>
    None.
  <Side Effects>
    None.
  <Returns>
    A Django response containing a JSON representation of the new user, or an
    error.
  """
  
  def ErrorResponse(message=''):
    return TextResponse(json.dumps({'error': message}))
    
  if 'name' not in request.POST:
    return ErrorResponse('User name not specified.')
        
  name = request.POST['name']
  public_key = None

  if 'public_key' in request.FILES:
    public_file = request.FILES['public_key']
    public_key = public_file.read().strip()
        
  try:
    validations.validate_username(name)
        
    if public_key is not None:
      validations.validate_public_key(public_key)
        
  except validations.ValidationError as e:
    return ErrorResponse(e)
  except:
    log_exception(request)
    return ErrorResponse('Unknown error occured while trying to add user.')
  else:
    return TextResponse(json.dumps({'user': {'name': name, 'public_key': public_key}}))