def add(): form = CategoryForm() if form.validate(): c = Category(form.category_name.data, form.description.data) db.session.add(c) db.session.commit() return redirect(url_for('category.index'))
def submit_category(self): storage = request.form form = CategoryForm(storage) is_valid = form.validate() is_new = int(storage['category_id']) < 0 if not is_valid: return render_template('forms/category.html', form=form, new=is_new) if is_new: # Adding a new category result = add_category(storage['category_html'], storage['category_human']) if result: message = 'Category successfully added!' else: message = 'Category failed to be added; please try again.' else: # Editing an existing category result = edit_category(storage['category_id'], storage['category_html'], storage['category_human']) if result: message = 'Category successfully edited!' else: message = 'Category failed to be edited; please try again.' if result: return render_template('confirmation_page.html', message=message) else: return render_template('error_page.html', error=message)
def new_category(): form = CategoryForm(request.form) if request.method == 'POST' and form.validate(): new = Categories(name=request.form['name'], user_id=login_session['user_id']) db_session.add(new) db_session.commit() flash('Category {} has been successfully created!'.format(new.name)) return redirect(url_for('home.index')) return render_template('/newcategory.html', form=form)
def edit(category_id): form = CategoryForm() if form.validate(): c = Category.query.filter(Category.category_name==category_id).first() sc = Category.query.filter(Category.category_name==form.category_name.data).count() if sc > 0: return redirect(url_for('category.index')) c.category_name = form.category_name.data c.description = form.description.data db.session.add(c) db.session.commit() return redirect(url_for('category.index'))
def edit_category(category_id): edit = db_session.query(Categories).filter_by(id=category_id).one() form = CategoryForm(request.form) if edit.user_id != login_session['user_id']: flash('Unauthorized to edit this category') return redirect( url_for('category_owner.showCategory', category_id=category_id)) if request.method == 'POST' and form.validate(): edit.name = request.form['name'] db_session.add(edit) db_session.commit() flash('Category {} has been successfully edited!'.format(edit.name)) return redirect( url_for('category_owner.show_categories', category_id=category_id)) else: return render_template('/editcategory.html', category=edit, form=form)