示例#1
0
  def get_model(self, modelname):
    """ Gets a model (or None) given a model name.

    Args:
      modelname: a string that should name a model
    Returns:
      a model class, or None (if no model's registered with that name)
    Side effects:
      sets response status to 400 if no model's registered with that name
    """
    model = restutil.modelClassFromName(modelname)
    if model is None:
      self.handler.response.set_status(400, 'Model %r not found' % modelname)
    return model
示例#2
0
    def get_model(self, modelname):
        """ Gets a model (or None) given a model name.

    Args:
      modelname: a string that should name a model
    Returns:
      a model class, or None (if no model's registered with that name)
    Side effects:
      sets response status to 400 if no model's registered with that name
    """
        model = restutil.modelClassFromName(modelname)
        if model is None:
            self.handler.response.set_status(400,
                                             'Model %r not found' % modelname)
        return model
示例#3
0
  def _get_model_and_entity(self, need_model, need_id, user):
    """ Analyze self.request.path to get model and entity.

    Args:
      need_model: bool: if True, fail if classname is missing
      need_id: bool: if True, fail if ID is missing

    Returns 3-item tuple:
      failed: bool: True iff has failed
      model: class object or None
      entity: instance of model or None
    """
    classname, strid = jsonutil.path_to_classname_and_id(self.request.path)
    self._classname = classname
    if not classname:
      if need_model:
        self.response.set_status(400, 'Cannot do it without a model.')
      return need_model, None, None
    model = restutil.modelClassFromName(classname)
    if model is None:
      self.response.set_status(400, 'Model %r not found' % classname)
      return True, None, None
    if not strid:
      if need_id:
        self.response.set_status(400, 'Cannot do it without an ID.')
      return need_id, model, None
    try:
      numid = int(strid)
    except TypeError:
      self.response.set_status(400, 'ID %r is not numeric.' % strid)
      return True, model, None
    else:
      entity = model.get_by_id(numid)
      if entity is None:
        self.response.set_status(404, "Entity %s not found" % self.request.path)
        return True, model, None
    return False, model, entity