コード例 #1
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)
コード例 #2
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"]))
コード例 #3
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"]))
コード例 #4
0
 def add_control_category(self,cat_name):
     '''
     Leads to a resource that represent and contain information of a category of recipe.
     '''
     self.add_control(
         "fpoint:category",
         href=api.url_for(EachCategory, cat_name=cat_name),
         title="Category of this recipe"
     )
コード例 #5
0
 def add_control_ethnicity(self,eth_name):
     '''
     Leads to a resource that represent and contain information of an ethnicity of recipe.
     '''
     self.add_control(
         "fpoint:ethnicity",
         href=api.url_for(EachEthnicity, eth_name=eth_name),
         title="Ethnicity of this recipe"
     )
コード例 #6
0
 def add_control_all_ethnicities(self):
     '''
     Leads to a resource that has a list of all ethnicities of recipe known to the API.
     '''
     self.add_control(
         "fpoint:all-ethnicities",
         href=api.url_for(AllEthnicities),
         title="All Ethnicities"
     )
コード例 #7
0
 def add_control_all_users(self):
     '''
     Leads to a resource that has a list of all users known to the API.
     '''
     self.add_control(
         "fpoint:all-users",
         href=api.url_for(AllUsers),
         title="All users"
     )
コード例 #8
0
 def add_control_collections_by(self, user):
     '''
     Leads to a resource that has a list of all collections owned by a user.
     Parameters:
      - user: String, string to identify user
     '''
     self.add_control(
         "fpoint:collections-by",
         href=api.url_for(CollectionsByUser, user=user),
         title="Collections by this user"
     )
コード例 #9
0
 def add_control_add_category(self):
     '''
     Control For adding category
     '''
     self.add_control(
         "fpoint:add-category",
         href=api.url_for(AllCategories),
         title="Add a new Category",
         method="POST",
         encoding="json",
         schema=self.category_schema()
     )
コード例 #10
0
 def add_control_add_ethnicity(self):
     '''
     Control For adding ethnicity
     '''
     self.add_control(
         "fpoint:add-ethnicity",
         href=api.url_for(AllEthnicities),
         title="Add a new Ethnicity",
         method="POST",
         encoding="json",
         schema=self.ethnicity_schema()
     )
コード例 #11
0
 def add_control_delete_user(self, user):
     '''
     For deleting a user
     Parameters:
      - user: String, string to identify user
     '''
     self.add_control(
         "fpoint:delete",
         href=api.url_for(EachUser, user=user),
         title="Delete this user",
         method="DELETE"
     )
コード例 #12
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)
コード例 #13
0
 def add_control_add_user(self):
     '''
     To add a user to the AllUsers resource.
     Accessed with POST and includes JSON schema
     '''
     self.add_control(
         "fpoint:add-user",
         href=api.url_for(AllUsers),
         title="Add a new user",
         method="POST",
         encoding="json",
         schema=self.user_schema()
     )
コード例 #14
0
 def add_control_delete_collection(self, user, col_name):
     '''
     Control for deleting collection for user
     Parameters:
      - user: String, string to identify user
      - col_name: String, string to identify collection
     '''
     self.add_control(
         "fpoint:delete",
         href=api.url_for(EachCollection, user=user, col_name=col_name),
         title="Delete this collection",
         method="DELETE"
     )
コード例 #15
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")
コード例 #16
0
 def get(self):
     """
     Method used to get list of all ethnicities (returns a Mason document)
     """
     ethnicities = Ethnicity.query.all()
     all_ethnicities = []
     for ethnicity in ethnicities:
         temp = FoodpointBuilder(
             name=ethnicity.name,
             description=ethnicity.description
         )
         temp.add_control("self", api.url_for(EachEthnicity, eth_name=ethnicity.name))
         temp.add_control("profile", ETHNICITY_PROFILE)
         all_ethnicities.append(temp)
     # create the response body, with the previous list as a field called 'items'
     body = FoodpointBuilder(
         items=all_ethnicities
     )
     body.add_namespace("fpoint", LINK_RELATIONS_URL)
     body.add_control("self", api.url_for(AllEthnicities))
     body.add_control_all_users()
     body.add_control_add_ethnicity()
     return Response(json.dumps(body), 200, mimetype=MASON)
