def mark_recommandation(form): """ - Retrieve the marks of the user and the mark of other users on the same recipes - compare them to determine the distance between the user and other users - find the recipes that the user is interested in (according to the ingredients) and order them in function of the opinions of "close distance" users @param form form informations @return a list of ids sorted thanks to the "opinions" """ # retrieve the opinions of the users request = """ SELECT DISTINCT recipes.id FROM recipes INNER JOIN recipe_has_ingredients as ingr ON recipes.id LIKE ingr.idRecipe WHERE recipes.type_id LIKE \"{0}\" """.format(form['recipe_type']) for _ingr in form['ingr_dislike']: request += "AND ingr.idIngr NOT LIKE \"{}\"".format(_ingr) request += "AND (ingr.idIngr LIKE \"{}\"".format(form['ingr_like'].pop()) for _ingr in form['ingr_like']: request += "OR ingr.idIngr LIKE \"{}\"".format(_ingr) request += ");" recipe_id = db_execute_out(request) recipe_list = format_recipes(recipe_id)
def create_opinions(user_id): """ retrieve the recipes the user visited and didn't comment, format them then return them in a form intended to be in the left part @param user_id the id of the user @return string containing all the opinion forms """ search_rows = db_execute_out(""" SELECT DISTINCT recipe_id FROM search WHERE user_id LIKE {0} AND recipe_id NOT NULL AND recipe_id NOT IN ( SELECT DISTINCT recipe_id FROM opinions WHERE author LIKE {0} ); """.format(user_id)) if search_rows == [] or search_rows is None: return parse( """ <h4>How did you find theese recipes ?</h4><p>No recipe to comment</p> """, 'lxml').prettify(formatter='html') opinion_list = format_recipes([x[0] for x in search_rows]) # constructing the web page part config = SafeConfigParser() config.read(CONFIG_FILE) with open(config.get('html', 'opinion_form_path')) as _fd: search_panel = _fd.read() soup = parse('<h4>How did you find theese recipes ?</h4><div></div>', 'lxml') form_group = soup.div form_group['class'] = 'container-fluid' # creating a form for each recipe for recipe in opinion_list: form = parse(search_panel, 'lxml') # hidden info r_id = form.select('input#$recipe_info')[0] r_id['id'] = 'recipe_info_{}'.format(str(recipe['id'])) r_id['value'] = str(recipe['id']) u_id = form.select('input#$user_info')[0] u_id['id'] = 'user_info_{}'.format(str(recipe['id'])) u_id['value'] = str(user_id) # the form head = form.select('form#$id_form')[0] head['id'] = '{}_{}_form_head'.format(str(user_id), str(recipe['id'])) # the button button = form.select('button#$id_button')[0] button['id'] = '{}_{}_form'.format(str(user_id), str(recipe['id'])) # the img img = form.select('img')[0] img['src'] = recipe['img'] # the fav button fav_button = form.select('button#$fav_id')[0] fav_button['id'] = 'fav_{}_{}'.format(str(user_id), str(recipe['id'])) form_group.append(form) return soup.prettify(formatter='html')
def create_opinions(user_id): """ retrieve the recipes the user visited and didn't comment, format them then return them in a form intended to be in the left part @param user_id the id of the user @return string containing all the opinion forms """ search_rows = db_execute_out(""" SELECT DISTINCT recipe_id FROM search WHERE user_id LIKE {0} AND recipe_id NOT NULL AND recipe_id NOT IN ( SELECT DISTINCT recipe_id FROM opinions WHERE author LIKE {0} ); """.format(user_id)) if search_rows == [] or search_rows is None: return parse(""" <h4>How did you find theese recipes ?</h4><p>No recipe to comment</p> """, 'lxml').prettify(formatter='html') opinion_list = format_recipes([x[0] for x in search_rows]) # constructing the web page part config = SafeConfigParser() config.read(CONFIG_FILE) with open(config.get('html', 'opinion_form_path')) as _fd: search_panel = _fd.read() soup = parse('<h4>How did you find theese recipes ?</h4><div></div>', 'lxml') form_group = soup.div form_group['class'] = 'container-fluid' # creating a form for each recipe for recipe in opinion_list: form = parse(search_panel, 'lxml') # hidden info r_id = form.select('input#$recipe_info')[0] r_id['id'] = 'recipe_info_{}'.format(str(recipe['id'])) r_id['value'] = str(recipe['id']) u_id = form.select('input#$user_info')[0] u_id['id'] = 'user_info_{}'.format(str(recipe['id'])) u_id['value'] = str(user_id) # the form head = form.select('form#$id_form')[0] head['id'] = '{}_{}_form_head'.format(str(user_id), str(recipe['id'])) # the button button = form.select('button#$id_button')[0] button['id'] = '{}_{}_form'.format(str(user_id), str(recipe['id'])) # the img img = form.select('img')[0] img['src'] = recipe['img'] # the fav button fav_button = form.select('button#$fav_id')[0] fav_button['id'] = 'fav_{}_{}'.format(str(user_id), str(recipe['id'])) form_group.append(form) return soup.prettify(formatter='html')
def create_favs(user_id): """ retrieve the favorites recipes of the user and format them then return them @param user_id the id of the user @return favorites recipes formatted in html """ fav_rows = db_execute_out(""" SELECT idRecipe FROM user_has_favorite_recipes WHERE idUser LIKE \"{}\"; """.format(user_id)) if fav_rows == []: return parse( """ <h4>Favorite List :</h4><p>No favorite</p> """, 'lxml').prettify(formatter='html') favorite_list = format_recipes([x[0] for x in fav_rows]) # constructing the web page part config = SafeConfigParser() config.read(CONFIG_FILE) _fd = open(config.get('html', 'fav_panel')) fav_panel = _fd.read() _fd.close() soup = parse('<h4>Favorite List :</h4><div></div>', 'lxml') panel_group = soup.div panel_group['class'] = 'container-fluid' # creating a panel for each recipe for recipe in favorite_list: panel = parse(fav_panel, 'lxml') # the well well = panel.select('div#$id_fav')[0] well['id'] = 'well_unfav_{}_{}'.format(str(user_id), str(recipe['id'])) unfav = panel.select('button#$unfav_id')[0] unfav['id'] = 'unfav_{}_{}'.format(str(user_id), str(recipe['id'])) # the img img = panel.select('img#$fav_img')[0] img['id'] = str(recipe['id']) + '_favimg' img['src'] = recipe['img'] # the url url = panel.select('a#$fav_url')[0] url['id'] = str(recipe['id']) + '_favurl' url['href'] = recipe['url'] panel_group.append(panel) return soup.prettify(formatter='html')
def create_favs(user_id): """ retrieve the favorites recipes of the user and format them then return them @param user_id the id of the user @return favorites recipes formatted in html """ fav_rows = db_execute_out(""" SELECT idRecipe FROM user_has_favorite_recipes WHERE idUser LIKE \"{}\"; """.format(user_id)) if fav_rows == []: return parse(""" <h4>Favorite List :</h4><p>No favorite</p> """, 'lxml').prettify(formatter='html') favorite_list = format_recipes([x[0] for x in fav_rows]) # constructing the web page part config = SafeConfigParser() config.read(CONFIG_FILE) _fd = open(config.get('html', 'fav_panel')) fav_panel = _fd.read() _fd.close() soup = parse('<h4>Favorite List :</h4><div></div>', 'lxml') panel_group = soup.div panel_group['class'] = 'container-fluid' # creating a panel for each recipe for recipe in favorite_list: panel = parse(fav_panel, 'lxml') # the well well = panel.select('div#$id_fav')[0] well['id'] = 'well_unfav_{}_{}'.format(str(user_id), str(recipe['id'])) unfav = panel.select('button#$unfav_id')[0] unfav['id'] = 'unfav_{}_{}'.format(str(user_id), str(recipe['id'])) # the img img = panel.select('img#$fav_img')[0] img['id'] = str(recipe['id'])+'_favimg' img['src'] = recipe['img'] # the url url = panel.select('a#$fav_url')[0] url['id'] = str(recipe['id'])+'_favurl' url['href'] = recipe['url'] panel_group.append(panel) return soup.prettify(formatter='html')
db_execute_in([REQ]) # format the informations for the recommandation engine CLEAN_FORM = format_form_result(FORM, USER_ID) # getting the recommandation for the user # RECOMMANDATION = recommander(CLEAN_FORM) RECOMMANDATION = get_recipes( CLEAN_FORM['user_id'], CLEAN_FORM['recipe_type'], CLEAN_FORM['ingr_like'], CLEAN_FORM['ingr_dislike'] ) # formatting the result to display it RESULT = format_recipes(RECOMMANDATION) # create the list of opinions OPINIONS = create_opinions(USER_ID) # create the favorite list FAVS = create_favs(USER_ID) CONTENT = { 'title': '{} Recipes found !'.format(str(len(RESULT))), 'middle': create_recipe_list(RESULT, USER_ID), 'left': OPINIONS, 'right': FAVS } display(CONTENT)
# adding a search for the user REQ = "INSERT INTO search(user_id, recipe_id) VALUES ({}, NULL);".format( USER_ID) db_execute_in([REQ]) # format the informations for the recommandation engine CLEAN_FORM = format_form_result(FORM, USER_ID) # getting the recommandation for the user # RECOMMANDATION = recommander(CLEAN_FORM) RECOMMANDATION = get_recipes(CLEAN_FORM['user_id'], CLEAN_FORM['recipe_type'], CLEAN_FORM['ingr_like'], CLEAN_FORM['ingr_dislike']) # formatting the result to display it RESULT = format_recipes(RECOMMANDATION) # create the list of opinions OPINIONS = create_opinions(USER_ID) # create the favorite list FAVS = create_favs(USER_ID) CONTENT = { 'title': '{} Recipes found !'.format(str(len(RESULT))), 'middle': create_recipe_list(RESULT, USER_ID), 'left': OPINIONS, 'right': FAVS } display(CONTENT)