def get(self): try: self.response.headers['Content-Type'] = 'text/json' user = users.get_current_user() user_id = user.user_id() response = {} entity = CartModel(id=user_id).get_by_id(user_id) if "to_dict" in dir(entity): response['success'] = True response['entity'] = entity.to_dict() self.response.set_status(200) self.response.write(json.dumps(response)) else: self.response.set_status(404) response['success'] = True response['message'] = "Cart does not exist." self.response.write(json.dumps(response)) except Exception, e: log.debug(e) self.response.set_status(404) self.response.write( json.dumps({ "success": False, "message": "ViewHandler Exception: " + e.message }))
def post(self): log.info("create") try: user = users.get_current_user() user_id = user.user_id() 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 cart_items = jdata['cart_items'] new_cart = CartModel(id=user_id, cart_items=cart_items) result = new_cart.put() if result: entity = result.get().to_dict() entity['success'] = True self.response.set_status(201) self.response.write(json.dumps(entity))
def get(self, id): try: self.response.headers['Content-Type'] = 'text/json' user = users.get_current_user() user_id = user.user_id() response = {} entity = CartModel(id=user_id).get_by_id(user_id) if "to_dict" in dir(entity): response['success'] = True response['entity'] = entity.to_dict() self.response.set_status(200) self.response.write(json.dumps(response)) else: self.response.set_status(404) response['success'] = True response['message'] = "Cart does not exist." self.response.write(json.dumps(response)) except Exception, e: log.debug(e) self.response.set_status(404) self.response.write( json.dumps({ "success": False, "message": "ViewHandler Exception: " + e.message }))
def post(self): log.info("create") try: user = users.get_current_user() user_id = user.user_id() 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 cart_items = jdata['cart_items'] new_cart = CartModel( id = user_id, cart_items = cart_items ) result = new_cart.put() if result: entity = result.get().to_dict() entity['success'] = True self.response.set_status(201) self.response.write(json.dumps(entity))
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...')
def mutate(self, info, product_id=None): cart = CartModel(total=0) db_session.add(cart) db_session.commit() if product_id is not None: local_product_id=from_global_id(product_id)[1] item = ItemModel(cart_id=cart.id,product_id=local_product_id) db_session.add(item) db_session.commit() return CartCreate(cart=cart)
def delete(self): try: self.response.headers['Content-Type'] = 'text/json' user = users.get_current_user() user_id = user.user_id() try: entity = CartModel(id=user_id).get_by_id(user_id) if "key" in dir(entity): entity.key.delete() self.response.set_status(200) self.response.write( json.dumps({ "success": True, "entity": { "id": user_id } })) else: self.response.set_status(200) self.response.write( json.dumps({ "success": True, "message": "entity did not exist." })) except AttributeError, e: log.debug(e) self.response.set_status(404) self.response.write( json.dumps({ "success": False, "message": "DeleteHandler Exception: " + e.message })) except Exception, e: # Should Log to console - exception message log.debug(e) self.response.set_status(404) self.response.write( json.dumps({ "success": False, "message": "DeleteHandler Exception: " + e.message }))
def get(self): try: self.response.headers['Content-Type'] = 'text/json' entities = CartModel().list() response = {"success": True, "entities": {}} for entity in entities.fetch(): key_id = entity.key.id() response['entities'][key_id] = (entity.to_dict()) self.response.write(json.dumps(response)) except Exception, e: # Should Log to console - exception message log.debug(e) self.response.write( json.dumps({ "success": False, "message": "ViewHandler Exception: " + e.message }))
def get(self): try: self.response.headers['Content-Type'] = 'text/json' entities = CartModel().list() response = {"success": True, "entities":{}} for entity in entities.fetch(): key_id = entity.key.id() response['entities'][key_id] = (entity.to_dict()) self.response.write(json.dumps(response)) except Exception, e: # Should Log to console - exception message log.debug(e) self.response.write( json.dumps({ "success": False, "message": "ViewHandler Exception: " + e.message }))
def new(self, request): cart = CartModel() cart.save() request.session[CART_ID] = cart.id return cart