def get(): item_id = request.args.get('id') user_id = request.args.get('user_id') current_app.logger.debug(user_id) item = Item.getProductById(item_id=item_id, user_id=user_id) item_schema = ItemSchema() current_app.logger.debug(item) return_item = { 'item': item_schema.dump(item[0]), 'favorite': item[1], 'set_count': 0 if item[2] is None else int(item[2]) } return jsonify({'state': True, 'entries': return_item})
def test_get_item_include_pictures__success(self): data, errors = ItemSchema.jsonapi( self.item1, include_data=['pictures']) assert errors == {} expected_result = EXPECTED_ITEMS['get_item_include_pictures__success'] assert_valid_response(data, expected_result)
def getByUserId(): current_app.logger.debug(request.args) items = Item.getItemsBySearch(args=request.args.to_dict(), user_id=request.args.get('user_id')) koes = Koe.getRecordsBySearch(args=request.args.to_dict()) # items = Item.getItemsByUserId(user_id=user_id) # item_schema = ItemSchema() current_app.logger.debug(koes) current_app.logger.debug(items) itemsResult = [] for value in items: favorite = value[1] set_count = value[2] itemsResult.append({ 'item': ItemSchema().dump(value[0]), 'favorite': favorite, 'set_count': 0 if set_count is None else int(set_count) }) koe_schema = KoeSchema(many=True) current_app.logger.debug(itemsResult) return jsonify({ 'state': True, 'entries': { 'items': itemsResult, 'koes': koe_schema.dump(koes) } })
class ItemResource(Resource): schema = ItemSchema() def get(self, itemId): item = Item.objects.get(id=itemId) result = self.schema.dump(item) return {'result': result}
class ItemsListResource(Resource): schema = ItemSchema(many=True) def get(self): items = Item.objects result = self.schema.dump(items) return {'result': result}
def getByUserId(): user_id = request.args.get('user_id') current_app.logger.debug(user_id) items = Item.getItemsByUserId(user_id=user_id) item_schema = ItemSchema() current_app.logger.debug(items) result = [] for value in items: favorite = value[1] set_count = value[2] result.append({ 'item': ItemSchema().dump(value[0]), 'favorite': favorite, 'set_count': 0 if set_count is None else int(set_count) }) current_app.logger.debug(result) return jsonify({'state': True, 'entries': result})
def post(): jsonData = json.dumps(request.json) userData = json.loads(jsonData) json_dict = json.loads(request.form['params']) # app.logger.debug(json_dict) # app.logger.debug(request.files) item = Item(name=json_dict['name'], description=json_dict['description'], period=json_dict['period'], remaining_days=json_dict['remaining_days'], remaining_format_id=json_dict['remaining_format_id'], category_id=json_dict['category_id'], shipment=json_dict['shipment'], price=json_dict['price'], volume=json_dict['volume'], area=json_dict['area'], user_id=json_dict['user_id']) db.session.add(item) db.session.commit() db.session.refresh(item) item_schema = ItemSchema() # post_record = item.__dict__ # post_record.pop('_sa_instance_state') for key in request.files: current_app.logger.debug(item.id) image = request.files[key] current_app.logger.debug(image) response = item_image_bucket.put_object( Body=io.BufferedReader(image).read(), Key=f's3/item-images/' + str(image.filename)) item_image = ItemImage( current_app.config['ITEM_IMAGE_BASE'] + response.key, item.id) item_image.postRecord() return jsonify({ 'state': True, 'entries': { 'item': item_schema.dump(item) } })
def test_item_validate_input__fail(self): data = { 'name': '', # not empty 'price': -2, # more than 0 'availability': -2, # more than 0 'description': '', # not empty 'category': '', # not empty } post_data = format_jsonapi_request('item', data) errors = ItemSchema.validate_input(post_data) expected_result = EXPECTED_ITEMS['item_validate_input__fail'] assert_valid_response(errors, expected_result)
def test_get_items_list__success(self): data = ItemSchema.jsonapi_list([self.item1, self.item2]) expected_result = EXPECTED_ITEMS['get_items_list__success'] assert_valid_response(data, expected_result)
def test_get_item_json__success(self): data, errors = ItemSchema.jsonapi(self.item1) assert errors == {} expected_result = EXPECTED_ITEMS['get_item_json__success'] assert_valid_response(data, expected_result)
def test_item_validate_input__success(self): post_data = format_jsonapi_request('item', self.data) errors = ItemSchema.validate_input(post_data) assert errors == {}
def get_schema(cls): return ItemSchema()