healthier/views.py
def results(request):
    """
    returns results for searched replacement food or error/ 
    allows AJAX to add a replacement item to favorites
    """
    request.session["visited"] = True
    form1 = FoodQuery(auto_id="form1")
    message = {
        "form1": form1,
        "food_items": "",
        "searched": "",
    }
    if "form" in request.GET or "form1" in request.GET:
        # search for replacement food if a form is present in request
        form = FoodQuery(data=request.GET, auto_id="form")
        if form.is_valid():
            results = Food_item.get_searched_food_Item(
                food_name=form.cleaned_data.get("name")
            )
        else:
            message.update({"form": form})
            return render(request, "healthier/_no_results.html", message)
    elif "id" in request.GET:
        # search directly by id if id is present in request
        # (used in the case of the user has had to choose between several items following a foodname search)
        results = Food_item.get_searched_food_Item(food_id=request.GET["id"])
        form = FoodQuery(data={"name":results.get("to_be_replaced_item")}, auto_id="form")
    elif request.method == "POST":
        # handle AJAX request to add an item to user's favorites on ly available when logged-in
        results = Food_item.save_favorites(request.POST["value"], request.user)
        return HttpResponse(json.dumps(results))
    else:
        raise Http404("pas d'aliments recherchés dans la requette")

    if results["status"] == "ok":
        message["food_items"] = results["replacement_items"]
        message["searched"] = results["to_be_replaced_item"]
        return render(request, "healthier/_results.html", message)

    elif results["status"] == "choice_to_make":
        if results["to_be_replaced_item"].count() < 100:
            # arbitrary limiting the number of returned choices to 99
            form.add_error(
                None,
                "Il existe "
                + str(results["to_be_replaced_item"].count())
                + " aliments contenant '"
                + form.cleaned_data.get("name")
                + "' !"
                " merci de choisir l'aliment à remplacer dans la liste ci dessous.",
            )
        else:
            # if more than 99 items are found the user is asked to be more specific
            form.add_error(
                None,
                "Il existe "
                + str(results["to_be_replaced_item"].count())
                + " aliments contenant '"
                + form.cleaned_data.get("name")
                + "' !"
                " merci de préciser votre recherche.",
            )
        message.update({"form": form})
        message["searched"] = results["to_be_replaced_item"]

    elif results["status"] == "not_found":
        # if the name searched was not in DB the user is warned
        form.add_error(
            None,
            form.cleaned_data.get("name")
            + " est introuvable dans notre liste d'aliments ! Merci de renouveller votre recherche",
        )
        message.update({"form": form})
        # if no healthier replacement is found in DB the user is also warned
    elif results["status"] == "no_replacement":
        form.add_error(
            None,
            "il n'existe pas à ce jour d'aliment de remplacement plus sain dans notre base de données.",
        )
        message.update({"form": form})
    return render(request, "healthier/_no_results.html", message)

    returns results for searched replacement food or error/ 
    allows AJAX to add a replacement item to favorites
    
returns results for searched replacement food or error
allows AJAX to add a replacement item to favorites
returns results for searched replacement food or error allows AJAX to add a replacement item to favorites
def results(request):
    request.session["visited"] = True
    form1 = FoodQuery(auto_id="form1")
    message = {
        "form1": form1,
        "food_items": "",
        "searched": "",
    }
    if "form" in request.GET or "form1" in request.GET:
        form = FoodQuery(data=request.GET, auto_id="form")
        if form.is_valid():
            results = Food_item.get_searched_food_Item(
                food_name=form.cleaned_data.get("name")
            )
        else:
            message.update({"form": form})
            return render(request, "healthier/_no_results.html", message)
    elif "id" in request.GET:
        results = Food_item.get_searched_food_Item(food_id=request.GET["id"])
        form = FoodQuery(data={"name":results.get("to_be_replaced_item")}, auto_id="form")
    elif request.method == "POST":
        results = Food_item.save_favorites(request.POST["value"], request.user)
        return HttpResponse(json.dumps(results))
    else:
        raise Http404("pas d'aliments recherchés dans la requette")
    if results["status"] == "ok":
        message["food_items"] = results["replacement_items"]
        message["searched"] = results["to_be_replaced_item"]
        return render(request, "healthier/_results.html", message)
    elif results["status"] == "choice_to_make":
        if results["to_be_replaced_item"].count() < 100:
            form.add_error(
                None,
                "Il existe "
                + str(results["to_be_replaced_item"].count())
                + " aliments contenant '"
                + form.cleaned_data.get("name")
                + "' !"
                " merci de choisir l'aliment à remplacer dans la liste ci dessous.",
            )
        else:
            form.add_error(
                None,
                "Il existe "
                + str(results["to_be_replaced_item"].count())
                + " aliments contenant '"
                + form.cleaned_data.get("name")
                + "' !"
                " merci de préciser votre recherche.",
            )
        message.update({"form": form})
        message["searched"] = results["to_be_replaced_item"]
    elif results["status"] == "not_found":
        form.add_error(
            None,
            form.cleaned_data.get("name")
            + " est introuvable dans notre liste d'aliments ! Merci de renouveller votre recherche",
        )
        message.update({"form": form})
    elif results["status"] == "no_replacement":
        form.add_error(
            None,
            "il n'existe pas à ce jour d'aliment de remplacement plus sain dans notre base de données.",
        )
        message.update({"form": form})
    return render(request, "healthier/_no_results.html", message)
