def get(self): """ Returns all of the Shopcarts """ logger.info('Request to list Shopcarts...') args = shopcart_args.parse_args() if args['user_id']: logger.info('Find by user') shopcarts = Shopcart.find_by_user(args['user_id']) else: logger.info('Find all') shopcarts = Shopcart.all() results = [shopcart.serialize() for shopcart in shopcarts] logger.info('[%s] Shopcarts returned', len(results)) return results, status.HTTP_200_OK
def post(self): """ Create a Shopcart """ logger.info("Request to create a shopcart") check_content_type("application/json") logger.debug('Payload = %s', api.payload) shopcart = None if 'user_id' in api.payload: shopcart = Shopcart.find_by_user(api.payload['user_id']).first() if shopcart is None: shopcart = Shopcart() shopcart.deserialize(api.payload) shopcart.create() logger.info("Shopcart with ID [%s] created.", shopcart.id) location_url = api.url_for(ShopcartResource, shopcart_id=shopcart.id, _external=True) return shopcart.serialize(), status.HTTP_201_CREATED, {"Location": location_url}
def test_find_by_user(self): """ Find a Shopcart by user """ Shopcart(user_id=101).create() Shopcart(user_id=201).create() shopcarts = Shopcart.find_by_user(101) self.assertEqual(shopcarts[0].user_id, 101)