Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
0
 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
Esempio n. 5
0
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
Esempio n. 6
0
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