Beispiel #1
0
 def get_step_list(self):
     step_list = [
         Step(
             name="Bước 1: Bước ngẫu nhiên từ mỗi nút trong mạng",
             inputs={
                 "CoNet1": "CoNet1",
                 "walk_length": self.setting["walk_length"],
                 "num_walks": self.setting["num_walks"],
                 "p": self.setting["p"],
                 "q": self.setting["q"],
             },
             outputs={"Walks": "Tập bước ngẫu nhiên từ mọi nút trong mạng"},
             output_file=""),
         Step(name="Bước 2: Ánh xạ nút vào không gian vectơ",
              inputs={
                  "Walks": "Walks",
                  "dimensions": self.setting["dimensions"],
                  "window_size": self.setting["window_size"],
              },
              outputs={"Vectơ đặc trưng": "Vectơ đặc trưng của từng nút"},
              output_file=""),
         Step(name="Bước 3: Khuyến nghị theo vectơ đặc trưng",
              inputs={
                  "Vectơ đặc trưng": "Vectơ đặc trưng",
                  "topK": 10,
              },
              outputs={
                  "Recommendations":
                  "Danh sách khuyến nghị cho từng nghiên cứu viên"
              },
              output_file="")
     ]
     return step_list
Beispiel #2
0
def show_single_recipe(id):
    recipe = Recipe.get_by_id(id)
    step = Step.select().where(Step.recipe_id == id)
    recipe_ingredient = RecipeIngredient.select().where(
        RecipeIngredient.recipe_id == id)
    if recipe:

        step_data = []
        for s in step:
            data = {"number": s.number, "description": s.description}
            step_data.append(data)

        ingredient_data = []
        for i in recipe_ingredient:
            data = {"name": i.name}
            ingredient_data.append(data)

        results = {
            "id": recipe.id,
            "name": recipe.name,
            "image": recipe.image,
            "step": step_data,
            "ingredient": ingredient_data
        }
        return jsonify({"data": results})
Beispiel #3
0
def show_recipe_step(cuisine_name, recipe_name, step_number):
    cuisine = Cuisine.get_or_none(name=cuisine_name)

    if cuisine:
        recipe = Recipe.get_or_none(name=recipe_name)
        if recipe:
            if recipe.cuisine_id == cuisine.id:
                step = Step.select().where(Step.recipe_id == recipe.id,
                                           Step.number == step_number)
                for s in step:
                    result = {
                        "step_number": s.number,
                        "description": s.description
                    }
                return jsonify({"data": result})
            else:
                return jsonify({
                    "errors":
                    recipe_name + " is not found in " + cuisine_name +
                    " cuisine."
                })
        else:
            return jsonify({"errors": "Recipe not exists"})
    else:
        return jsonify({"errors": cuisine_name + " cusine is not exist"})
Beispiel #4
0
 def _add_repeat_step(self, list_iter, parent):
     for i in range(parent.get_repeat_number()):
         # Get the next step in the list
         ps = Step.create_step(next(list_iter))
         # We can also check if we have another repeat here
         # Add the step to the parent
         parent.add_repeat_step(ps)
Beispiel #5
0
    def _next_step(self):
        list_iter = iter(self._read_step())
        for i in list_iter:
            s = Step.create_step(i)
            # Handle repeats
            if s.is_repeat():
                self._add_repeat_step(list_iter, s)

            yield s
Beispiel #6
0
def new_recipe_step(cuisine_name, recipe_name):
    user = User.get_by_id(get_jwt_identity())
    cuisine = Cuisine.get_or_none(name=cuisine_name)
    if user.is_admin:
        if cuisine:
            recipe = Recipe.get_or_none(name=recipe_name)
            if recipe:
                step_number = request.form.get("step_number")
                step_description = request.form.get("step_description")
                new_step = Step(number=step_number,
                                description=step_description,
                                recipe_id=recipe.id)
                new_step.save()
                return jsonify({"message": "Step successfully created!"})
            else:
                return jsonify({"errors": "Recipe not exists"})
        else:
            return jsonify({"errors": cuisine_name + " cusine is not exist"})
    else:
        return 0
