def wiki_page_revert(request, slug, revision_pk): slug_original = slug slug = urlize_title(slug) if slug != slug_original: return HttpResponseRedirect( reverse('wiki_page_revert', args=[slug, revision_pk]) ) try: wiki_pages = WikiPage.objects.published(for_user=request.user) wiki_page = wiki_pages.get(slug=slug) except WikiPage.DoesNotExist: return HttpResponseRedirect(reverse('wiki_page_edit', args=[slug])) src_revision = get_object_or_404(WikiPageRevision, page=wiki_page, pk=revision_pk) new_revision = WikiPageRevision(page=wiki_page, user=request.user if request.user.is_authenticated() else User.objects.get(id=-1)) if request.method == 'POST': form = WikiPageForm(data=request.POST or None, instance=wiki_page) if form.is_valid(): form.save() new_revision.content = form.cleaned_data["content"] new_revision.description = form.cleaned_data["summary"] new_revision.save() return HttpResponseRedirect(reverse('wiki_page_detail', kwargs={'slug': slug})) else: if src_revision.user: description = _("Reverted to revision of %(time)s by %(user)s.") % \ {'time': src_revision.created, 'user': src_revision.user.username} else: description = _("Reverted to anonymous revision of %(time)s.") % \ {'time': src_revision.created} form = WikiPageForm(data=request.POST or None, instance=wiki_page, initial={'content': src_revision.content, 'summary': description}) return render(request, 'mezawiki/wiki_page_edit.html', {'wiki_page': wiki_page, 'form': form, 'src_revision': src_revision})
def wiki_page_edit(request, slug, template="mezawiki/wiki_page_edit.html"): """ Displays the form for editing and deleting a page. Custom templates are checked for using the name ``mezawiki/wiki_page_edit_XXX.html`` where ``XXX`` is the wiki pages's slug. """ try: wiki_pages = WikiPage.objects.published(for_user=request.user) wiki_page = wiki_pages.get(slug=slug) initial = {} except WikiPage.DoesNotExist: wiki_page = WikiPage(slug=slug) wiki_page.is_initial = True initial = {"content": _("Describe your new page %s here..." % slug)} #'message': _('Initial revision')} if request.method == "POST": form = WikiPageForm(request.POST, instance=wiki_page) if form.is_valid(): page = form.save(commit=False) page.user = request.user page.title = slug page.save() return HttpResponseRedirect(reverse("wiki_page_detail", args=[slug])) else: form = WikiPageForm(initial=initial, instance=wiki_page) context = {"wiki_page": wiki_page, "form": form} templates = [u"mezawiki/wiki_page_edit_%s.html" % unicode(slug), template] return render(request, templates, context)
def wiki_page_undo(request, slug, revision_pk): slug_original = slug slug = urlize_title(slug) if slug != slug_original: return HttpResponseRedirect( reverse('wiki_page_undo', args=[slug, revision_pk]) ) try: wiki_pages = WikiPage.objects.published(for_user=request.user) wiki_page = wiki_pages.get(slug=slug) except WikiPage.DoesNotExist: return HttpResponseRedirect(reverse('wiki_page_edit', args=[slug])) src_revision = get_object_or_404(WikiPageRevision, page=wiki_page, pk=revision_pk) new_revision = WikiPageRevision(page=wiki_page, user=request.user if request.user.is_authenticated() else User.objects.get(id=-1)) if request.method == 'POST': form = WikiPageForm(data=request.POST or None, instance=wiki_page) if form.is_valid(): form.save() new_revision.content = form.cleaned_data["content"] new_revision.description = form.cleaned_data["summary"] new_revision.save() return HttpResponseRedirect(reverse('wiki_page_detail', kwargs={'slug': slug})) else: if src_revision.user: description = _("Undid revision of %(time)s by %(user)s.") % \ {'time': src_revision.created, 'user': src_revision.user.username} else: description = _("Undid anonymous revision of %(time)s.") % {'time': src_revision.created} prev_revision = None try: prev_revision = WikiPageRevision.objects\ .filter(page=wiki_page, created__lt=src_revision.created)\ .order_by('-created')[0] prev_content = prev_revision.content except IndexError: prev_content = '' dmp = diff_match_patch() rdiff = dmp.patch_make(src_revision.content, prev_content) content, results = dmp.patch_apply(rdiff, wiki_page.content) if False in results: urldata = {'to_revision_pk': src_revision.pk} if prev_revision: urldata['from_revision_pk'] = prev_revision.pk urldata['undo'] = 'error' return HttpResponseRedirect("%s?%s" % ( reverse('wiki_page_diff', kwargs={'slug': slug}), urlencode(urldata))) form = WikiPageForm(data=request.POST or None, instance=wiki_page, initial={'content': content, 'summary': description}) return render(request, 'mezawiki/wiki_page_edit.html', {'wiki_page': wiki_page, 'form': form})
def wiki_page_edit(request, slug, template="mezawiki/wiki_page_edit.html"): """ Displays the form for editing a page. """ try: #wiki_pages = WikiPage.objects.published(for_user=request.user) wiki_page = WikiPage.objects.get(slug=slug) wiki_page.is_initial = False initial = {} except WikiPage.DoesNotExist: wiki_page = WikiPage(slug=slug) wiki_page.is_initial = True initial = {'status': 1} if not wiki_page.can_edit_wikipage(request.user): return HttpResponseForbidden( _("You don't have permission to edit this wiki page.")) if request.method == 'POST': form = WikiPageForm(request.POST, instance=wiki_page) if form.is_valid(): page = form.save(commit=False) if wiki_page.is_initial: try: page.user = request.user except: page.user_id = -1 page.title = deurlize_title(slug) page.save() if 'content' in form.changed_data: revision = WikiPageRevision() revision.content = page.content revision.description = form.cleaned_data["summary"] revision.page = page try: revision.user = request.user except: # anonymous revision.user_id = -1 revision.save() return HttpResponseRedirect( reverse('wiki_page_detail', args=[slug])) else: form = WikiPageForm(initial=initial, instance=wiki_page) context = {'wiki_page': wiki_page, 'form': form, 'title': deurlize_title(slug)} return render(request, template, context)
def wiki_page_new(request, template="mezawiki/wiki_page_new.html"): """ Displays the form for creating a page. """ if not can_add_wikipage(request.user): return HttpResponseForbidden( _("You don't have permission to create wiki page.")) if request.method == 'POST': form = WikiPageForm(request.POST) if form.is_valid(): page = form.save(commit=False) try: page.user = request.user except: # anonymous page.user_id = -1 page.slug = urlize_title(form.cleaned_data["title"]) # TODO Check slug, it is not a unique field page.save() revision = WikiPageRevision() revision.content = page.content revision.description = form.cleaned_data["summary"] revision.page = page try: revision.user = request.user except: # anonymous revision.user_id = -1 revision.save() return HttpResponseRedirect( reverse('wiki_page_detail', args=[page.slug])) else: form = WikiPageForm(initial={'status': 1}) context = {'form': form} return render(request, template, context)
def get(self, request, slug, revision_pk, template='mezawiki/wiki_page_edit.html'): slug_original = slug slug = urlize_title(slug) if slug != slug_original: return HttpResponseRedirect( reverse('wiki_page_revert', args=[slug, revision_pk])) try: wiki_pages = WikiPage.objects.published(for_user=request.user) wiki_page = wiki_pages.get(slug=slug) except WikiPage.DoesNotExist: return HttpResponseRedirect(reverse('wiki_page_edit', args=[slug])) src_revision = get_object_or_404(WikiPageRevision, page=wiki_page, pk=revision_pk) new_revision = WikiPageRevision( page=wiki_page, user=request.user if request.user.is_authenticated() else User.objects.get(id=-1)) if request.method == 'POST': form = WikiPageForm(data=request.POST or None, instance=wiki_page) if form.is_valid(): form.save() new_revision.content = form.cleaned_data["content"] new_revision.description = form.cleaned_data["summary"] new_revision.save() return HttpResponseRedirect( reverse('wiki_page_detail', kwargs={'slug': slug})) else: if src_revision.user: description = _("Reverted to revision of %(time)s by %(user)s.") % \ {'time': src_revision.created, 'user': src_revision.user.username} else: description = _("Reverted to anonymous revision of %(time)s.") % \ {'time': src_revision.created} form = WikiPageForm(data=request.POST or None, instance=wiki_page, initial={ 'content': src_revision.content, 'summary': description }) context = { 'wiki_page': wiki_page, 'form': form, 'src_revision': src_revision } return render(request, template, context)
def wiki_page_edit(request, slug, template="mezawiki/wiki_page_edit.html"): """ Displays the form for editing a page. """ try: #wiki_pages = WikiPage.objects.published(for_user=request.user) wiki_page = WikiPage.objects.get(slug=slug) wiki_page.is_initial = False initial = {} except WikiPage.DoesNotExist: wiki_page = WikiPage(slug=slug) wiki_page.is_initial = True initial = {'status': 1} if not wiki_page.can_edit_wikipage(request.user): return HttpResponseForbidden( _("You don't have permission to edit this wiki page.")) if request.method == 'POST': form = WikiPageForm(request.POST, instance=wiki_page) if form.is_valid(): page = form.save(commit=False) if wiki_page.is_initial: try: page.user = request.user except: page.user_id = -1 page.title = deurlize_title(slug) page.save() if 'content' in form.changed_data: revision = WikiPageRevision() revision.content = page.content revision.description = form.cleaned_data["summary"] revision.page = page try: revision.user = request.user except: # anonymous revision.user_id = -1 revision.save() return HttpResponseRedirect( reverse('wiki_page_detail', args=[slug])) else: form = WikiPageForm(initial=initial, instance=wiki_page) context = { 'wiki_page': wiki_page, 'form': form, 'title': deurlize_title(slug) } return render(request, template, context)
def get(self, request, slug, template="mezawiki/wiki_page_edit.html"): """ Displays the form for editing a page. """ try: #wiki_pages = WikiPage.objects.published(for_user=request.user) wiki_page = WikiPage.objects.get(slug=slug) wiki_page.is_initial = False initial = {} except WikiPage.DoesNotExist: wiki_page = WikiPage(slug=slug) wiki_page.is_initial = True initial = {'status': 1} form = WikiPageForm(request.POST, instance=wiki_page) if not wiki_page.can_edit_wikipage(request.user): return HttpResponseForbidden( _("You don't have permission to edit this wiki page.")) context = { 'wiki_page': wiki_page, 'form': form, 'title': deurlize_title(slug) } return render(request, template, context)
def post(self, request, slug, template="mezawiki/wiki_page_edit.html"): try: #wiki_pages = WikiPage.objects.published(for_user=request.user) wiki_page = WikiPage.objects.get(slug=slug) wiki_page.is_initial = False initial = {} except WikiPage.DoesNotExist: wiki_page = WikiPage(slug=slug) wiki_page.is_initial = True initial = {'status': 1} form = WikiPageForm(request.POST, instance=wiki_page) if form.is_valid(): page = form.save(commit=False) if wiki_page.is_initial: try: page.user = request.user except: page.user_id = -1 page.title = deurlize_title(slug) page.save() if 'content' in form.changed_data: revision = WikiPageRevision() revision.content = page.content revision.description = form.cleaned_data["summary"] revision.page = page try: revision.user = request.user except: # anonymous revision.user_id = -1 revision.save() return HttpResponseRedirect( reverse('wiki_page_detail', args=[slug])) else: form = WikiPageForm(initial=initial, instance=wiki_page) context = { 'wiki_page': wiki_page, 'form': form, 'title': deurlize_title(slug) } return render(request, template, context)
def wiki_page_undo(request, slug, revision_pk): slug_original = slug slug = urlize_title(slug) if slug != slug_original: return HttpResponseRedirect( reverse('wiki_page_undo', args=[slug, revision_pk])) try: wiki_pages = WikiPage.objects.published(for_user=request.user) wiki_page = wiki_pages.get(slug=slug) except WikiPage.DoesNotExist: return HttpResponseRedirect(reverse('wiki_page_edit', args=[slug])) src_revision = get_object_or_404(WikiPageRevision, page=wiki_page, pk=revision_pk) new_revision = WikiPageRevision( page=wiki_page, user=request.user if request.user.is_authenticated() else User.objects.get(id=-1)) if request.method == 'POST': form = WikiPageForm(data=request.POST or None, instance=wiki_page) if form.is_valid(): form.save() new_revision.content = form.cleaned_data["content"] new_revision.description = form.cleaned_data["summary"] new_revision.save() return HttpResponseRedirect( reverse('wiki_page_detail', kwargs={'slug': slug})) else: if src_revision.user: description = _("Undid revision of %(time)s by %(user)s.") % \ {'time': src_revision.created, 'user': src_revision.user.username} else: description = _("Undid anonymous revision of %(time)s.") % { 'time': src_revision.created } prev_revision = None try: prev_revision = WikiPageRevision.objects\ .filter(page=wiki_page, created__lt=src_revision.created)\ .order_by('-created')[0] prev_content = prev_revision.content except IndexError: prev_content = '' dmp = diff_match_patch() rdiff = dmp.patch_make(src_revision.content, prev_content) content, results = dmp.patch_apply(rdiff, wiki_page.content) if False in results: urldata = {'to_revision_pk': src_revision.pk} if prev_revision: urldata['from_revision_pk'] = prev_revision.pk urldata['undo'] = 'error' return HttpResponseRedirect("%s?%s" % (reverse( 'wiki_page_diff', kwargs={'slug': slug}), urlencode(urldata))) form = WikiPageForm(data=request.POST or None, instance=wiki_page, initial={ 'content': content, 'summary': description }) return render(request, 'mezawiki/wiki_page_edit.html', { 'wiki_page': wiki_page, 'form': form })