From 023d0c42a76c54e50d49d1af956c41e2325f30a1 Mon Sep 17 00:00:00 2001 From: Frank Loemker Date: Sun, 14 Jan 2018 22:54:25 -0500 Subject: [PATCH] Support caller provided content when editing an article. --- src/wiki/forms.py | 24 +++++++++++++++--------- src/wiki/views/article.py | 8 ++++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/wiki/forms.py b/src/wiki/forms.py index 0ef95fc1..9c0177f5 100644 --- a/src/wiki/forms.py +++ b/src/wiki/forms.py @@ -234,7 +234,14 @@ class EditForm(forms.Form, SpamProtectionMixin): self.initial_revision = current_revision self.presumed_revision = None if current_revision: - initial = {'content': current_revision.content, + # For e.g. editing a section of the text: The content provided by the caller is used. + # Otherwise use the content of the revision. + provided_content = True + content = kwargs.pop('content', None) + if content is None: + provided_content = False + content = current_revision.content + initial = {'content': content, 'title': current_revision.title, 'current_revision': current_revision.id} initial.update(kwargs.get('initial', {})) @@ -249,18 +256,17 @@ class EditForm(forms.Form, SpamProtectionMixin): data = kwargs.get('data', None) if data: self.presumed_revision = data.get('current_revision', None) - if not str( - self.presumed_revision) == str( - self.initial_revision.id): + if not str(self.presumed_revision) == str(self.initial_revision.id): newdata = {} for k, v in data.items(): newdata[k] = v newdata['current_revision'] = self.initial_revision.id - newdata['content'] = simple_merge( - self.initial_revision.content, - data.get( - 'content', - "")) + # Don't merge if content comes from the caller + if provided_content: + self.presumed_revision = self.initial_revision.id + else: + newdata['content'] = simple_merge( + content, data.get('content', "")) newdata['title'] = current_revision.title kwargs['data'] = newdata else: diff --git a/src/wiki/views/article.py b/src/wiki/views/article.py index 3ead25e3..e63e2faa 100644 --- a/src/wiki/views/article.py +++ b/src/wiki/views/article.py @@ -225,6 +225,7 @@ class Edit(ArticleMixin, FormView): @method_decorator(get_article(can_write=True, not_locked=True)) def dispatch(self, request, article, *args, **kwargs): + self.orig_content = kwargs.pop('content', None) self.sidebar_plugins = plugin_registry.get_sidebar() self.sidebar = [] return super().dispatch(request, article, *args, **kwargs) @@ -253,6 +254,7 @@ class Edit(ArticleMixin, FormView): kwargs['data'] = None kwargs['files'] = None kwargs['no_clean'] = True + kwargs['content'] = self.orig_content return form_class(self.request, self.article.current_revision, **kwargs) def get_sidebar_form_classes(self): @@ -301,8 +303,10 @@ class Edit(ArticleMixin, FormView): title = form.cleaned_data['unsaved_article_title'] content = form.cleaned_data['unsaved_article_content'] - - if title != self.article.current_revision.title or content != self.article.current_revision.content: + orig_content = self.orig_content + if not orig_content: + orig_content = self.article.current_revision.content + if title != self.article.current_revision.title or content != orig_content: request.session['unsaved_article_title_%d' % self.article.id] = title request.session['unsaved_article_content_%d' % self.article.id] = content messages.warning( -- 2.45.2