コード例 #1
0
ファイル: views.py プロジェクト: cargo8/eat_healthy_for_less
def menu(request, form):
    calories_per_day = form.cleaned_data['calories_per_day']
    budget = form.cleaned_data['budget']
    address = form.cleaned_data['address']

    constraint_names = []
    for constraint_name in ('low_sugar', 'high_fiber', 'low_sodium'):
        if form.cleaned_data[constraint_name]:
            constraint_names.append(constraint_name)

    # choose recipes that match constraints
    recipes = recipe.read_recipes()
    recipes = [r for r in recipes
               if r.satisfies_constraints(constraint_names)]

    calories_per_meal = calories_per_day * 0.75
    menu = MealSelector.select_optimal_menu(recipes, budget, calories_per_meal)
    alternate_meals = [r for r in recipes if r not in menu]
    for m in alternate_meals:
        m.est_price = m.get_display_price(calories_per_meal)
        m.est_price = str(m.get_display_price(calories_per_meal))[:5]
        m.cook_time = m.convenience/60
        m.img = m.images[0]['hostedSmallUrl']
    for m in menu:
        m.est_price = str(m.get_display_price(calories_per_meal))[:5]
        m.cook_time = m.convenience/60
        m.img = m.images[0]['hostedSmallUrl']
        m.alterns = alternate_meals
    # debug
    print >> sys.stderr, 'menu:', [r.name for r in menu]
    return render(request, 'results.html', {'results_set':menu, 'cals':calories_per_meal})
コード例 #2
0
def menu(request, form):
    calories_per_day = form.cleaned_data['calories_per_day']
    budget = form.cleaned_data['budget']
    address = form.cleaned_data['address']

    constraint_names = []
    for constraint_name in ('low_sugar', 'high_fiber', 'low_sodium'):
        if form.cleaned_data[constraint_name]:
            constraint_names.append(constraint_name)

    # choose recipes that match constraints
    recipes = recipe.read_recipes()
    recipes = [r for r in recipes if r.satisfies_constraints(constraint_names)]

    calories_per_meal = calories_per_day * 0.75
    menu = MealSelector.select_optimal_menu(recipes, budget, calories_per_meal)
    alternate_meals = [r for r in recipes if r not in menu]
    for m in alternate_meals:
        m.est_price = m.get_display_price(calories_per_meal)
        m.est_price = str(m.get_display_price(calories_per_meal))[:5]
        m.cook_time = m.convenience / 60
        m.img = m.images[0]['hostedSmallUrl']
    for m in menu:
        m.est_price = str(m.get_display_price(calories_per_meal))[:5]
        m.cook_time = m.convenience / 60
        m.img = m.images[0]['hostedSmallUrl']
        m.alterns = alternate_meals
    # debug
    print >> sys.stderr, 'menu:', [r.name for r in menu]
    return render(request, 'results.html', {
        'results_set': menu,
        'cals': calories_per_meal
    })
コード例 #3
0
def select_optimal_menu(budget, calories_per_meal, constraint_names):
    # choose recipes that match constraints
    recipes = recipe.read_recipes()
    recipes = [r for r in recipes
               if r.satisfies_constraints(constraint_names)]

    penalizer = MealPenalizer(budget, calories_per_meal)
    driver = SelectionDriver(penalizer.penalty)
    menu = driver.pick_menu(recipes)
    return menu
コード例 #4
0
ファイル: views.py プロジェクト: cargo8/eat_healthy_for_less
def savemenu(request):
  meal_set = [request.GET.get('meal-1'), request.GET.get('meal-2'),
  request.GET.get('meal-3'),request.GET.get('meal-4'),request.GET.get('meal-5'),
  request.GET.get('meal-6'),request.GET.get('meal-7')]
  cals = float(request.GET.get('cals'))
  recipe_set = list()
  recipes = dict(((i.name, i) for i in read_recipes()))
  # print recipes.keys()
  for id in meal_set:
    if (id == '0'):
        continue
    recipe_set.append(recipes[id])
  res_set, price = get_menu_price(recipe_set, cals)
  print res_set, price
  return render(request, 'shoppinglist.html', {'results_set':res_set, 'total':price})
コード例 #5
0
def savemenu(request):
    meal_set = [
        request.GET.get('meal-1'),
        request.GET.get('meal-2'),
        request.GET.get('meal-3'),
        request.GET.get('meal-4'),
        request.GET.get('meal-5'),
        request.GET.get('meal-6'),
        request.GET.get('meal-7')
    ]
    cals = float(request.GET.get('cals'))
    recipe_set = list()
    recipes = dict(((i.name, i) for i in read_recipes()))
    # print recipes.keys()
    for id in meal_set:
        if (id == '0'):
            continue
        recipe_set.append(recipes[id])
    res_set, price = get_menu_price(recipe_set, cals)
    print res_set, price
    return render(request, 'shoppinglist.html', {
        'results_set': res_set,
        'total': price
    })
コード例 #6
0
import os
import sys
import json

# HACK HACK
os.environ['DJANGO_SETTINGS_MODULE'] = 'ehfl.settings'
PARENT_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), '')
sys.path.insert(0, PARENT_DIR)

from glob import glob

from ehfl.models import Ingredient
from ehfl.recipe import read_recipes

recipes = read_recipes()
all_ingredients = {}
all_units = {}
for r in recipes:
    for qty, unit, name in r.ingredientLines:
        all_ingredients[name] = True
        all_units[unit] = True
        Ingredient.objects.get_or_create(name=name,
                                         defaults={'price_per_kg': 2.00})

ingredients = list(sorted(all_ingredients.keys()))
print
print 'ALL INGREDIENTS'
print
for ing in ingredients:
    print ing
コード例 #7
0
import sys
import json
import numpy

# HACK HACK
os.environ['DJANGO_SETTINGS_MODULE'] = 'ehfl.settings'
PARENT_DIR = os.path.join(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))), '')
sys.path.insert(0, PARENT_DIR)

from glob import glob

from ehfl.models import Ingredient
from ehfl.recipe import read_recipes

recipes = read_recipes()

print 'MIN_SATURATED_FAT_PER_CALORIE =', min(
    [r.saturated_fat_per_calorie for r in recipes])
print 'MAX_SATURATED_FAT_PER_CALORIE =', max(
    [r.saturated_fat_per_calorie for r in recipes])

# constraint thresholds
print 'MAX_SODIUM_PER_CALORIE =', numpy.median(
    [r.sodium_per_calorie for r in recipes])
print 'MAX_SUGAR_PER_CALORIE =', numpy.median(
    [r.sugar_per_calorie for r in recipes])
print 'MIN_FIBER_PER_CALORIE =', numpy.median(
    [r.fiber_per_calorie for r in recipes])

for r in recipes: