Exemplo n.º 1
0
  def post(self):
    try:
      
      self.response.headers['Content-Type'] = 'text/json'

      try:
        jdata = json.loads(self.request.body)
      except ValueError, e:
        log.debug(e)
        self.response.set_status(404)
        msg = {'success': False, 'message': "Failed to read JSON body"}
        self.response.write(json.dumps(msg))
        return
    
      new_product = ProductModel(
        product_id=jdata['product_id'],
        manufacturer_id=jdata['manufacturer_id'],
        name=jdata['name'],
        price=jdata['price'],
        qty=jdata['qty'] or 0
      )
      
      result = new_product.put()
      
      if result:
        entity = result.get().to_dict()
        entity['success'] = True
        self.response.set_status(201)
        self.response.write(json.dumps(entity))
Exemplo n.º 2
0
  def put(self, id):
    try:

      self.response.headers['Content-Type'] = 'text/json'

      if not id:
        self.response.set_status(204)
        self.response.write("{\"id value missing from url. aborted update.\"}")
        return

      try:
        jdata = json.loads(self.request.body)
      except ValueError, e:
        log.debug(e)
        self.response.set_status(404)
        msg = {'success': False, 'message': "Failed to read JSON body"}
        self.response.write(json.dumps(msg))
        return
    
      entity = ProductModel(id=int(id)).get_by_id(int(id))
      
      entity.product_id           = jdata['product_id']
      entity.manufacturer_id      = jdata['manufacturer_id']
      entity.name                 = jdata['name']
      entity.qty                  = jdata['qty']
      entity.price                = jdata['price']
      result = entity.put()

      if result:
        rdata = {}
        rdata['success'] = True
        rdata['entity'] = {
          "product_id":       entity.product_id,
          "manufacturer_id":  entity.manufacturer_id,
          "name":             entity.name,
          "qty":              entity.qty
        }
        self.response.set_status(200)
        self.response.write(json.dumps(rdata))