コード例 #1
0
 def put(self):
     u_id = session.get('id')
     r_id = request.form.get('r_id')
     # query recipe
     recipe = Recipe.query.filter_by(id=r_id).first()
     # check correctness and  user authority
     if not u_id or not recipe or recipe.author != int(u_id):
         return {'res': False}, 400
     # update title of recipe
     recipe.title = pre_process(request.form.get('title'), 'title')
     # update ingredients of recipe
     RecIng.query.filter_by(recipe=recipe.id).delete()
     ingredients = eval(request.form.get('ingredients'))
     self.connect_rec_ing(recipe, ingredients)
     # update recipe type
     RelRecType.query.filter_by(r_id=recipe.id).delete()
     categories = eval(request.form.get('select_cat'))
     categories = [int(re.findall('[0-9]+', c)[0]) for c in categories]
     for c in categories:
         rel = RelRecType(r_id=recipe.id,
                          t_id=RecType.query.filter_by(id=c).first().id)
         db.session.add(rel)
     # update cook time
     cook_hour = int(request.form.get('hour'))
     cook_minus = int(request.form.get('minus'))
     recipe.cook_time = cook_hour * 60 + cook_minus
     # update update time
     recipe.update_time = time_now()
     # update steps
     steps = eval(request.form.get('steps'))
     steps = '\n'.join(
         [f'{i + 1}: {step.strip()}' for i, step in enumerate(steps)])
     recipe.steps = steps
     # update describe
     describe = request.form.get('describe')
     describe = pre_process(describe)
     recipe.describe = describe
     # update img
     img = request.form.get('img')
     if img:
         img_type = img.rsplit('.')[1]
         if img_type not in self.allow_file_type:
             return {'res': False}, 400
         img_path = f'Static/img/RecipeImg/temp{int(u_id)}.{img_type}'
         new_path = f'Static/img/RecipeImg/recipe_{recipe.id}.{img_type}'
         os.rename(os.path.join(BASE_DIR, img_path),
                   os.path.join(BASE_DIR, new_path))
         recipe.img_link = new_path
         recipe.img_type = img_type
     user = User.query.filter_by(id=u_id).first()
     if user.max_exp <= 30:
         user.exp += 1
         user.max_exp += 1
     db.session.commit()
     return {'res': True}
コード例 #2
0
    def post(self):
        # check user authority
        u_id = session.get('id')
        if not u_id:
            return {'res': False}, 400
        # receive uploaded info
        title = pre_process(request.form.get('title'), 'title')
        ingredients = eval(request.form.get('ingredients'))
        categories = eval(request.form.get('select_cat'))
        cook_hour = int(request.form.get('hour'))
        cook_minus = int(request.form.get('minus'))
        describe = request.form.get('describe')
        describe = pre_process(describe)
        steps = eval(request.form.get('steps'))
        img_type = request.form.get('img').rsplit('.')[1]
        # check file type
        if img_type not in self.allow_file_type:
            return {'res': False}, 400
        steps = '\n'.join(
            [f'{i+1}: {step.strip()}' for i, step in enumerate(steps)])
        img_path = f'Static/img/RecipeImg/temp{int(u_id)}.{img_type}'

        # create recipe
        recipe = Recipe(title=title,
                        steps=steps,
                        update_time=time_now(),
                        author=session.get('id'),
                        cook_time=cook_hour * 60 + cook_minus,
                        describe=describe,
                        img_type=img_type)
        db.session.add(recipe)
        categories = [int(re.findall('[0-9]+', c)[0]) for c in categories]
        # connect recipe and its recipe category
        user = User.query.filter_by(id=u_id).first()
        for c in categories:
            rel = RelRecType(r_id=recipe.id,
                             t_id=RecType.query.filter_by(id=c).first().id)
            db.session.add(rel)
        # connect ingredients to recipe
        self.connect_rec_ing(recipe, ingredients)
        db.session.commit()
        # add the img link for this recipe
        new_path = f'Static/img/RecipeImg/recipe_{recipe.id}.{img_type}'
        os.rename(os.path.join(BASE_DIR, img_path),
                  os.path.join(BASE_DIR, new_path))
        recipe.img_link = new_path

        user.exp += 10
        db.session.commit()
        return jsonify({'res': True})
