Beispiel #1
0
    def get(cls, handle=None):
        if handle is None:
            # Meal collection
            resp = CalorieBuilder(items=[])
            for meal in Meal.query.all():
                m = meal_to_api_meal(meal)
                m.add_control_self(api.url_for(MealItem, handle=meal.id))
                m.add_control_collection(api.url_for(MealItem, handle=None))
                add_control_edit_meal(m, handle=meal.id)

                resp['items'].append(m)
            add_control_add_meal(resp)
        else:
            # Meal item
            meal = Meal.query.filter(Meal.id == handle).first()
            if meal is None:
                return error_404()
            resp = meal_to_api_meal(meal)
            resp.add_control_collection(api.url_for(MealItem, handle=None))
            resp.add_control_delete(api.url_for(MealItem, handle=handle))
            resp.add_control_profile()
            add_control_edit_meal(resp, handle)

        # Common fields for person item and person collection
        resp.add_control_self(api.url_for(MealItem, handle=handle))
        resp.add_control(NS + ':meals-all', api.url_for(MealItem, handle=None))
        add_calorie_namespace(resp)
        return Response(json.dumps(resp),
                        200,
                        headers=add_mason_response_header())
Beispiel #2
0
    def get(cls, handle=None):
        if handle is None:
            # Portion collection
            resp = CalorieBuilder(items=[])
            for portion in Portion.query.all():
                m = portion_to_api_portion(portion)
                m.add_control_collection(api.url_for(PortionItem, handle=None))
                m.add_control_delete(api.url_for(PortionItem, handle=portion.id))
                resp['items'].append(m)
            add_control_add_portion(resp)
        else:
            # Portion item
            portion = Portion.query.filter(Portion.id == handle).first()
            if portion is None:
                return error_404()

            resp = portion_to_api_portion(portion)
            resp.add_control_collection(api.url_for(PortionItem, handle=None))
            resp.add_control_delete(api.url_for(PortionItem, handle=handle))
            resp.add_control_profile()
            add_control_edit_portion(resp, handle)

        # Common fields for portion item and portion collection
        resp.add_control_self(api.url_for(PortionItem, handle=handle))
        resp.add_control(NS+':portions-all', api.url_for(PortionItem, handle=None))
        add_calorie_namespace(resp)
        return Response(json.dumps(resp), 200, headers=add_mason_response_header())
def entrypoint():
    resp = CalorieBuilder()
    resp.add_control(NS + ':persons-all', api.url_for(PersonItem, handle=None))
    resp.add_control(NS + ':meals-all', api.url_for(MealItem, handle=None))
    resp.add_control(NS + ':portions-all', api.url_for(PortionItem,
                                                       handle=None))
    add_calorie_namespace(resp)
    return Response(json.dumps(resp), 200, headers=add_mason_response_header())
Beispiel #4
0
    def get(cls, meal=None, handle=None, person_id=None):

        if handle is None and person_id is not None:
            resp = CalorieBuilder(items=[])
            for mealrecord in MealRecord.query.filter(
                    MealRecord.person_id == person_id):
                m = mealrecord_to_api_mealrecord(mealrecord)
                m.add_control_collection(
                    api.url_for(MealRecordItem, meal=None, handle=None))
                resp['items'].append(m)
            add_control_add_mealrecord(resp)

        elif handle is None:
            # MealRecord collection
            resp = CalorieBuilder(items=[])
            for mealrecord in MealRecord.query.all():
                m = mealrecord_to_api_mealrecord(mealrecord)
                m.add_control_collection(
                    api.url_for(MealRecordItem, meal=None, handle=None))
                resp['items'].append(m)
            add_control_add_mealrecord(resp)
        else:
            # MealRecord item
            person, meal_id, timestamp = split_mealrecord_handle(meal, handle)

            mealrecord = MealRecord.query.filter(
                MealRecord.person_id == person, MealRecord.meal_id == meal_id,
                MealRecord.timestamp == timestamp).first()
            if mealrecord is None:
                return error_404()
            resp = mealrecord_to_api_mealrecord(mealrecord)
            resp.add_control_collection(
                api.url_for(MealRecordItem, meal=None, handle=None))
            resp.add_control_delete(
                api.url_for(MealRecordItem, meal=meal, handle=handle))
            resp.add_control_profile()
            add_control_edit_mealrecord(resp, meal, handle)

        # Common fields for mealrecord item and mealrecord collection
        resp.add_control_self(
            api.url_for(MealRecordItem, meal=meal, handle=handle))
        resp.add_control(NS + ':mealrecords-all',
                         api.url_for(MealRecordItem, meal=None, handle=None))
        add_calorie_namespace(resp)
        return Response(json.dumps(resp, default=myconverter),
                        200,
                        headers=add_mason_response_header())
    def get(cls, meal, handle):
        # MealPortion
        meal_id, portion_id = decode_handle(meal, handle)

        mealportion = MealPortion.query.filter(
            MealPortion.meal_id == meal_id,
            MealPortion.portion_id == portion_id).first()
        if mealportion is None:
            return error_404()
        resp = mealportion_to_api_mealportion(mealportion)

        resp.add_control_collection(
            api.url_for(MealItem, handle=meal_id) + "mealportions/")
        resp.add_control_delete(
            api.url_for(MealPortionItem, meal=meal, handle=handle))
        resp.add_control_profile()
        add_control_edit_mealportion(resp, meal, handle)

        resp.add_control_self(
            api.url_for(MealPortionItem, meal=meal, handle=handle))
        add_calorie_namespace(resp)
        return Response(json.dumps(resp),
                        200,
                        headers=add_mason_response_header())