Beispiel #7
0
 def get_step_list(self):
     step_list = [
         Step(
             name="Bước 1: Tính Common Neighbor từ mỗi cặp nút trong mạng",
             inputs={
                 "CoNet1": "CoNet1",
             },
             outputs={
                 "Tập Common Neighbor": "Tập cặp nút trong mạng kèm Common Neighbor"
             },
             output_file=""),
         Step(
             name="Bước 2: Khuyến nghị theo số Common Neighbor",
             inputs={
                 "Tập Common Neighbor": "Tập Common Neighbor",
                 "topK": 10,
             },
             outputs={
                 "Recommendations": "Danh sách khuyến nghị cho từng nghiên cứu viên"
             },
             output_file=""),
     ]
     return step_list
 def get_step_list(self):
     step_list = [
         Step(name=
              "Bước 1: Tính Jaccard Coefficient từ mỗi cặp nút trong mạng",
              inputs={
                  "CoNet1": "CoNet1",
              },
              outputs={
                  "Tập Jaccard Coefficient":
                  "Tập cặp nút trong mạng kèm Jaccard Coefficient"
              },
              output_file=""),
         Step(name="Bước 2: Khuyến nghị theo số Jaccard Coefficient",
              inputs={
                  "Tập Jaccard Coefficient": "Tập Jaccard Coefficient",
                  "topK": 10,
              },
              outputs={
                  "Recommendations":
                  "Danh sách khuyến nghị cho từng nghiên cứu viên"
              },
              output_file=""),
     ]
     return step_list
Beispiel #9
0
def delete_recipe_step(cuisine_name, recipe_name):
    user = User.get_by_id(get_jwt_identity())
    cuisine = Cuisine.get_or_none(name=cuisine_name)
    if user.is_admin:
        if cuisine:
            recipe = Recipe.get_or_none(name=recipe_name)
            if recipe:
                step_number = request.form.get("step_number")
                delete = Step.delete().where(Step.number == step_number,
                                             Step.recipe_id == recipe.id)
                delete.execute()
                return jsonify(
                    {"message": "Step has been successfully deleted."})
            else:
                return jsonify({"errors": "Recipe not exists"})
        else:
            return jsonify({"errors": cuisine_name + " cusine is not exist"})
    else:
        return 0
Beispiel #10
0
def show_recipe_all_step(cuisine_name, recipe_name):
    cuisine = Cuisine.get_or_none(name=cuisine_name)
    if cuisine:
        recipe = Recipe.get_or_none(name=recipe_name)
        if recipe:
            step_all = Step.select().where(Step.recipe_id == recipe.id)
            results = []
            for step in step_all:
                step_data = {
                    "step_number": step.number,
                    "step_description": step.description
                }
                results.append(step_data)
            return jsonify({"data": results})
        else:
            return jsonify({
                "errors":
                recipe_name + " is not found in " + cuisine_name + " cuisine."
            })
    else:
        return jsonify({"errors": cuisine_name + " cusine is not exist"})
Beispiel #11
0
 def get_steps_by_status(self, status):
     return Step.objects(owner=self.owner, status=status)
Beispiel #12
0
def create_recipe():
    categories = [str(c.name) for c in Category.load_unique_categories()]
    ingredients = [str(i.name.encode('utf-8')) for i in Ingredient.load_unique_ingredients()]

    if request.method == 'GET':
        recipe = Recipe()
        
        # TESTING FORM REFILL
        # recipe.name = 'name'
        # recipe.servings = 'servings'
        # recipe.preparation_time = 'prep_time'
        # recipe.nutritional_info = 'nut_info'
        # recipe.categories_string = 'cat1, cat2'
        # recipe.ingredients = []
        # ingredient = Ingredient()
        # ingredient.name = 'ing1-name'
        # ingredient.quantity = 'ing1-quantity'
        # ingredient.unit = 'ing1-unit'
        # ingredient.comment = 'ing1-comment'
        # recipe.ingredients.append(ingredient)
        # ingredient = Ingredient()
        # ingredient.name = 'ing2-name'
        # ingredient.quantity = 'ing2-quantity'
        # ingredient.unit = 'ing2-unit'
        # ingredient.comment = 'ing2-comment'
        # recipe.ingredients.append(ingredient)
        # recipe.steps = []
        # step = Step()
        # step.number = 1
        # step.instructions = 'inst1'
        # recipe.steps.append(step)
        # step = Step()
        # step.number = 2
        # step.instructions = 'inst2'
        # recipe.steps.append(step)

        return render_template('create_recipe.html', categories=categories, ingredients=ingredients, recipe=recipe)

    elif request.method == 'POST':
        f = request.form

        recipe = Recipe()
        recipe.name = f['recipe_name'].strip()
        recipe.servings = f['recipe_servings'].strip()
        recipe.preparation_time = f['recipe_preparation_time'].strip()
        recipe.nutritional_info = f['recipe_nutritional_info'].strip()
        recipe.creator_id = g.current_user.id

        # file
        recipe.upload_file = request.files['recipe_image']

        recipe.category_names = [cat_name.strip().title()[0:29] for cat_name in f['recipe_categories'].split(',') if cat_name.strip()]
        recipe.categories_string = ', '.join(recipe.category_names)

        recipe.ingredients = []
        ingredient_names = f.getlist('recipe_ingredients[name][]')[0:-1]
        ingredient_quantities = f.getlist('recipe_ingredients[quantity][]')[0:-1]
        ingredient_units = f.getlist('recipe_ingredients[unit][]')[0:-1]
        ingredient_comments = f.getlist('recipe_ingredients[comment][]')[0:-1]
        lengths = [len(ingredient_names), len(ingredient_quantities), len(ingredient_units), len(ingredient_comments)]
        ingredient_count = min(lengths)
        for i in xrange(ingredient_count):
            if ingredient_names[i].strip():
                ingredient = Ingredient()
                ingredient.name = first_lower(ingredient_names[i].strip())
                ingredient.quantity = ingredient_quantities[i].strip()
                ingredient.unit = ingredient_units[i].strip()
                ingredient.comment = ingredient_comments[i].strip()
                recipe.ingredients.append(ingredient)

        recipe.steps = []
        step_descriptions = f.getlist('recipe_steps[]')[0:-1]
        step_number = 1
        for description in step_descriptions:
            if description.strip():
                step = Step()
                step.instructions = description.strip()
                step.number = step_number
                recipe.steps.append(step)
                step_number += 1

        if recipe.valid():
            if recipe.save(categories, ingredients):
                return redirect(url_for('recent_recipes'))
            else:
                return render_template('create_recipe.html', categories=categories, ingredients=ingredients, recipe=recipe, error="An error has occured while saving the recipe: " + recipe.error_message)
        else:
            return render_template('create_recipe.html', categories=categories, ingredients=ingredients, recipe=recipe, error="An error has occured while saving the recipe: " + recipe.error_message)      