コード例 #3
0
ファイル: recommend.py プロジェクト: DsDoctor/Meal-Match
 def get(self):
     result = []
     rec_list = []
     # search string pre process
     strings = pre_process(request.args.get('string'), 'search')
     # read running list
     running_list = session.get('list')
     running_list = eval(session.get('list')) if running_list else []
     running_list = [ing['id'] for ing in running_list]
     rec_list.extend(running_list)
     # for each searched ingredient
     for ing in strings:
         ing = Ingredient.query.filter_by(name=ing).first()
         # if searched ingredient in db, recommend user add it to running list
         if ing:
             if ing.id not in rec_list:
                 result.append({'id': ing.id, 'ingredient': ing.name})
                 rec_list.append(ing.id)
     # recommend next ingredients
     for ex_ing in rec_list[::-1]:
         # maximum of 6 recommend ingredients
         if len(result) < 6:
             temp_list = []
             # query all recipe including that ingredient
             recipes = RecIng.query.filter_by(ingredient=ex_ing).all()
             for recipe in recipes:
                 #
                 next_ing_list = RecIng.query.filter_by(
                     recipe=recipe.recipe).all()
                 for next_ing in next_ing_list:
                     ing = Ingredient.query.filter_by(
                         id=next_ing.ingredient).first()
                     if ing.id not in rec_list:
                         temp_list.append(ing)
                         rec_list.append(ing.id)
             for i, ing in enumerate(temp_list):
                 try:
                     temp_list[i] = (ing,
                                     self.model.wv.similarity(
                                         ing.name,
                                         Ingredient.query.filter_by(
                                             id=ex_ing).first().name))
                 except KeyError:
                     temp_list[i] = (ing, 0)
             temp_list.sort(key=lambda x: x[1], reverse=True)
             temp_list = list(
                 map(lambda x: {
                     'id': x[0].id,
                     'ingredient': x[0].name
                 }, temp_list))
             result.extend(temp_list[:6 - (len(result))])
         else:
             return jsonify(result)
     return jsonify(result)
コード例 #4
0
ファイル: recommend.py プロジェクト: DsDoctor/Meal-Match
 def get(self):
     # string preprocess
     ingredient = pre_process(request.args.get('ingredient'), 'ingredient')
     # not an ingredient return 400
     if len(ingredient) <= 2 or not ingredient.replace(' ', '').isalpha():
         return {'msg': 'Maybe not an ingredient?'}, 400
     # try to find ingredient in db, if db does not have this ingredient return type=''
     ing = Ingredient.query.filter_by(name=ingredient).first()
     if ing:
         # select (type_id, ingredient_id, count(type))
         res = db.session.query(RelIngType.type, RelIngType.ingredient, db.func.count(RelIngType.type)).\
             filter_by(ingredient=ing.id).group_by('type').order_by(db.func.count(RelIngType.type).desc()).first()
         if res:
             ing_type_id = res[0]
             # find type name and return
             ing = IngType.query.filter_by(id=ing_type_id).first()
             return jsonify({'type': ing.name})
     else:
         return jsonify({'type': ''})
コード例 #5
0
ファイル: comment.py プロジェクト: DsDoctor/Meal-Match
 def post(self):
     # get user id
     user = session.get('id')
     # if not login
     if not user:
         return {'res': False}, 400
     # preprocess string
     string = pre_process(request.form.get('comment'), 'comment')
     comment = Comment(content=string,
                       user=user,
                       recipe=request.form.get('r_id'),
                       update_time=time_now())
     user = User.query.filter_by(id=user).first()
     if user.max_exp <= 30:
         user.exp += 1
         user.max_exp += 1
     db.session.add(comment)
     db.session.commit()
     return jsonify({'res': True})