~netlandish/django-wiki

023d0c42a76c54e50d49d1af956c41e2325f30a1 — Frank Loemker 6 years ago 95bfb05
Support caller provided content when editing an article.
2 files changed, 21 insertions(+), 11 deletions(-)

M src/wiki/forms.py
M src/wiki/views/article.py
M src/wiki/forms.py => src/wiki/forms.py +15 -9
@@ 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:

M src/wiki/views/article.py => src/wiki/views/article.py +6 -2
@@ 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(