def results ( request ) : request . session [ "visited" ] = True form1 = FoodQuery ( auto_id = "form1" ) message = { "form1" : form1 , "food_items" : "" , "searched" : "" , } if "form" in request . GET or "form1" in request . GET : form = FoodQuery ( data = request . GET , auto_id = "form" ) if form . is_valid ( ) : results = Food_item . get_searched_food_Item ( food_name = form . cleaned_data . get ( "name" ) ) else : message . update ( { "form" : form } ) return render ( request , "healthier/_no_results.html" , message ) elif "id" in request . GET : results = Food_item . get_searched_food_Item ( food_id = request . GET [ "id" ] ) form = FoodQuery ( data = { "name" : results . get ( "to_be_replaced_item" ) } , auto_id = "form" ) elif request . method == "POST" : results = Food_item . save_favorites ( request . POST [ "value" ] , request . user ) return HttpResponse ( json . dumps ( results ) ) else : raise Http404 ( "pas d'aliments recherchés dans la requette") if results [ "status" ] == "ok" : message [ "food_items" ] = results [ "replacement_items" ] message [ "searched" ] = results [ "to_be_replaced_item" ] return render ( request , "healthier/_results.html" , message ) elif results [ "status" ] == "choice_to_make" : if results [ "to_be_replaced_item" ] . count ( ) < 100 : form . add_error ( None , "Il existe " + str ( results [ "to_be_replaced_item" ] . count ( ) ) + " aliments contenant '" + form . cleaned_data . get ( "name" ) + "' !" " merci de choisir l'aliment à remplacer dans la liste ci dessous.", ) else : form . add_error ( None , "Il existe " + str ( results [ "to_be_replaced_item" ] . count ( ) ) + " aliments contenant '" + form . cleaned_data . get ( "name" ) + "' !" " merci de préciser votre recherche.", ) message . update ( { "form" : form } ) message [ "searched" ] = results [ "to_be_replaced_item" ] elif results [ "status" ] == "not_found" : form . add_error ( None , form . cleaned_data . get ( "name" ) + " est introuvable dans notre liste d'aliments ! Merci de renouveller votre recherche" , ) message . update ( { "form" : form } ) elif results [ "status" ] == "no_replacement" : form . add_error ( None , "il n'existe pas à ce jour d'aliment de remplacement plus sain dans notre base de données.", ) message . update ( { "form" : form } ) return render ( request , "healthier/_no_results.html" , message )
returns results for searched replacement food or error
allows AJAX to add a replacement item to favorites
returns results for searched replacement food or error allows AJAX to add a replacement item to favorites
"""
    returns results for searched replacement food or error/ 
    allows AJAX to add a replacement item to favorites
    """ # search for replacement food if a form is present in request # search directly by id if id is present in request # (used in the case of the user has had to choose between several items following a foodname search) # handle AJAX request to add an item to user's favorites on ly available when logged-in # arbitrary limiting the number of returned choices to 99 # if more than 99 items are found the user is asked to be more specific # if the name searched was not in DB the user is warned # if no healthier replacement is found in DB the user is also warned
request