def add_category(request, category_id="", template_name="manage/category/add_category.html"): """Provides an add form and adds a new category to category with given id. """ if category_id == "": parent = None else: try: parent = Category.objects.get(pk=category_id) except ObjectDoesNotExist: parent = None if request.method == "POST": form = CategoryAddForm(data=request.POST) if form.is_valid(): new_category = form.save(commit=False) new_category.parent = parent new_category.position = 999 if parent: new_category.level = parent.level + 1 new_category.save() # Update positions manage_utils.update_category_positions(parent) url = reverse("muecke_manage_category", kwargs={"category_id": new_category.id}) return HttpResponseRedirect(url) else: form = CategoryAddForm(initial={"parent": category_id}) return render_to_response(template_name, RequestContext(request, { "category": parent, "form": form, "came_from": request.REQUEST.get("came_from", reverse("muecke_manage_categories")), }))
def delete_category(request, id): """Deletes category with given id. """ category = muecke_get_object_or_404(Category, pk=id) parent = category.parent category.delete() manage_utils.update_category_positions(parent) url = reverse("muecke_manage_categories") return HttpResponseRedirect(url)