def create_category(data, user): try: new_category = CategoryModel(**data) new_category.save() return jsonify(GetCategorySchema().dump(new_category)), 201 except IntegrityError: raise BadRequestException(CATEGORY_NAME_EXIST)
def create_category(data): """Create a new category.""" # check if new category's name has existed." if CategoryModel.query.filter_by(name=data['name']).first(): raise BadRequest(f"Category {data['name']} existed.") # save category to database & response to client category = CategoryModel(**data) category.save_to_db() return jsonify(CategorySchema().dump(category)), 201
def new_category(): cat_data = request.json schema = CategorySchema() schema.load(cat_data) category = CategoryModel(cat_data['name'], cat_data['description']) category.save_to_db() return jsonify({ 'id': category.id, 'name': category.name, 'description': category.description }), 201
def insert_test_data(): for data in user_data: UserModel(**data).save() for data in category_data: CategoryModel(**data).save() for data in item_data: ItemModel(**data).save()
def category_add(): form = CategoryForm() if form.validate_on_submit(): new_category = CategoryModel( name=form.name.data, title=form.title.data, ) db.session.add(new_category) db.session.commit() return redirect(url_for('category')) return render_template('category_add.html', form=form)
def init_db(): user1 = UserModel('Test User1', 'testuser1', 'testpassword123', '*****@*****.**') user2 = UserModel('Test User2', 'testuser2', 'testpassword123', '*****@*****.**') category1 = CategoryModel('Category 1', 'Test category number 1') category2 = CategoryModel('Category 2', 'Test category number 2') item1 = ItemModel('Item 1', 'Test item number 1', 10, 1, 1) item2 = ItemModel('Item 2', 'Test item number 2', 20, 1, 1) user1.save_to_db() user2.save_to_db() category1.save_to_db() category2.save_to_db() item1.save_to_db() item2.save_to_db() yield db db.session.close() db.drop_all()
def init_categories(): """Create 2 sample categories.""" CategoryModel('soccer').save_to_db() CategoryModel('badminton').save_to_db()