コード例 #1
0
    def post(cls):
        try:
            if request.json is None:
                return error_415()
        except BadRequest:
            return error_415()

        try:
            validate(request.json, schema=meal_schema())
        except (SchemaError, ValidationError):
            return error_400()

        meal_id = request.json['id']
        meal_name = request.json['name']
        meal_servings = request.json['servings']

        meal = Meal(id=meal_id, name=meal_name, servings=meal_servings)

        if 'description' in request.json.keys():
            meal.description = request.json['description']

        db.session.add(meal)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            return error_409()

        h = add_mason_response_header()
        h.add('Location', api.url_for(MealItem, handle=meal.id))

        return Response(status=201, headers=h)
コード例 #2
0
    def post(cls):
        try:
            if request.json is None:
                return error_415()
        except BadRequest:
            return error_415()

        try:
            validate(request.json, schema=person_schema())
        except (SchemaError, ValidationError):
            return error_400()

        person_id = request.json['id']
        person = Person(id=person_id)
        db.session.add(person)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            return error_409()

        h = add_mason_response_header()
        h.add('Location', api.url_for(PersonItem, handle=person.id))

        return Response(
            status=201,
            headers=h
        )
コード例 #3
0
    def post(cls, handle):
        try:
            if request.json is None:
                return error_415()
        except BadRequest:
            return error_415()

        try:
            validate(request.json, schema=mealportion_schema())
        except (SchemaError, ValidationError):
            return error_400()

        meal_id = handle
        portion_id = request.json['portion_id']
        mealportion = MealPortion(
            meal_id=meal_id,
            portion_id=portion_id,
            weight_per_serving=request.json['weight_per_serving'])

        db.session.add(mealportion)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            return error_409()

        h = add_mason_response_header()
        h.add(
            'Location',
            api.url_for(MealPortionItem,
                        meal=meal_id,
                        handle=make_mealportion_handle(meal_id, portion_id)))

        return Response(status=201, headers=h)
コード例 #4
0
def add_control_edit_mealrecord(resp, meal, handle):
    resp.add_control(
        NS + ":edit-mealrecord",
        href=api.url_for(MealRecordItem, meal=meal, handle=handle),
        method="PUT",
        # TODO: json or should it be application/json?
        encoding="json",
        title="Edits a MealRecord",
        schema=mealrecord_schema())
コード例 #5
0
def add_control_add_mealrecord(resp):
    resp.add_control(
        NS + ":add-mealrecord",
        href=api.url_for(MealRecordItem, meal=None, handle=None),
        method="POST",
        # TODO: json or should it be application/json?
        encoding="json",
        title="Creates a new MealRecord",
        schema=mealrecord_schema())
コード例 #6
0
def add_control_add_person(resp):
    resp.add_control(
        NS + ":add-person",
        href=api.url_for(PersonItem, handle=None),
        method="POST",
        encoding="json",
        title="Creates a new Person",
        schema=person_schema()
    )
コード例 #7
0
def add_control_edit_meal(resp, handle):
    resp.add_control(
        NS + ":edit-meal",
        href=api.url_for(MealItem) + handle + '/',
        method="PUT",
        # TODO: json or should it be application/json?
        encoding="json",
        title="Edits a Meal",
        schema=meal_schema())
コード例 #8
0
def add_control_edit_portion(resp, handle):
    resp.add_control(
        NS + ":edit-portion",
        href=api.url_for(PortionItem) + handle + '/',
        method="PUT",
        # TODO: json or should it be application/json?
        encoding="json",
        title="Edits a Portion",
        schema=portion_schema()
    )
コード例 #9
0
def add_control_add_portion(resp):
    resp.add_control(
        NS + ":add-portion",
        href=api.url_for(PortionItem, handle=None),
        method="POST",
        # TODO: json or should it be application/json?
        encoding="json",
        title="Creates a new Portion",
        schema=portion_schema()
    )
コード例 #10
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())
コード例 #11
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())
コード例 #12
0
    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())
コード例 #13
0
    def post(cls):
        try:
            if request.json is None:
                return error_415()
        except BadRequest:
            return error_415()

        try:
            validate(request.json, schema=mealrecord_schema())
        except (SchemaError, ValidationError):
            return error_400()

        mealrecord_person = request.json['person_id']
        mealrecord_meal = request.json['meal_id']
        mealrecord_amount = request.json['amount']
        mealrecord_timestamp = request.json['timestamp']

        mealrecord = MealRecord(person_id=mealrecord_person,
                                meal_id=mealrecord_meal,
                                amount=mealrecord_amount,
                                timestamp=datetime.datetime.strptime(
                                    mealrecord_timestamp,
                                    '%Y-%m-%d %H:%M:%S.%f'))

        db.session.add(mealrecord)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            return error_409()

        h = add_mason_response_header()
        h.add(
            'Location',
            api.url_for(MealRecordItem,
                        meal=mealrecord.meal_id,
                        handle=make_mealrecord_handle(mealrecord.person_id,
                                                      mealrecord.meal_id,
                                                      mealrecord.timestamp)))

        return Response(status=201, headers=h)
コード例 #14
0
    def post(cls):
        try:
            if request.json is None:
                return error_415()
        except BadRequest:
            return error_415()

        try:
            validate(request.json, schema=portion_schema())
        except (SchemaError, ValidationError):
            return error_400()

        portion_id = request.json['id']
        portion_name = request.json['name']
        portion_calories = request.json['calories']
        optional_fields = extract_optional_fields(request.json)

        # First set required fields
        portion = Portion(
            id=portion_id,
            name=portion_name,
            calories=portion_calories
        )
        # Then optional, which are stored in 'found' dict
        for k in optional_fields.keys():
            setattr(portion, k, optional_fields[k])

        db.session.add(portion)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            return error_409()

        h = add_mason_response_header()
        h.add('Location', api.url_for(PortionItem, handle=portion.id))

        return Response(
            status=201,
            headers=h
        )
コード例 #15
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())