Example #1
0
 def seed(self):
     print("Categories seed ..")
     with open(self.seed_file) as json_file:
         data = json.load(json_file)
         for index in range(len(data)):
             category = CategoriesModel().get({'name': data[index]['name']})
             category = [doc for doc in category]
             if (0 == len(category)):
                 CategoriesModel().insert(data[index])
    def post(cls):
        data = cls.parser.parse_args()

        category = CategoriesModel(
            data['id'],
            data['name'],
            data['description']
        )
        category.save_to_db()

        return {"message": "Category created successfully."}, 201
    def post(cls):
        data = cls.parser.parse_args()
        recipe = RecipeModel(data['user_id'], data['cuisine_id'], data['name'],
                             data['description'], data['image_path'],
                             data['total_time'], data['prep_time'],
                             data['cook_time'], data['level'], data['source'])
        categories = data['category']
        if categories is not None:
            for cat in categories:
                category_json = ast.literal_eval(cat)
                category = CategoriesModel(category_json['id'],
                                           category_json['name'],
                                           category_json['description'])
                recipe.save_recipe_to_db(category)

        allergens = data['allergens']
        if allergens is not None:
            for aller in allergens:
                allergens_json = ast.literal_eval(aller)
                allergen = AllergensModel(
                    allergens_json['id'],
                    allergens_json['name'],
                )
                recipe.save_allergen_to_db(allergen)

        courses = data['courses']
        if courses is not None:
            for cour in courses:
                courses_json = ast.literal_eval(cour)
                course = AllergensModel(
                    courses_json['id'],
                    courses_json['name'],
                )
                recipe.save_course_to_db(course)

        steps = data['steps']
        if steps is not None:
            for step in steps:
                steps_json = ast.literal_eval(step)
                step = StepsModel(
                    steps_json['id'],
                    steps_json['step_number'],
                    steps_json['instructions'],
                )
                recipe.save_step_to_db(step)

        ingredients = data['ingredients']
        if ingredients is not None:
            for ingredient in ingredients:
                ingredients_json = ast.literal_eval(ingredient)
                ingredient = IngredientsModel(ingredients_json['id'],
                                              ingredients_json['amount'],
                                              ingredients_json['ingredient'],
                                              ingredients_json['measurement'])
                recipe.save_ingredient_to_db(ingredient)

        return {"message": "Recipe created successfully."}, 201
    def put(cls, id):
        data = cls.parser.parse_args()
        category = CategoriesModel.find_by_id(id)

        if category:
            category.name = data['name']
            category.description = data['description']
        else:
            category = CategoriesModel(**data)

        category.save_to_db()

        return category.json()
 def get(cls, id: int):
     category = CategoriesModel.find_by_id(id)
     if not category:
         return {'message': 'Category Not Found'}, 404
     return category.json(), 200
 def delete(self, objectId):
     CategoriesModel().delete(objectId)
     return http_response(204, {"status": "category record deleted"})
 def put(self, objectId):
     category = request.get_json()
     CategoriesModel().update(objectId, category)
     return http_response(202, {"status": "category record updated"})
 def get(self, objectId):
     category = CategoriesModel().getById(objectId)
     return http_response(200, category)
 def get(self):
     categories = CategoriesModel().get({})
     categories = [doc for doc in categories]
     return http_response(200, categories)
 def test_Categories_Delete(self):
     inserted = CategoriesModel().insert(TEST_OBJECT)
     inserted_id = inserted.inserted_id
     inserted = CategoriesModel().delete(inserted_id)
     deleted = CategoriesModel().getById(inserted_id)
     self.assertIsNone(deleted)
 def test_Categories_Post(self):
     inserted = CategoriesModel().insert(TEST_OBJECT)
     fetch_inserted = CategoriesModel().getById(inserted.inserted_id)
     self.assertEqual(TEST_OBJECT['name'], fetch_inserted['name'])
 def test_Categories_Get_ById(self):
     m = CategoriesModel().getById(SAMPLE_OBJECT_CAT)
     self.assertEqual(m['name'], MOVIE)
    def test_Categories_Get(self):

        m = CategoriesModel().get({})
        self.assertGreaterEqual(m.count(), MOVIE_LENGTH)
 def delete(cls, id: int):
     category = CategoriesModel.find_by_id(id)
     if not category:
         return {'message': 'Category Not Found'}, 404
     category.delete_from_db()
     return {'message': 'Category deleted.'}, 200
 def post(self):
     category = request.get_json()
     CategoriesModel().insert(category)
     return http_response(201, {"status": "category record inserted"})
 def get(cls):
     return {'categories': [category.json() for category in CategoriesModel.find_all()]}
    def put(cls, id):
        data = cls.parser.parse_args()
        recipe = RecipeModel.find_by_id(id)

        if recipe:
            recipe.user_id = data['user_id']
            recipe.cuisine_id = data['cuisine_id']
            recipe.name = data['name']
            recipe.description = data['description']
            recipe.image_path = data['image_path']
            recipe.total_time = data['total_time']
            recipe.prep_time = data['prep_time']
            recipe.cook_time = data['cook_time']
            recipe.level = data['level']
            recipe.source = data['source']
        else:
            recipe = RecipeModel(**data)

        recipe_categories = data['category']
        for cat in recipe_categories:
            category = RecipeCategory.find_by_id(id)
            if category:
                recipe.delete_category_from_db(category)

        for cat in recipe_categories:
            category_json = ast.literal_eval(cat)
            category = CategoriesModel(category_json['id'],
                                       category_json['name'],
                                       category_json['description'])
            recipe.save_recipe_to_db(category)

        allergens = data['allergens']
        if allergens:
            for aller in allergens:
                allergen = RecipeAllergens.find_by_id(id)
                if allergen:
                    recipe.delete_allergen_from_db(allergen)
        else:
            allergen = RecipeAllergens.find_by_id(id)
            if allergen:
                recipe.delete_allergen_from_db(allergen)

        if allergens:
            for aller in allergens:
                allergens_json = ast.literal_eval(aller)
                allergen = AllergensModel(
                    allergens_json['id'],
                    allergens_json['name'],
                )
                recipe.save_allergen_to_db(allergen)

        courses = data['courses']
        for cour in courses:
            course = RecipeCourses.find_by_id(id)
            if course:
                recipe.delete_course_from_db(course)
        for cour in courses:
            courses_json = ast.literal_eval(cour)
            course = CoursesModel(
                courses_json['id'],
                courses_json['name'],
            )
            recipe.save_course_to_db(course)

        steps = data['steps']
        for step in steps:
            steps_json = ast.literal_eval(step)
            step = StepsModel.find_by_id(steps_json['id'])
            if step:
                step.step_number = steps_json['step_number']
                step.instructions = steps_json['instructions']
            else:
                step = StepsModel(
                    steps_json['id'],
                    steps_json['step_number'],
                    steps_json['instructions'],
                )
            recipe.save_step_to_db(step)

        ingredients = data['ingredients']
        for ingredient in ingredients:
            ingredients_json = ast.literal_eval(ingredient)
            ingredient = IngredientsModel.find_by_id(ingredients_json['id'])
            if ingredient:
                ingredient.amount = ingredients_json['amount']
                ingredient.ingredient = ingredients_json['ingredient']
                ingredient.measurement = ingredients_json['measurement']
            else:
                ingredient = IngredientsModel(ingredients_json['id'],
                                              ingredients_json['amount'],
                                              ingredients_json['ingredient'],
                                              ingredients_json['measurement'])
            recipe.save_ingredient_to_db(ingredient)

        return recipe.json()