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)
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 )
def put(cls, meal, 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, 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() # Only weight per serving can be edited mealportion.weight_per_serving = request.json['weight_per_serving'] db.session.add(mealportion) try: db.session.commit() except IntegrityError: db.session.rollback() return error_409() return Response(response="", status=204, headers=add_mason_response_header())
def put(cls, handle): 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 = Meal.query.filter(Meal.id == handle).first() if meal is None: return error_404() # We don't support a change of ID, so ID field from the request is ignored meal.name = request.json['name'] meal.servings = request.json['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() return Response(response="", status=204, headers=add_mason_response_header())
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)
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)
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 )
def put(cls, meal, handle): 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() 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() mealrecord.person_id = request.json['person_id'] mealrecord.meal_id = request.json['meal_id'] mealrecord.amount = request.json['amount'] mealrecord.timestamp = datetime.datetime.strptime( request.json['timestamp'], '%Y-%m-%d %H:%M:%S.%f') db.session.add(mealrecord) try: db.session.commit() except IntegrityError: db.session.rollback() return error_409() return Response(response="", status=204, headers=add_mason_response_header())
def put(cls, handle): 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 = Portion.query.filter(Portion.id == handle).first() if portion is None: return error_404() # We don't support a change of ID, so ID field from the request is ignored portion.name = request.json['name'] portion.calories = request.json['calories'] optional_fields = extract_optional_fields(request.json) 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() return Response( response="", status=204, headers=add_mason_response_header() )