def newCategory(): params = request.form # ensure name key and name value if 'name' in params.keys(): name = params['name'] category = Category(name=name, type=name, depth=0) else: return json.dumps({'error': 'missing name parameter'}), 422 try: # attach category to user if current_user.is_authenticated: category.user_id = current_user.id # add new category to the database session.add(category) session.commit() return json.dumps(category.serialize) except: session.rollback() return json.dumps({'error': 'failed to create a category'}), 400
def fake_categoies(count=10): with db.auto_commit(): category = Category() category.name = 'Default' db.session.add(category) for i in range(count): with db.auto_commit(): category = Category(name=fake.word()) db.session.add(category)
def add_category(): name = request.json['category'] if category_exists_name(name): return jsonify({'error': 'this category already exists in database'}) else: category = Category(name) db.session.add(Category(name)) db.session.commit() return jsonify({'response': category.serialized})
def new_category(): form = CategoryForm() if form.validate_on_submit(): name = form.name.data category = 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 question_new(): question = Question() form = QuestionForm() if form.validate_on_submit(): question.title = form.title.data # Look for existing Category category = Category.query.filter_by(name=form.category.data).first() if not category: category = Category(name=form.category.data) question.category = category try: db.session.add(question) db.session.commit() return redirect(url_for('question_edit', question_id=question.id)) except IntegrityError as e: db.session.rollback() reason = e.message flash(reason) return render_template('game/new_question.html', form=form)
def category(): title = 'Admin Category' if request.method == 'POST': cat = request.form cat_name = cat['cat_name'] cat_url = cat['cat_url'] cat_date = datetime.now() if cat['cat_id'] != '': # this process edits a selected category data_id = cat['cat_id'] cat = Category.query.filter_by(cat_id=data_id).one() cat.cat_name = cat_name cat.cat_url = cat_url cat.date_added = cat_date message = 'Category was updated successfully' else: # this process adds new category cat = Category(cat_name, cat_url, cat_date) db.session.add(cat) message = 'Category was added successfully' db.session.commit() flash(message, "success") return redirect(request.url) # this process retrieves data from database and displays it to the user data = Category.query.all() return render_template('admin/category.html', categories=data, title=title)
def update_category(): authentication("Admin") if check_request_data("id", "turkish_name", "english_name"): category = Category(turkish_name=request.get_json()["turkish_name"], english_name=request.get_json()["english_name"], id=request.get_json()["id"]) return model_to_json(category_repository.update(category))
def category_add(): """ represents add category processing url """ name = request.form.get("name") try: category = Category(name) category.save() except IntegrityError: flash("error", "kategori yang anda masukan duplicated") except Exception as Error: flash("error", Error.__str__()) else: flash("success", "sukses menambah kategori") return redirect(url_for("dashboard.category"))
def _make_category(self): # self.categories.append(Category(name='全部')) categories_names = set() while len(categories_names) < self.category_num: categories_names.add(self.blog_fake.word()) for name in categories_names: new_category = Category(name=name) self.categories.append(new_category) db.session.add(new_category)
def admin_add_category(): if request.method == 'GET': return render_template('admin/add_category.html') else: from app.model import Category from app import db name = request.form['name'] db.session.add(Category(name=name)) db.session.commit() return json.dumps({'success': True})
async def create_category(request: Request): form = CategoryForm(await request.post()) if request.method == 'POST' and form.validate(): category = Category( name=form.name.data, allowed_roles=form.allowed_roles.data, ) await category_dao.create(request.app, category) raise HTTPFound('/admin/categories') return {'form': form}
async def get_all(app, limit, offset): async with app['pool'].acquire() as conn: async with conn.transaction(): stmt = await conn.prepare(''' select id, name, allowed_roles from "category" order by id limit $1 offset $2 ''') results = await stmt.fetch(limit, offset) return [Category(**dict(result.items())) for result in results]
async def get_by_roles(app, roles): async with app['pool'].acquire() as conn: async with conn.transaction(): stmt = await conn.prepare(''' select id, name, allowed_roles from "category" where allowed_roles && $1 ''') results = await stmt.fetch(roles) return [Category(**dict(result.items())) for result in results]
async def get(app, id_): async with app['pool'].acquire() as conn: async with conn.transaction(): stmt = await conn.prepare(''' select id, name, allowed_roles from "category" where id = $1 ''') result = await stmt.fetchrow(id_) return Category(**dict(result.items()))
async def edit_category(request: Request): category_id = request.match_info['id'] category = await category_dao.get(request.app, int(category_id)) if not category: raise HTTPNotFound form = CategoryForm(await request.post(), obj=category) if request.method == 'POST' and form.validate(): category = Category( name=form.name.data, allowed_roles=form.allowed_roles.data, ) await category_dao.update(request.app, category) raise HTTPFound('/admin/categories') return {'form': form}
def create_user(username, password): if not all([username, password]): print('参数不足') admin = Admin.query.first() if admin: admin.username = username admin.password = password else: admin = Admin() admin.username = username admin.password = password admin.blog_title = '我的博客' admin.blog_sub_title = '博客子标题' admin.about = '这什么都没有' db.session.add(admin) categroy = Category.query.first() if categroy is None: print('创建初始分类') categroy = Category(name='默认') with db.auto_commit(): db.session.add(categroy) db.session.commit()
def post(self): c = json.loads(request.form.get('category')) imgurl = json.loads(request.form.get('img_url')) # 通知 name = request.form.get('name') af = Applyform.from_request(request) send_email('*****@*****.**', '新的入驻申请', body=name) # 修改状态 current_user.applystatus = APPLYSTATUS['CHECKING'] session['applystatus'] = current_user.applystatus db.session.add(current_user) db.session.add(af) db.session.commit() for t in c: c = Category(category_name=t,apply_id=af.id) db.session.add(c) for w in imgurl: aw = Applywork(work_url=w,apply_id=af.id) db.session.add(aw) db.session.commit() current_app.logger.info('新入驻设计师:%s' % af.name) return jsonify({'code':0})
def question_edit(question_id): question = Question.query.get(question_id) if not question: abort(404) form = QuestionForm() slide_form = SlideForm() if form.validate_on_submit(): question.title = form.title.data # Look for existing Category category = Category.query.filter_by(name=form.category.data).first() if not category: category = Category(name=form.category.data) question.category = category try: db.session.commit() return redirect(url_for('question_edit', question_id=question.id)) except IntegrityError as e: db.session.rollback() reason = e.message flash(reason) if request.method == 'GET': form.title.data = question.title form.category.data = question.category.name return render_template('game/edit_question.html', form=form, question=question, slide_form=slide_form)
def test_insert_data(test_client): ##New user db.session.add( User(username="******", email="*****@*****.**", password=bcrypt.generate_password_hash("test").decode('utf-8'))) db.session.add( User(username="******", email="*****@*****.**", password=bcrypt.generate_password_hash("test").decode('utf-8'))) db.session.add( User(username="******", email="*****@*****.**", password=bcrypt.generate_password_hash("test").decode('utf-8'))) ##New Business db.session.add( Business(owner=User.query.get(1), name="Business1", description="Some description")) db.session.add( Business(owner=User.query.get(2), name="Business2", description="Some description")) db.session.add( Business(owner=User.query.get(3), name="Business3", description="Some description")) ##Add location db.session.add( Location(business=Business.query.get(1), county='Kiambu', region='Lari', location='Some location')) db.session.add( Location(business=Business.query.get(2), county='Nairobi', region='Ruiru', location='Membley')) db.session.add( Location(business=Business.query.get(3), county='Kajiado', region='Some region', location='Some location')) ##Add Category db.session.add( Category(business=Business.query.get(1), category='Some category')) db.session.add( Category(business=Business.query.get(2), category='Some category')) db.session.add( Category(business=Business.query.get(3), category='Some category')) ##Add review db.session.add( Review(business=Business.query.get(1), email='*****@*****.**', message='Some message')) db.session.add( Review(business=Business.query.get(2), email='*****@*****.**', message='Some message')) db.session.add( Review(business=Business.query.get(3), email='*****@*****.**', message='Some message')) db.session.commit()
def add_category(): authentication("Admin") if check_request_data("turkish_name", "english_name"): category = Category(turkish_name=request.get_json()["turkish_name"], english_name=request.get_json()["english_name"]) return model_to_json(category_repository.add(category)), 201