def test_get_questions_by_category_valid(self): """ Test that a GET request to the /categories/<int:category_id>/questions endpoint returns the correct response when there are questions in the database for the chosen category. """ with self.client as c: category_id = self.c_1.format()["id"] result = json.loads( c.get( f"/categories/{category_id}/questions", headers={ "Content-Type": "application/json" }, ).data) self.assertTrue(result["success"]) self.assertEqual( result["current_category"], Category.get_by_id(category_id).type, ) self.assertEqual( result["questions"], [ question.format() for question in Question.get_by_category(category_id)[0:10] ], ) self.assertEqual( result["total_questions"], len(Question.get_by_category(category_id)), )
def post(self): categoryKeyString = self.request.get('deleteCategoryKey') #generate message category = Category.get_by_id(int(categoryKeyString)) category.remove_from_search_index() message = "Successfully deleted category: " + category.categoryName #delete category categoryKey = category.key categoryKey.delete() self.response.write(message)
def questions_by_category(category_id): questions = Question.get_by_category_by_page(category_id, request, QUESTIONS_PER_PAGE) if len(questions) == 0: abort(404) return jsonify({ "questions": questions, "total_questions": len(Question.get_by_category(category_id)), "current_category": Category.get_by_id(category_id).type, "success": True, })
def post(self): categoryKeyString = self.request.get('editCategoryKey') categoryID = int(categoryKeyString) categoryName = self.request.get('editCategoryName') photoName = self.request.get('editCategoryPhotoName') #get the photo specified by the user photo = File.query(File.file_name == photoName.upper()).get() #get the category based on the key and update all fields category = Category.get_by_id(categoryID) category.categoryName = categoryName category.picture = photo.key category.uploaded_by = users.get_current_user() category.put() category.add_to_search_index() message = "Successfully updated category record: " + category.categoryName self.response.write(message)
def post(self): categoryKeyString = self.request.get('editCategoryKey') categoryID = int(categoryKeyString) categoryName = self.request.get('editCategoryName') photoName = self.request.get('editCategoryPhotoName') #get the photo specified by the user photo = File.query(File.file_name==photoName.upper()).get() #get the category based on the key and update all fields category = Category.get_by_id(categoryID) category.categoryName=categoryName category.picture=photo.key category.uploaded_by=users.get_current_user() category.put() category.add_to_search_index() message = "Successfully updated category record: " + category.categoryName self.response.write(message)
def category(id): category = Category.get_by_id(id) return jsonify({"id": category.id, "name": category.name})
def index(category_id): category = Category.get_by_id(category_id) sub_categories = SubCategory.select().where( SubCategory.category == category)
def index(category_id): category = Category.get_by_id(category_id) products = ProductCategory.select().where(ProductCategory.category == category) return jsonify ([{"id" : p.product_id, "product_name" : p.product.name, "product_chinese_name" : p.product.chinese_name, "category" : p.product.category_id, "price" : p.product.price, "url" : p.product.image_path} for p in products])
def test_get_by_id(self): """Test the get_by_id method for the Category model.""" with self.app_context: category = Category.get_by_id(self.c_1.id) self.assertIsNotNone(category) self.assertEqual(category.type, self.c_1.type)