def fake_categories(count=10): category = Category(name='Default', title="Default") db.session.add(category) for i in range(count): category = Category(name=fake.word(), title=fake.word()) db.session.add(category) try: db.session.commit() except IntegrityError: db.session.rollback()
def init(username, password): """创客一个博客系统""" # 初始化数据库用户名密码 click.echo("initializing database...") db.create_all() admin = Admin.query.first() # 如果已经存在管理员记录,就更新 if admin: click.echo("该管理员已存在,更新中...") admin.username = username admin.set_password(password) # 否则创建新的管理员 else: click.echo("创建一个新的管理员...") admin = Admin( username=username, blog_title="FlaskBlog", # 博客标题 blog_sub_title="This is a blog system build by %s" % username, # 博客副标题 name="Admin", about="Feel free to add some comments here.") admin.set_password(password) db.session.add(admin) category = Category.query.first() # 创建文章分类 if category is None: click.echo("create the default category...") category = Category(name="默认") db.session.add(category) db.session.commit() click.echo("Done!")
def fake_categories(cnt=10): category = Category( name="Default" ) db.session.add(category) db.session.commit() # 添虚拟分类 for i in range(cnt): # 可能产生相同的分类名,而category表中的name字段定义为unique,从而数据库可能抛出异常 category_item = Category( name=fake.word() ) db.session.add(category_item) try: db.session.commit() except IntegrityError as e: db.session.rollback()
def add_category(): global crash if crash == 1: return '', 500 with counter.get_lock(): counter.value += 1 name = request.json if len(name[0]) == 0: return '', 400 new_category = Category(name[0]) new_category = Category(name[0]) exist_cat = Category.query.get(name[0]) if exist_cat is not None: return '', 400 db.session.add(new_category) db.session.commit() # onreturn category_schema.jsonify(new_category),201 return jsonify({}), 201
def new_cat(): global crash if crash == 1: return '', 500 with counter.get_lock(): counter.value += 1 cat = Category(request.form["name"]) db.session.add(cat) db.session.commit() return 200
def new_category(): form = CategoryForm() if form.validate_on_submit(): name = form.name.data category = Category(name=name) db.session.add(category) db.session.commit() flash('新建分类成功!', 'success') return redirect(url_for('.manage_category')) return render_template('admin/new_category.html', form=form)
def init_db(self): post = Post(title='Test Post', author='John Doe', description='Test Subtitle', content='## Title\nHello World.', slug='test-post') post.tags.append(Tag(text='testing')) post.category = Category(text='testing') db.session.add(post) db.session.commit()
def new_category(): form = CategoryForm() if form.validate_on_submit(): flash('Category Created!', 'success') category = Category(name=form.name.data, sequence=form.sequence.data) db.session.add(category) db.session.commit() return redirect(url_for('main.home')) return render_template('categories/create_category.html', title='New Category', form=form, legend='New Category')
def add_category(): data = request.get_json() db.session.add(Category(name=data)) db.session.commit() return jsonify('New Category Added...!')