def add_section(request, volume, page): volume_object = get_object_or_404(Volume, slug=volume) page_object = get_object_or_404(Page, volume=volume_object, slug=page) form = forms.NewSectionForm(request.POST) if form.is_valid(): position = request.POST.get("after", "first") new_section = Section(page=page_object, type=form.cleaned_data["type"]) new_section.save() # Get the order right. pk = new_section.pk order = page_object.get_section_order() order.remove(pk) if position == "first": # Put the new section at the beginning of the page. order = [pk] + order else: # Put the new section after an existing section. position = int(position) order.insert(order.index(position) + 1, pk) page_object.set_section_order(order) new_revision = Revision(section=new_section, author=request.user, body=form.cleaned_data["body"]) new_revision.save() return {"success": True, "section": new_section.pk} else: return {"errors": form.errors}
def modify_section(request, volume, page, section): volume_object = get_object_or_404(Volume, slug=volume) page_object = get_object_or_404(Page, volume=volume_object, slug=page) section_object = get_object_or_404(Section, pk=section) form = forms.NewSectionForm(request.POST) if form.is_valid(): section_object.modified = datetime.datetime.now() section_object.save() body = form.cleaned_data["body"].strip() # Check to make sure we're not saving duplicate content. last_revision = section_object.get_latest_revision() if last_revision.body == body: return {"success": True} new_revision = Revision(section=section_object, author=request.user, body=body) new_revision.save() return {"success": True} else: return {"errors": form.errors}