Beispiel #13
0
    def load_recipe(cls, recipe_id, current_user):
        attributes = [
            "id",
            "name",
            "servings",
            "preparation_time",
            "photo_file",
            "created_at",
            "creator_id",
            "nutritional_info",
            "avg_ratings.avg_rating",
            "my_ratings.rating",
            "saved_counts.saved_count",
            "saved.saved_at",
            "avg_ratings.rating_count",
        ]

        join_list = []
        join_list.append(
            "LEFT OUTER JOIN (SELECT recipe_id, AVG(rating) AS avg_rating, COUNT(rating) AS rating_count FROM ratings GROUP BY recipe_id) AS avg_ratings ON recipes.id=avg_ratings.recipe_id"
        )
        join_list.append(
            "LEFT OUTER JOIN ratings AS my_ratings ON recipes.id=my_ratings.recipe_id AND my_ratings.user_id=%s"
            % adapt(str(current_user.id)).getquoted()
        )
        join_list.append(
            "LEFT OUTER JOIN (SELECT recipe_id, COUNT(user_id) AS saved_count FROM saved GROUP BY recipe_id) AS saved_counts ON recipes.id=saved_counts.recipe_id"
        )
        join_list.append(
            "LEFT OUTER JOIN saved ON recipes.id=saved.recipe_id AND saved.user_id=%s"
            % adapt(str(current_user.id)).getquoted()
        )
        join_sql = " ".join(join_list)

        where_sql = "WHERE recipes.id=%s" % adapt(str(recipe_id)).getquoted()

        results = engine.execute("SELECT %s FROM recipes %s %s" % (",".join(attributes), join_sql, where_sql))

        recipe = None
        for result in results:
            recipe = Recipe()
            recipe.id = result[0]
            recipe.name = result[1]
            recipe.servings = result[2]
            recipe.preparation_time = result[3]
            recipe.photo_file = result[4]
            recipe.created_at = result[5]
            recipe.creator_id = result[6]
            recipe.nutritional_info = result[7]
            recipe.avg_rating = result[8]
            recipe.rating = result[9]
            recipe.favorite_count = result[10]
            recipe.is_favorite = result[11] != None
            recipe.rating_count = result[12]

            if not recipe.favorite_count:
                recipe.favorite_count = 0

            if not recipe.rating_count:
                recipe.rating_count = 0

            if not recipe.avg_rating:
                recipe.avg_rating = 0.0
            else:
                recipe.avg_rating = round(recipe.avg_rating, 1)

            user = models.user.User.load_user_by_id(recipe.creator_id)
            recipe.creator = user

            recipe.steps = Step.load_steps(recipe.id)
            recipe.ingredients_recipes = IngredientRecipe.load_ingredients(recipe.id)
            recipe.categories = Category.load_categories(recipe.id)
            recipe.category_count = len(recipe.categories)
            recipe.comments = Comment.load_comments(recipe.id)

        return recipe