Ejemplo n.º 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 
Ejemplo n.º 2
0
 def remove_item_from_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"])
     plan = DayPlan.by_user_and_uid(user_id, params["plan_uid"])
     if not plan:
         raise RequestException("plan not found")
     if params["item_uid"] not in plan.items:
         raise RequestException("item not found")
     item = plan.items[params["item_uid"]]
     db.session.delete(item)
     db.session.commit()
     return True
Ejemplo n.º 3
0
 def modify_plan_item(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"], 
         optionalParams=["food_weight_id", "servings"])
     plan = DayPlan.by_user_and_uid(user_id, params["plan_uid"])
     if not plan:
         raise RequestException("plan not found")
     if params["item_uid"] not in plan.items:
         raise RequestException("item not found")
     item = plan.items[params["item_uid"]]
     if params["food_weight_id"]:
         if item.food_data_source_type == DayPlanItem.FOOD_DATA_SOURCE_TYPE_DEFAULT:
             item.chosen_default_food_weight_id = params["food_weight_id"]
         else:
             raise RequestException("custom foods do not have weight id specifiers")
     if params["servings"]:
         item.servings = params["servings"]
     db.session.commit()
     return True