Example #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)
Example #2
0
 def do_post_model(self, model):
   """ Hook method to "call a model" (to create an entity)
   """
   themodel = self.get_model(model)
   if themodel is None: return ''
   jobj = jsonutil.receive_json(self.handler.request)
   jobj = jsonutil.make_entity(themodel, jobj)
   self._classname = model
   return jobj
Example #3
0
 def do_post_model(self, model):
     """ Hook method to "call a model" (to create an entity)
 """
     themodel = self.get_model(model)
     if themodel is None: return ''
     jobj = jsonutil.receive_json(self.handler.request)
     jobj = jsonutil.make_entity(themodel, jobj)
     self._classname = model
     return jobj
Example #4
0
 def do_put(self, model, strid):
   """ Hook method to update an entity given modelname and strid.
   """
   entity = self.get_entity(model, strid)
   if entity is None:
     return {}
   jobj = jsonutil.receive_json(self.handler.request)
   jobj = jsonutil.update_entity(entity, jobj)
   updated_entity_path = "/%s/%s" % (model, jobj['id'])
   self.handler.response.set_status(200, 'Updated entity %s' %
                                          updated_entity_path)
   return jobj
Example #5
0
 def do_put(self, model, strid):
     """ Hook method to update an entity given modelname and strid.
 """
     entity = self.get_entity(model, strid)
     if entity is None:
         return {}
     jobj = jsonutil.receive_json(self.handler.request)
     jobj = jsonutil.update_entity(entity, jobj)
     updated_entity_path = "/%s/%s" % (model, jobj['id'])
     self.handler.response.set_status(
         200, 'Updated entity %s' % updated_entity_path)
     return jobj
Example #6
0
  def put(self):
    """ Update an entity of model given by path /classname/id.

        Request body is JSON for a jobj for an existing entity.
        Response is JSON for a jobj for the updated 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, True, user)
    if failed: return
    jobj = jsonutil.receive_json(self.request)
    jobj = jsonutil.update_entity(entity, jobj)
    self._serve(jobj)
    updated_entity_path = "/%s/%s" % (self._classname, jobj['id'])
    self.response.set_status(200, 'Updated entity %s' % updated_entity_path)