Пример #1
0
 def add_item_to_plan(self):
     if not current_user.is_authenticated():
         raise RequestException("access denied")
     user_id = current_user.id
     params = self._get_json_func_params(
         requiredParams=["plan_uid", "item_uid", "food_data_source_type", "servings"], 
         optionalParams=["food_id", "food_uid", "food_weight_id"])
     food_data_source_type = int(params["food_data_source_type"])
     plan = DayPlan.by_user_and_uid(user_id, params["plan_uid"])
     if not plan:
         raise RequestException("plan not found")
     item_params = {
         'servings': params["servings"],
         'food_data_source_type': food_data_source_type,
         'day_plan_id': plan.id,
         'uid': params["item_uid"]
     }
     if food_data_source_type == DayPlanItem.FOOD_DATA_SOURCE_TYPE_DEFAULT:
         item_params['chosen_default_food_id'] = params["food_id"] or 0
         item_params['chosen_default_food_weight_id'] = params["food_weight_id"] or 0
     elif food_data_source_type == DayPlanItem.FOOD_DATA_SOURCE_TYPE_CUSTOM:
         if (params["food_uid"]):
             custom_food = CustomFood.by_user_and_uid(user_id, params["food_uid"])
             if custom_food is None:
                 raise RequestException("food not found")
             item_params['chosen_custom_food_id'] = custom_food.id
         else:
             #only allow access to custom foods the user owns
             raise RequestException("access forbidden")
     else:
         raise RequestException('Unrecognized food_data_source_type')
     new_item = DayPlanItem(**item_params)
     db.session.add(new_item)
     db.session.commit()
     return True 
Пример #2
0
 def delete_custom_food(self):
     if not current_user.is_authenticated():
         raise RequestException("access denied")
     user_id = current_user.id
     params = self._get_json_func_params(requiredParams=["food_uid"])
     custom_food = CustomFood.by_user_and_uid(user_id, params["food_uid"])
     if custom_food is None:
         raise RequestException("food not found")
     #delete items using custom food
     DayPlanItem.query.filter(DayPlanItem.chosen_custom_food_id == custom_food.id).delete()
     db.session.delete(custom_food)
     db.session.commit()
     return True
Пример #3
0
 def modify_custom_food(self):
     if not current_user.is_authenticated():
         raise RequestException("access denied")
     user_id = current_user.id
     params = self._get_json_func_params(requiredParams=["food_uid", "name", "serving_size", "protein", "carbs", "fat", "calories"])
     custom_food = CustomFood.by_user_and_uid(user_id, params["food_uid"])
     if custom_food is None:
         raise RequestException("food not found")
     custom_food.name = params["name"]
     custom_food.serving_size = params["serving_size"]
     custom_food.protein = params["protein"]
     custom_food.carbs = params["carbs"]
     custom_food.fat = params["fat"]
     custom_food.calories = params["calories"]
     db.session.commit()
     return True