def load_meals(): meal_1 = Meal(name='breakfast') meal_2 = Meal(name='lunch') meal_3 = Meal(name='dinner') meal_4 = Meal(name='snacks') db.session.add_all([meal_1, meal_2, meal_3, meal_4]) db.session.commit()
def host_meal_process(): user = utils.get_logged_in_user() name = request.form.get('mealName') description = request.form.get('mealDescription') pick_up_hour = int(request.form.get('hour')) pick_up_minute = int(request.form.get('minute')) address = request.form.get('address') lat = float(request.form.get('lat')) lng = float(request.form.get('lng')) servings = int(request.form.get('mealServings')) image_file = request.files['file'] # We use uuid to generate a unique name for the image # We also only accept jpeg data filename_str = str(uuid.uuid4()) + ".jpg" filename = secure_filename(filename_str) filename = os.path.join(app.config['UPLOAD_FOLDER'], filename) image_file.save(filename) pickup_time = utils.datetime_from_hour_and_minute(pick_up_hour, pick_up_minute) # we need to add the slash for HTML img href filename = "/" + filename new_meal = Meal.create_new_meal(user, name, description, pickup_time, address, lat, lng, servings, filename) return redirect(url_for(".host_meal_details", meal_id=new_meal.meal_id))
def get_meals(): """Load fake meals into database.""" restaurants_in_db = db.session.query(Restaurant.yelp_restaurant_id, Restaurant.zipcode, Restaurant.rating).all() meal_types = ['dinner', 'lunch'] for restaurant in restaurants_in_db: (restaurant_id, zipcode, rating) = restaurant meal_type = random.choice(meal_types) price = round(random.uniform(10, 80), 2) percentatge_tip = create_tip_from_rating(rating) meal = Meal(yelp_restaurant_id=restaurant_id, zipcode=zipcode, meal_type=meal_type, price=price, percentage_tip=percentatge_tip, date=datetime.now()) db.session.add(meal) db.session.commit()
def load_meals(meals_filename): """Load ratings from u.data into database.""" print("Meals") for i, row in enumerate(open(meals_filename)): row = row.rstrip() # user_id, movie_id, score, timestamp = row.split("\t") # user_id = int(user_id) # movie_id = int(movie_id) # score = int(score) # We don't care about the timestamp, so we'll ignore this meal = Meal(user_id=user_id, ) # We need to add to the session or it won't ever be stored db.session.add(meal) # provide some sense of progress if i % 1000 == 0: print(i) # An optimization: if we commit after every add, the database # will do a lot of work committing each record. However, if we # wait until the end, on computers with smaller amounts of # memory, it might thrash around. By committing every 1,000th # add, we'll strike a good balance. db.session.commit() # Once we're done, we should commit our work db.session.commit()
def add_meal_and_calculate(): """Add a meal to telp_db return a json with tip in dollars, total price and price per diner """ # get information from telp.js function submitMeal formInputs price = round(float(request.form.get("price")), 2) percentage_tip = int(request.form.get("percentage_tip")) diners = int(request.form.get("diners")) restaurant_id = request.form.get("restaurant_id") # when user search by zipcode, there is no restaurant # when user search by restaurant, it is added a new meal in telp_db if restaurant_id: new_meal = Meal(yelp_restaurant_id=restaurant_id, zipcode=request.form.get("restaurant_zipcode"), meal_type=request.form.get("meal_type"), price=price, percentage_tip=percentage_tip, date=datetime.now()) db.session.add(new_meal) db.session.commit() return calculate(price, percentage_tip, diners)
def meal_api(): meal_id = request.args.get('meal_id') meal = Meal.get_meal_by_id(meal_id) if meal is None: abort(404) return jsonify(meal.serialize())
def create_meal(dog_id, meal_type, meal_date, meal_month, meal_time): meal = Meal(dog_id=dog_id, meal_type=meal_type, meal_date=meal_date, meal_month=meal_month, meal_time=meal_time) db.session.add(meal) db.session.commit() return meal
def update_db_session(meals): for meal in meals: meals_to_db = Meal(title=meal["title"], description=meal["description"]) for choice in meal["choices"]: meal_choice = MealChoice(title=choice["title"], price=choice["price"]) meals_to_db.choices.append(meal_choice) db.session.add(meals_to_db)
def new_entry(user_id): """Process new food entry.""" # Get form variables user_id = session["user_id"] food = request.form["food"] carbs = request.form["carbs"] date = request.form["date"] new_meal = Meal(user_id=user_id, food=food, carbs=carbs, date=date) db.session.add(new_meal) db.session.commit() return redirect(f"/users/{user_id}")
def host_meal_api(): user = utils.get_logged_in_user() meal_id = int(request.args.get('meal_id')) meal = Meal.get_meal_by_id(meal_id) # TODO better error reporting if meal is None: abort(404) # make sure the meal's user ID is the same as the logged in user # TODO better error reporting if meal.user_id != user.user_id: abort(400) return jsonify(meal.serialize())
def load_meals2(): pass loc = "POINT(%0.8f %0.8f)" % (-122, 37) meal_1 = Meal(meal_id=1, user_id=2, meal_type='dinein', name='hells kictchen eggs', description="scrambled eggs mmm", address='225 Ellis St, San Francisco, CA, 94102', geo=loc, servings=5) user_1 = User(user_id=2, first_name='juli', last_name='goat sacrifices', email='*****@*****.**', password='******') # meal_2 = Meal(meal_id=1, user_id=2, meal_type='dinein', # name='other egg thing', description="eggs mmm", # address='1245 S Van Ness Ave, San Francisco, CA 94110', geo=, servings=5) db.session.add(user_1) db.session.add(meal_1) db.session.commit()
def api_meals(): guest_address = request.args.get('address') guest_lat = float(request.args.get('lat')) guest_lng = float(request.args.get('lng')) start_time_hour = int(request.args.get("startTimeHour", 0)) start_time_minutes = int(request.args.get("startTimeMinutes", 0)) meters = int(request.args.get('meters', 5000)) start_time = None # 0 hour and 0 minutes is a special value that means from right now if start_time_hour == 0 and start_time_minutes == 0: start_time = datetime.datetime.now(datetime.timezone.utc) else: start_time = utils.datetime_from_hour_and_minute( start_time_hour, start_time_minutes) meals = Meal.nearby(meters, guest_lat, guest_lng, start_time) return jsonify([meal.serialize() for meal in meals])
def logged_meal(): """User loged meal will enter database""" # fetching data from form for each meal component meal_time_html = request.form.get("meal_time") if meal_time_html: local_zone = pytz.timezone("US/Pacific") meal_time_local = datetime.datetime.strptime(meal_time_html, '%Y-%m-%dT%H:%M') datetime_with_tz = local_zone.localize(meal_time_local, is_dst=None) # No daylight saving meal_time = datetime_with_tz.astimezone(pytz.utc) else: meal_time = datetime.datetime.utcnow() meal_name = request.form.get("meal_name") meal_component_40 = request.form.get("meal_component_40") meal_component_10 = request.form.get("meal_component_10") meal_component_25 = request.form.get("meal_component_25") meal_component_2_25 = request.form.get("meal_component_2_25") meal_component_drink = request.form.get("meal_component_drink") meal_component_oil = request.form.get("meal_component_oil") # getting user_id out from session and assigning it to user_id variable user_id = session.get('user_id') # adding user_id and meal_name to the meal table in database meal = Meal(user_id=user_id, meal_time=meal_time, meal_name=meal_name) # meal.users.user_id.append(meal) instead of the user_id=user_id above db.session.add(meal) db.session.commit() # fetching the meal_id of currently entered meal and storing it in variable meal_id = meal.meal_id # adding the foodgroups, meal, user, percentage to meal_foodgroups table meal_foodgroup40 = Meal_Foodgroup(meal_id=meal_id, foodgroup_id=foodgroup_dictionary[meal_component_40], percentage_meal=40) meal_foodgroup10 = Meal_Foodgroup(meal_id=meal_id, foodgroup_id=foodgroup_dictionary[meal_component_10], percentage_meal=10) meal_foodgroup25 = Meal_Foodgroup(meal_id=meal_id, foodgroup_id=foodgroup_dictionary[meal_component_25], percentage_meal=25) meal_foodgroup225 = Meal_Foodgroup(meal_id=meal_id, foodgroup_id=foodgroup_dictionary[meal_component_2_25], percentage_meal=25) meal_foodgroupdrink = Meal_Foodgroup(meal_id=meal_id, foodgroup_id=foodgroup_dictionary[meal_component_drink], percentage_meal=1) meal_foodgroupoil = Meal_Foodgroup(meal_id=meal_id, foodgroup_id=foodgroup_dictionary[meal_component_oil], percentage_meal=2) db.session.add_all([meal_foodgroup40, meal_foodgroup10, meal_foodgroup25, meal_foodgroup225, meal_foodgroupdrink, meal_foodgroupoil]) db.session.commit() return redirect('/calendar')