def process_form(category_id=None): form = CategoryForm() cat_contr = CategoryController(current_user.id) if not form.validate(): return render_template('edit_category.html', form=form) existing_cats = list(cat_contr.read(name=form.name.data)) if existing_cats and category_id is None: flash(gettext("Couldn't add category: already exists."), "warning") return redirect( url_for('category.form', category_id=existing_cats[0].id)) # Edit an existing category category_attr = {'name': form.name.data} if category_id is not None: cat_contr.update({'id': category_id}, category_attr) flash( gettext('Category %(cat_name)r successfully updated.', cat_name=category_attr['name']), 'success') return redirect(url_for('category.form', category_id=category_id)) # Create a new category new_category = cat_contr.create(**category_attr) flash( gettext('Category %(category_name)r successfully created.', category_name=new_category.name), 'success') return redirect(url_for('category.form', category_id=new_category.id))
def form(category_id=None): action = gettext("Add a category") head_titles = [action] if category_id is None: return render_template('edit_category.html', action=action, head_titles=head_titles, form=CategoryForm()) category = CategoryController(current_user.id).get(id=category_id) action = gettext('Edit category') head_titles = [action] if category.name: head_titles.append(category.name) return render_template('edit_category.html', action=action, head_titles=head_titles, category=category, form=CategoryForm(obj=category))
def category_details(request, idk=None): if idk: category = get_object_or_404(Category, pk=idk) category_form = CategoryForm(request.POST or None, request.FILES or None, instance=category) else: category = Category() category_form = CategoryForm() if request.POST: category_form = CategoryForm(instance=category, data=request.POST, files=request.FILES) if category_form.is_valid(): category_form.save() return redirect('category_list') return render(request, 'category/category_details.html', {'category_form': category_form})
def categoryList(request): category_form = CategoryForm() all_categories = Category.objects.all() if request.is_ajax(): cmd = request.POST.get('cmd', '') if cmd == 'delete_category': category = get_object_or_404(Category, pk=request.POST.get('idk', '')) category.delete() return render(request, 'category/category_results.html', {'category_list': all_categories}) return render(request, 'category/category_list.html', { 'category_form': category_form, 'category_list': all_categories })
def attributes(category_id=None, attribute_id=None): if attribute_id: attribute = Attribute.query.filter(Attribute.id==attribute_id).first() form = AttributeForm(obj=attribute) return render_template('admin/values.html', attribute=attribute, form=form) if category_id: category = Category.query.filter(Category.id==category_id).first() form = CategoryForm(obj=category) return render_template('admin/attributes.html', category=category, form=form) categories = Category.query.filter(Category.parent_id!=None) return render_template('admin/categories.html', categories=categories)