コード例 #17
0
 def add_control_edit_ethnicity(self, eth_name):
     '''
     Control of editing ethincity.
     Parameters:
     - eth_name: String, string to identify ethincity
     '''
     self.add_control(
         "edit",
         href=api.url_for(EachEthnicity, eth_name=eth_name),
         title="Edit this ethnicity's information",
         method="PUT",
         encoding="json",
         schema=self.ethnicity_schema()
     )
コード例 #18
0
 def add_control_edit_user(self, user):
     '''
     For editing this user information
     Parameters:
      - user: String, string to identify user
     '''
     self.add_control(
         "edit",
         href=api.url_for(EachUser, user=user),
         title="Edit this user's information",
         method="PUT",
         encoding="json",
         schema=self.user_schema()
     )
コード例 #19
0
 def add_control_edit_category(self, cat_name):
     '''
     Control of editing category.
     Parameters:
     - cat_name: String, string to identify category
     '''
     self.add_control(
         "edit",
         href=api.url_for(EachCategory, cat_name=cat_name),
         title="Edit this category's information",
         method="PUT",
         encoding="json",
         schema=self.category_schema()
     )
コード例 #20
0
 def add_control_delete_recipe(self, user, col_name, recipe_id):
     '''
     Control for deleting given recipe of collection for user
     Parameters:
      - user: String, string to identify user
      - col_name: String, string to identify collection
      - recipe_id: Integer, integer to identify recipe
     '''
     self.add_control(
         "fpoint:delete",
         href=api.url_for(EachRecipe, user=user, col_name=col_name, recipe_id=recipe_id),
         title="Delete this recipe",
         method="DELETE"
     )
コード例 #21
0
 def add_control_add_collection(self, user):
     '''
     To add a collection to the CollectionsByUser resource.
     Accessed with POST and control includes a JSON schema
     Parameters:
      - user: String, string to identify user
     '''
     self.add_control(
         "fpoint:add-collection",
         href=api.url_for(CollectionsByUser, user=user),
         title="Add new collection for user",
         method="POST",
         encoding="json",
         schema=self.collection_schema()
     )
コード例 #22
0
 def add_control_edit_collection(self, user, col_name):
     '''
     Control for editing given collection of user
     Parameters:
      - user: String, string to identify user
      - col_name: String, string to identify collection
     '''
     self.add_control(
         "edit",
         href=api.url_for(EachCollection, user=user, col_name=col_name),
         title="Edit this collection information",
         method="PUT",
         encoding="json",
         schema=self.collection_schema()
     )
コード例 #23
0
 def get(self):
     """
     Method used to get list of all users (returns a Mason document)
     """
     users = User.query.all()
     all_users = []
     for user in users:
         temp = FoodpointBuilder(
             name=user.name,
             userName=user.userName
         )
         temp.add_control("self", api.url_for(EachUser, user=user.userName))
         temp.add_control("profile", USER_PROFILE)
         all_users.append(temp)
     #create the response body, with the previous list as a field called 'items'
     body = FoodpointBuilder(
         items = all_users
     )
     body.add_namespace("fpoint", LINK_RELATIONS_URL)
     body.add_control("self", api.url_for(AllUsers))
     body.add_control_add_user()
     body.add_control_all_categories()
     body.add_control_all_ethnicities()
     return Response(json.dumps(body), 200, mimetype=MASON)
コード例 #24
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)
コード例 #25
0
 def add_control_edit_recipe(self, user, col_name, recipe_id):
     '''
     Control for editing given recipe of collection for user
     Parameters:
      - user: String, string to identify user
      - col_name: String, string to identify collection
      - recipe_id: Integer, integer to identify recipe
     '''
     self.add_control(
         "edit",
         href=api.url_for(EachRecipe, user=user, col_name=col_name, recipe_id=recipe_id),
         title="Edit this recipe information",
         method="PUT",
         encoding="json",
         schema=self.recipe_schema()
     )
コード例 #26
0
 def add_control_add_recipe(self, user, col_name):
     '''
     To add a recipe to the collection resource.
     Accessed with POST and includes JSON schema
     Parameters:
      - user: String, string to identify user
      - col_name: String, string to identify collection
     '''
     self.add_control(
         "fpoint:add-recipe",
         href=api.url_for(EachCollection, user=user, col_name=col_name),
         title="Add new recipe to collection of user",
         method="POST",
         encoding="json",
         schema=self.recipe_schema()
     )
コード例 #27
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")
コード例 #28
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")
コード例 #29
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))