Esempio n. 1
0
  def post(self):
    log.info("create")
    try:
      
      user = users.get_current_user()
      user_id = user.user_id()

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

      cart_entity = CartModel(id=user_id).get_by_id(user_id)
      log.info(cart_entity)
      if "to_dict" in dir(cart_entity):
        new_order = OrderModel(
          user_id    = user_id,
          cart  = cart_entity
        )
      else:
        self.response.set_status(404)
        self.response.write(json.dumps({
          "message": "Could not place order. No cart Found.",
          "success": False
        }))
        return
      
      result = new_order.put()
      if result:
        entity = result.get().to_dict()
        entity['success'] = True
        self.response.set_status(201)
        self.response.write(json.dumps(entity))
    
    except users.UserNotFoundError, e:
      # Should never happen - just incase of solar flares
      log.debug('User was not found...')
Esempio n. 2
0
  def put(self, id):
    try:

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

      user = users.get_current_user()
      user_id = user.user_id()

      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 = OrderModel(id=int(id)).get_by_id(int(id))
      
      entity.id        = id 
      entity.user_id   = user_id
      entity.cart      = jdata['cart']
      entity.created   = jdata['created'] 

      result = entity.put()

      if result:
        entity = result.get().to_dict()
        entity['success'] = True

        self.response.set_status(200)
        self.response.write(json.dumps(entity))