Ejemplo n.º 1
0
  def post(self):
    """ Create an entity of model given by path /classname.

        Request body is JSON for a jobj for a new entity (without id!).
        Response is JSON for a jobj for a newly created entity.
        Also sets HTTP Location: header to /classname/id for new entity.
    """
    user = users.get_current_user()
    if user is None:
      return self.redirect(users.create_login_url(self.request.uri))
    
    failed, model, entity = self._get_model_and_entity(True, False, user)
    if failed: return
    if entity is not None:
      self.response.set_status(400, 'Cannot create entity with fixed ID.')
      return
    jobj = jsonutil.receive_json(self.request)
    if hasattr(model, 'check_existing'):
      existing = model.check_existing(jobj)
      if existing:
        new_entity_path = "/%s/%d" % (self._classname, jsonutil.id_of(existing)['id'])
        self.response.headers['Location'] = new_entity_path
        self.response.set_status(302, 'Found existing object %s' % new_entity_path)
        return
        
    jobj = jsonutil.make_entity(model, jobj, user=user)
    self._serve(jobj)
    new_entity_path = "/%s/%s" % (self._classname, jobj['id'])
    logging.info('Post created %r', new_entity_path)
    self.response.headers['Location'] = new_entity_path
    self.response.set_status(201, 'Created entity %s' % new_entity_path)
Ejemplo n.º 2
0
  def get(self):
    """ Get JSON data for model names, entity IDs of a model, or an entity.

    Depending on the request path, serve as JSON to the response object:
    - for a path of /classname/id, a jobj for that entity
    - for a path of /classname, a list of id-only jobjs for that model
    - for a path of /, a list of all model classnames
    """
    user = users.get_current_user()
    if user is None:
      return self.redirect(users.create_login_url(self.request.uri))

    coon = str(1 + int(self.get_cookie('coon', '0')))
    self.set_cookie('count', coon)
    self.set_cookie('ts', str(int(time.time())))
    failed, model, entity = self._get_model_and_entity(False, False, user)
    if failed: return
    if model is None:
      return self._serve(restutil.allModelClassNames())
    if entity is None:
      return self._serve([jsonutil.id_of(x) for x in model.all()])
    jobj = jsonutil.make_jobj(entity)
    return self._serve(jobj)
Ejemplo n.º 3
0
 def do_get_model(self, model):
   """ Hook method to R/O "call a model" ("get list of all its IDs"...?)
   """
   themodel = self.get_model(model)
   if themodel is None: return ''
   return [jsonutil.id_of(x) for x in themodel.all()]
Ejemplo n.º 4
0
 def do_get_model(self, model):
     """ Hook method to R/O "call a model" ("get list of all its IDs"...?)
 """
     themodel = self.get_model(model)
     if themodel is None: return ''
     return [jsonutil.id_of(x) for x in themodel.all()]