def add_categories_to_db(): for category in CATEGORIES: c = Category() c.from_dict(category) db.session.add(c) db.session.commit() category["id"] = c.id
def add_category(): data = request.get_json() or {} category = Category() category.from_dict(data, is_new=True) db.session.add(category) db.session.commit() response = jsonify(category.to_dict()) response.status_code = 201 # response.headers['Location'] = url_for('api.get_categories', barcode=product.barcode) return response
def add_category(): data = request.get_json() or {} if 'name' not in data: return bad_request('must include name') category = Category() category.from_dict(data) db.session.add(category) db.session.commit() response = jsonify(category.to_dict()) response.status_code = 201 response.headers['Location'] = url_for('api.get_category', id=category.id) return response
def post(self): data = request.get_json(force=True)['category'] if not get_user(): return make_error("Incorrect login info") if 'name' not in data or 'limit' not in data: return {"ERROR": "BAD REQUEST"}, 404 category = Category() category.from_dict(data) u = get_user() u.categories.append(category) db.session.commit() return category.to_dict(), 201
def create_category(): data = request.get_json() or {} if "name" not in data: return bad_request("must include name field") if "units" not in data: return bad_request("must include units field") if Category.query.filter_by(name=data["name"]).first(): return bad_request("please use a different name") category = Category() category.from_dict(data) db.session.add(category) db.session.commit() response = jsonify(category.to_dict()) response.status_code = 201 response.headers["Location"] = url_for("api.get_category", category_id=category.id) return response
def create_category(): data = request.get_json() or {} error = Category.check_data(data=data, new=True) if 'name' in data \ and Category.query.filter_by(name=data['name']).first() is not None: error = 'nome já existe' if error: return bad_request(error) category = Category() category.from_dict(data) try: db.session.add(category) db.session.commit() except Exception: return internal_server() return jsonify(category.to_dict()), 201