def save(self, commit=True): article = super().save(commit=False) # Alter the owner according to the form field owner_username # TODO: Why not rename this field to 'owner' so this happens # automatically? article.owner = self.cleaned_data["owner_username"] # Revert any changes to group permissions if the # current user is not allowed (see __init__) # TODO: Write clean methods for this instead! if not self.can_change_groups: article.group = self.article.group article.group_read = self.article.group_read article.group_write = self.article.group_write if self.can_assign: if self.cleaned_data["recursive"]: article.set_permissions_recursive() if self.cleaned_data["recursive_owner"]: article.set_owner_recursive() if self.cleaned_data["recursive_group"]: article.set_group_recursive() if self.cleaned_data[ "locked"] and not article.current_revision.locked: revision = models.ArticleRevision() revision.inherit_predecessor(self.article) revision.set_from_request(self.request) revision.automatic_log = _("Article locked for editing") revision.locked = True self.article.add_revision(revision) elif not self.cleaned_data[ "locked"] and article.current_revision.locked: revision = models.ArticleRevision() revision.inherit_predecessor(self.article) revision.set_from_request(self.request) revision.automatic_log = _("Article unlocked for editing") revision.locked = False self.article.add_revision(revision) article.save()
def form_valid(self, form): """Create a new article revision when the edit form is valid (does not concern any sidebar forms!).""" revision = models.ArticleRevision() revision.inherit_predecessor(self.article) revision.title = form.cleaned_data["title"] revision.content = form.cleaned_data["content"] revision.user_message = form.cleaned_data["summary"] revision.deleted = False revision.set_from_request(self.request) self.article.add_revision(revision) messages.success( self.request, _("A new revision of the article was successfully added.")) return self.get_success_url()
def dispatch(self, request, article, *args, **kwargs): self.urlpath = kwargs.get("urlpath", None) self.article = article if self.urlpath: deleted_ancestor = self.urlpath.first_deleted_ancestor() if deleted_ancestor is None: # No one is deleted! return redirect("wiki:get", path=self.urlpath.path) elif deleted_ancestor != self.urlpath: # An ancestor was deleted, so redirect to that deleted page return redirect("wiki:deleted", path=deleted_ancestor.path) else: if not article.current_revision.deleted: return redirect("wiki:get", article_id=article.id) # Restore if request.GET.get("restore", False): can_restore = not article.current_revision.locked and article.can_delete( request.user) can_restore = can_restore or article.can_moderate(request.user) if can_restore: revision = models.ArticleRevision() revision.inherit_predecessor(self.article) revision.set_from_request(request) revision.deleted = False revision.automatic_log = _("Restoring article") self.article.add_revision(revision) messages.success( request, _('The article "%s" and its children are now restored.') % revision.title, ) if self.urlpath: return redirect("wiki:get", path=self.urlpath.path) else: return redirect("wiki:get", article_id=article.id) return super().dispatch1(request, article, *args, **kwargs)
def form_valid(self, form): cd = form.cleaned_data purge = cd["purge"] # If we are purging, only moderators can delete articles with children cannot_delete_children = False can_moderate = self.article.can_moderate(self.request.user) if purge and self.children_slice and not can_moderate: cannot_delete_children = True if self.cannot_delete_root or cannot_delete_children: messages.error( self.request, _("This article cannot be deleted because it has children or is a root article." ), ) return redirect("wiki:get", article_id=self.article.id) if can_moderate and purge: # First, remove children if self.urlpath: self.urlpath.delete_subtree() self.article.delete() messages.success( self.request, _("This article together with all its contents are now completely gone! Thanks!" ), ) else: revision = models.ArticleRevision() revision.inherit_predecessor(self.article) revision.set_from_request(self.request) revision.deleted = True self.article.add_revision(revision) messages.success( self.request, _('The article "%s" is now marked as deleted! Thanks for keeping the site free from unwanted material!' ) % revision.title, ) return self.get_success_url()
def get(self, request, article, revision_id, *args, **kwargs): revision = get_object_or_404(models.ArticleRevision, article=article, id=revision_id) current_text = (article.current_revision.content if article.current_revision else "") new_text = revision.content content = simple_merge(current_text, new_text) # Save new revision if not self.preview: old_revision = article.current_revision if revision.deleted: c = { "error_msg": _("You cannot merge with a deleted revision"), "article": article, "urlpath": self.urlpath, } return render(request, self.template_error_name, context=c) new_revision = models.ArticleRevision() new_revision.inherit_predecessor(article) new_revision.deleted = False new_revision.locked = False new_revision.title = article.current_revision.title new_revision.content = content new_revision.automatic_log = _( "Merge between revision #%(r1)d and revision #%(r2)d") % { "r1": revision.revision_number, "r2": old_revision.revision_number } article.add_revision(new_revision, save=True) old_revision.simpleplugin_set.all().update( article_revision=new_revision) revision.simpleplugin_set.all().update( article_revision=new_revision) messages.success( request, _("A new revision was created: Merge between revision #%(r1)d and revision #%(r2)d" ) % { "r1": revision.revision_number, "r2": old_revision.revision_number }, ) if self.urlpath: return redirect("wiki:edit", path=self.urlpath.path) else: return redirect("wiki:edit", article_id=article.id) c = { "article": article, "title": article.current_revision.title, "revision": None, "merge1": revision, "merge2": article.current_revision, "merge": True, "content": content, } return render(request, self.template_name, c)