Пример #1
0
    def put(self, eth_name):
        """
        This method replaces the ethnicity information with values from the request body. It returns 204 if the operation is successful.
        415 if the request didn't have JSON as the content type, 404 if the ethnicity doesn't exist, 400 if the JSON wasn't valid against the
        ethnicity schema.
        Parameters:
        - name: String, name of ethnicity
        - description: String, description of ethnicity
        Exception: Raise IntegrityError if ethnicity name is not valid (not unique)
        """
        if (request.json == None):
            return create_error_response(415, "Unsupported media type", "Request content type must be JSON")

        try:
            validate(request.json, FoodpointBuilder.ethnicity_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        target = Ethnicity.query.filter_by(name=eth_name).first()
        if (target):
            target.name = request.json["name"]
            try:
                target.description = request.json["description"]
            except KeyError:
                pass
            try:
                db.session.commit()
                return Response(status=204)
            except IntegrityError:
                db.session.rollback()
                return create_error_response(409, "Already exists", "Ethnicity with name {} already exists.".format(request.json["name"]))
        else:
            return create_error_response(404, "Ethnicity not found")
Пример #2
0
    def post(self):
        """
        Create a new ethnicity returns 201 along with the Location header if successful. If not successful, will return either 415
        if the request didn't have JSON as the content type, 400 if the JSON wasn't valid against the ethnicity schema
        Parameters:
        - name: String, string to identify ethnicity, this must be unique in the system otherwise adding to database will fail.
        - description: String, description of ethnicity.
        Exception: Raise IntegrityError if name is not valid (not unique)
        """
        if (request.json == None):
            return create_error_response(415, "Unsupported media type", "Request content type must be JSON")

        try:
            validate(request.json, FoodpointBuilder.ethnicity_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        name = request.json["name"]
        description = ""
        try:
            description = request.json["description"]
        except KeyError:
            pass
        ethnicity = Ethnicity(name=name, description=description)
        try:
            db.session.add(ethnicity)
            db.session.commit()
            headers = {}
            headers["location"] = api.url_for(EachEthnicity, eth_name=name)
            return Response("Success", 201, headers)
        except IntegrityError:
            db.session.rollback()
            return create_error_response(409, "Already exists", "Ethnicity with name {} already exists.".format(request.json["name"]))
Пример #3
0
    def put(self, user):
        """
        This method replaces the user's information with values from the request body. It returns 204 if the operation is successful.
        415 if the request didn't have JSON as the content type, 404 if the user doesn't exist, 400 if the JSON wasn't valid against the
        user schema.
        Parameters:
        - user: String, name of user
        Exception: Raise IntegrityError if username is not valid (not unique)
        """
        if (request.json == None):
            return create_error_response(415, "Unsupported media type", "Request content type must be JSON")

        try:
            validate(request.json, FoodpointBuilder.user_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        target = User.query.filter_by(userName=user).first()
        if (target):
            target.name = request.json["name"]
            target.userName = request.json["userName"]
            try:
                db.session.commit()
                return Response(status=204)
            except IntegrityError:
                db.session.rollback()
                return create_error_response(409, "Already exists", "User with userName {} already exists.".format(request.json["userName"]))
        else:
            return create_error_response(404, "User not found")
Пример #4
0
    def post(self):
        """
        Create a new user returns 201 along with the Location header if successful. If not successful, will return either 415
        if the request didn't have JSON as the content type, 400 if the JSON wasn't valid against the user schema
        Parameters:
        - name: String, name of user
        - username: String, string to identify user, this must be unique in the system otherwise adding to database will fail.
        Exception: Raise IntegrityError if username is not valid (not unique)
        """
        if (request.json == None):
            return create_error_response(415, "Unsupported media type", "Request content type must be JSON")

        try:
            validate(request.json, FoodpointBuilder.user_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        name = request.json["name"]
        userName = request.json["userName"]
        user = User(name=name, userName=userName)
        try:
            db.session.add(user)
            db.session.commit()
            headers = {}
            headers["location"] = api.url_for(EachUser, user=userName)
            return Response("Success", 201, headers)
        except IntegrityError:
            db.session.rollback()
            return create_error_response(409, "Already exists", "User with userName {} already exists.".format(request.json["userName"]))
Пример #5
0
    def get(self, user):
        """
        Method used to get list of collection by user (returns a Mason document) if found otherwise returns 404
        Parameters:
        - user: String, name of user
        """
        finduser = User.query.filter_by(userName=user).first()
        if finduser is None:
            return create_error_response(404, "User not found")

        userCollection = Collection.query.filter_by(userId=finduser.id)
        user_collection = []
        for collection in userCollection:
            temp = FoodpointBuilder(
                name=collection.name,
                author=user
            )
            temp.add_control("self", api.url_for(EachCollection, user=user, col_name=collection.name))
            temp.add_control("profile", COLLECTION_PROFILE)
            user_collection.append(temp)
        #create the response body, with the previous list as a field called 'items'
        body = FoodpointBuilder(
            items=user_collection
        )
        body.add_namespace("fpoint", LINK_RELATIONS_URL)
        #body.add_namespace("profile", COLLECTION_PROFILE)
        body.add_control("self", api.url_for(CollectionsByUser, user=user))
        body.add_control("author",api.url_for(EachUser, user=user))
        body.add_control_add_collection(user)
        return Response(json.dumps(body), 200, mimetype=MASON)
Пример #6
0
 def delete(self, user, col_name):
     '''
     Method used For deleting collection user, returns 204 if successful, 404 if the the user or collection didn't exist.
     Parameters:
     - user: String, name of user
     - name: String, name of collection
     '''
     finduser = User.query.filter_by(userName=user).first()
     if finduser is None:
         return create_error_response(404, "User not found")
     target = Collection.query.filter_by(userId=finduser.id, name=col_name).first()
     if (target):
         db.session.delete(target)
         db.session.commit()
         return Response(status=204)
     else:
         return create_error_response(404, "Collection not found")
Пример #7
0
 def get(self, user, col_name):
     """
     Method used to get list of all recipes of given collection (returns a Mason document) if found otherwise returns 404
     Parameters:
     - user: String, name of user
     - name: String, name of collection
     """
     finduser = User.query.filter_by(userName=user).first()
     if finduser is None:
         return create_error_response(404, "User not found")
     findCol = Collection.query.filter_by(userId=finduser.id, name=col_name).first()
     if findCol is None:
         return create_error_response(404, "Collection not found")
     #query to be tested- get all recipes with collection name for user
     #col_recipes = Recipe.query.filter(Collection.collection.any(name=col_name)).all()
     col_recipes = findCol.recipes
     recipe_collection = []
     for collection in col_recipes:
         temp = FoodpointBuilder(
             title=collection.title
         )
         temp.add_control("self", api.url_for(EachRecipe, user=user, col_name=col_name, recipe_id=collection.id))
         temp.add_control("profile", RECIPE_PROFILE)
         recipe_collection.append(temp)
     # create the response body, with the previous list as a field called 'items'
     body = FoodpointBuilder(
         name=col_name,
         author=user,
         description=findCol.description,
         items=recipe_collection
     )
     body.add_namespace("fpoint", LINK_RELATIONS_URL)
     body.add_control("profile", COLLECTION_PROFILE)
     body.add_control("self", api.url_for(EachCollection, user=user, col_name=col_name))
     body.add_control("profile", COLLECTION_PROFILE)
     body.add_control_collections_by(user)
     body.add_control_add_recipe(user, col_name)
     body.add_control_edit_collection(user, col_name)
     body.add_control_delete_collection(user, col_name)
     return Response(json.dumps(body), 200, mimetype=MASON)
Пример #8
0
 def delete(self, user):
     '''
     Method used For deleting a user, returns 204 if successful, 404 if the the user didn't exist.
     Parameters:
     - user: String, name of user
     '''
     target = User.query.filter_by(userName=user).first()
     if (target):
         db.session.delete(target)
         db.session.commit()
         return Response(status=204)
     else:
         return create_error_response(404, "User not found")
Пример #9
0
    def delete(self, user, col_name, recipe_id):
        '''
        Method used For deleting recipe of collection of user, returns 204 if successful, 404 if the the user or collection or recipe didn't exist.
        Parameters:
        - user: String, name of user
        - name: String, name of collection
        - recipe+id: Integer, id of recipe
        '''
        finduser = User.query.filter_by(userName=user).first()
        if finduser is None:
            return create_error_response(404, "User not found")
        findCol = Collection.query.filter_by(userId=finduser.id, name=col_name).first()
        if findCol is None:
            return create_error_response(404, "Collection not found")
        target = Recipe.query.filter_by(id=recipe_id).first()

        if (target in findCol.recipes):
            db.session.delete(target)
            db.session.commit()
            return Response(status=204)
        else:
            return create_error_response(404, "Recipe not found")
Пример #10
0
    def get(self, user, col_name, recipe_id):
        """
        Return all information of recipe (returns a Mason document) if found otherwise returns 404
        Parameters:
        - user: String, name of user
        - namae: String, name of collection
        - recipe_id: Integer, id of recipe
        """
        finduser = User.query.filter_by(userName=user).first()
        if finduser is None:
            return create_error_response(404, "User not found")
        findCol = Collection.query.filter_by(userId=finduser.id, name=col_name).first()
        if findCol is None:
            return create_error_response(404, "Collection not found")
        target = Recipe.query.filter_by(id=recipe_id).first()

        if target in findCol.recipes:
            findEthnicity = Ethnicity.query.filter_by(id=target.ethnicityId).first()
            findCategory = Category.query.filter_by(id=target.categoryId).first()
            body = FoodpointBuilder(
                title=target.title,
                description=target.description,
                ingredients=target.ingredients,
                rating=target.rating,
                ethnicity=findEthnicity.name,
                category=findCategory.name
            )
            body.add_namespace("fpoint", LINK_RELATIONS_URL)
            body.add_control("self", api.url_for(EachRecipe, user=user, col_name=col_name, recipe_id=recipe_id))
            body.add_control("profile", RECIPE_PROFILE)
            body.add_control("collection", api.url_for(EachCollection, user=user,col_name=col_name))
            body.add_control_ethnicity(target.ethnicity.name)
            body.add_control_category(target.category.name)
            body.add_control_edit_recipe(user, col_name, recipe_id)
            body.add_control_delete_recipe(user, col_name, recipe_id)
            return Response(json.dumps(body), 200, mimetype=MASON)
        else :
            return create_error_response(404, "Recipe not found")
Пример #11
0
    def post(self, user):
        """
        Create a new collection for user returns 201 along with the Location header if successful. If not successful, will return either 415
        if the request didn't have JSON as the content type, 400 if the JSON wasn't valid against the collection schema
        Parameters:
        - user: String, name of user
        - name: String, string to identify collection, this must be unique in the system otherwise adding to database will fail.
        - description: String, description of collection
        Exception: Raise IntegrityError if collection name is not valid (not unique)
        """
        finduser = User.query.filter_by(userName=user).first()
        if finduser is None:
            return create_error_response(404, "User not found")
        if (request.json == None):
            return create_error_response(415, "Unsupported media type", "Request content type must be JSON")

        try:
            validate(request.json, FoodpointBuilder.collection_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        name = request.json["name"]
        description = ""
        try:
            description = request.json["description"]
        except KeyError:
            pass
        collection = Collection(name=name, description=description, user=finduser)
        headers = {}
        headers["location"] = api.url_for(EachCollection, user=user, col_name=request.json["name"])
        try:
            db.session.add(collection)
            db.session.commit()
            return Response("Success", 201, headers)
        except IntegrityError:
            db.session.rollback()
            return create_error_response(409, "Already exists", "Collection against user {} already exists.".format(user))
Пример #12
0
    def put(self, user, col_name):
        """
        Mehtod used for editing collection of user returns 204  if successful. If not successful, will return
        either 415 if the request didn't have JSON as the content type, 400 if the JSON wasn't valid against the collection schema and
        404 if user or collection not found
        Parameters:
        - user: String, name of user
        - name: String, string to identify collection, this must be unique in the system otherwise adding to database will fail.
        - description: String, description of collection
        Exception: Raise IntegrityError if collection name is not valid (not unique)
        """
        if (request.json == None):
            return create_error_response(415, "Unsupported media type", "Request content type must be JSON")
        try:
            validate(request.json, FoodpointBuilder.collection_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))
        finduser = User.query.filter_by(userName=user).first()
        if finduser is None:
            return create_error_response(404, "User not found")
        findCol = Collection.query.filter_by(userId=finduser.id, name=col_name).first()

        if (findCol):
            findCol.name = request.json["name"]
            try:
                findCol.description = request.json["description"]
            except KeyError:
                pass
            try:
                db.session.commit()
                return Response(status=204)
            except IntegrityError:
                db.session.rollback()
                return create_error_response(409, "Already exists", "Collection with name {} already exists for this user.".format(request.json["name"]))
        else:
            return create_error_response(404, "Collection not found")
Пример #13
0
    def put(self, user, col_name, recipe_id):
        """
        Method used for editing recipe of collection of user returns 204  if successful. If not successful, will return
        either 415 if the request didn't have JSON as the content type, 400 if the JSON wasn't valid against the recipe schema,
        404 if user or collection not found and 409 if category or ethincity not found
        Parameters:
        - user: String, name of user
        - name: String, string to identify collection, this must be unique in the system otherwise adding to database will fail.
        - description: String, description of collection
        - ethincity: String, ethincity of collection
        - category: String, category of collection
        - ingredients: String, ingredients of collection
        Exception: Raise KeyError if rating is not valid (not float)
        """
        if (request.json == None):
            return create_error_response(415, "Unsupported media type", "Request content type must be JSON")
        try:
            validate(request.json, FoodpointBuilder.recipe_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))
        finduser = User.query.filter_by(userName=user).first()
        if finduser is None:
            return create_error_response(404, "User not found")
        findCol = Collection.query.filter_by(userId=finduser.id, name=col_name).first()
        if findCol is None:
            return create_error_response(404, "Collection not found")

        findcategory = Category.query.filter_by(name=request.json["category"]).first()
        findethnicity = Ethnicity.query.filter_by(name=request.json["ethnicity"]).first()
        if findcategory is None:
            return create_error_response(409, "Category does not exist", "Category {} does not exist.".format(request.json["category"]))
        findethnicity = Ethnicity.query.filter_by(name=request.json["ethnicity"]).first()
        if findethnicity is None:
            return create_error_response(409, "Ethnicity does not exist", "Ethnicity {} does not exist.".format(request.json["ethnicity"]))

        target = Recipe.query.filter_by(id=recipe_id).first()
        if (target in findCol.recipes):
            try:
                target.rating = request.json["rating"]
            except KeyError:
                pass
            target.title = request.json["title"]
            target.description = request.json["description"]
            target.ingredients = request.json["ingredients"]
            target.category =findcategory
            target.ethnicity =findethnicity
            db.session.commit()
            return Response(status=204)
        else:
            return create_error_response(404, "Recipe not found")
Пример #14
0
 def get(self, eth_name):
     """
     Return all information of ethnicity (returns a Mason document) if found otherwise returns 404
     Parameters:
     - name: String, name of ethnicity
     """
     target = Ethnicity.query.filter_by(name=eth_name).first()
     if (target):
         body = FoodpointBuilder(
             name=target.name,
             description=target.description
         )
         body.add_namespace("fpoint", LINK_RELATIONS_URL)
         body.add_control("self", api.url_for(EachEthnicity, eth_name=eth_name))
         body.add_control("profile", ETHNICITY_PROFILE)
         body.add_control_all_ethnicities()
         body.add_control_edit_ethnicity(eth_name)
         return Response(json.dumps(body), 200, mimetype=MASON)
     else:
         return create_error_response(404, "Ethnicity not found")
Пример #15
0
    def post(self, user, col_name):
        """
        Create a new recipe for collection of user returns 201 along with the Location header if successful. If not successful, will return
        either 415 if the request didn't have JSON as the content type, 400 if the JSON wasn't valid against the recipe schema,
        404 if user or collection not found and 409 if category or ethincity not found
        Parameters:
        - user: String, name of user
        - name: String, string to identify collection, this must be unique in the system otherwise adding to database will fail.
        - description: String, description of collection
        - ethincity: String, ethincity of collection
        - category: String, category of collection
        - ingredients: String, ingredients of collection
        Exception: Raise KeyError if rating is not valid (not float)
        """
        finduser = User.query.filter_by(userName=user).first()
        if finduser is None:
            return create_error_response(404, "User not found")
        findCol = Collection.query.filter_by(userId=finduser.id, name=col_name).first()
        if findCol is None:
            return create_error_response(404, "Collection not found")
        if (request.json == None):
            return create_error_response(415, "Unsupported media type", "Request content type must be JSON")
        try:
            validate(request.json, FoodpointBuilder.recipe_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))
        findcategory = Category.query.filter_by(name=request.json["category"]).first()
        findethnicity = Ethnicity.query.filter_by(name=request.json["ethnicity"]).first()
        if findcategory is None:
            return create_error_response(409, "Category does not exist", "Category {} does not exist.".format(request.json["category"]))
        if findethnicity is None:
            return create_error_response(409, "Ethnicity does not exist", "Ethnicity {} does not exist.".format(request.json["ethnicity"]))

        title = request.json["title"]
        description = request.json["description"]
        ingredients = request.json["ingredients"]
        rating = 0.0
        try:
            rating = request.json["rating"]
        except KeyError:
            pass
        recipe = Recipe(title=title, description=description,ingredients=ingredients,rating=rating,category=findcategory,ethnicity=findethnicity)
        findCol.recipes.append(recipe)
        headers = {}

        db.session.commit()
        headers["location"] = api.url_for(EachRecipe, user=user, col_name=col_name, recipe_id=recipe.id)
        return Response("Success", 201, headers)
Пример #16
0
 def get(self, user):
     """
     Return all information of user (returns a Mason document) if found otherwise returns 404
     Parameters:
     - user: String, name of user
     """
     target = User.query.filter_by(userName=user).first()
     if (target):
         body = FoodpointBuilder(
             name = target.name,
             userName = target.userName
         )
         body.add_namespace("fpoint", LINK_RELATIONS_URL)
         body.add_control("self", api.url_for(EachUser, user=target.userName))
         body.add_control("profile", USER_PROFILE)
         body.add_control_all_users()
         body.add_control_collections_by(target.userName)
         body.add_control_edit_user(target.userName)
         body.add_control_delete_user(target.userName)
         return Response(json.dumps(body), 200, mimetype=MASON)
     else:
         return create_error_response(404, "User not found")