def edit(request, name): """Allows users to edit wiki pages.""" try: page = Page.objects.get(name=name) except Page.DoesNotExist: page = None if request.method == 'POST': form = PageForm(request.POST) if form.is_valid(): if not page: # it's a new page page = Page() page.name = form.cleaned_data['name'] page.user = request.user page.created_on = datetime.now() page.save() page_revision = PageRevision(content = form.cleaned_data['content'], edit_reason = form.cleaned_data['edit_reason'], user = request.user, revision_for = page) page_revision.save() page.current_revision = page_revision page.save() else: # it's an edit on an old page page.name = form.cleaned_data['name'] revision_count = PageRevision.objects.filter(revision_for = page).count() new_rev = revision_count + 1 page_revision = PageRevision(content = form.cleaned_data['content'], edit_reason = form.cleaned_data['edit_reason'], user = request.user, revision_for = page, revision_num = new_rev) page_revision.save() page.current_revision = page_revision page.modified_on = datetime.now() page.user = request.user page.save() return HttpResponseRedirect('../../%s/' % page.name) else: if page: page_dict = page.__dict__ # we have phased out page.content, # instead page will be a reference to current revision content. # but for the purposes of the form, we'll emulate it (ugly hack!) page_dict['content'] = page.current_revision.content form = PageForm(initial=page_dict) else: form = PageForm(initial={'name': name}) context = RequestContext(request) return render_to_response('wiki/edit.html', {'form': form}, context)
def post(self): current_user = users.get_current_user() if not current_user: return self.redirect('/') user = User.get_by_id(current_user.user_id()) if not user: return self.redirect('/register') try: page = Page(parent=user.key) page.user = user.key page.url = self.request.get('url') page.name = self.request.get('name') page.put() self.redirect('/') except db.Error: self.redirect('/')