From d45e63f49d32b0f726c80ae718a64791a45783da Mon Sep 17 00:00:00 2001 From: Raffaele Salmaso Date: Thu, 22 Feb 2018 07:39:03 +0100 Subject: [PATCH] Update article merge view to CBV --- src/wiki/urls.py | 12 ++-- src/wiki/views/article.py | 132 +++++++++++++++++++------------------- 2 files changed, 71 insertions(+), 73 deletions(-) diff --git a/src/wiki/urls.py b/src/wiki/urls.py index 65340337..008d2760 100644 --- a/src/wiki/urls.py +++ b/src/wiki/urls.py @@ -30,7 +30,7 @@ class WikiURLPatterns: article_source_view_class = article.Source article_plugin_view_class = article.Plugin revision_change_view_class = article.ChangeRevisionView - revision_merge_view = staticmethod(article.merge) + revision_merge_view_class = article.MergeView search_view_class = settings.SEARCH_VIEW article_diff_view_class = article.DiffView @@ -119,10 +119,8 @@ class WikiURLPatterns: name='preview_revision'), url( r'^_revision/merge/(?P[0-9]+)/(?P[0-9]+)/preview/$', - self.revision_merge_view, - name='merge_revision_preview', - kwargs={ - 'preview': True}), + self.revision_merge_view_class.as_view(preview=True), + name='merge_revision_preview'), ] return urlpatterns @@ -162,7 +160,7 @@ class WikiURLPatterns: name='change_revision'), url( r'^(?P[0-9]+)/revision/merge/(?P[0-9]+)/$', - self.revision_merge_view, + self.revision_merge_view_class.as_view(), name='merge_revision'), url(r'^(?P[0-9]+)/plugin/(?P\w+)/$', self.article_plugin_view_class.as_view(), @@ -212,7 +210,7 @@ class WikiURLPatterns: name='change_revision'), url( r'^(?P.+/|)_revision/merge/(?P[0-9]+)/$', - self.revision_merge_view, + self.revision_merge_view_class.as_view(), name='merge_revision'), url(r'^(?P.+/|)_plugin/(?P\w+)/$', self.article_plugin_view_class.as_view(), diff --git a/src/wiki/views/article.py b/src/wiki/views/article.py index 0534b2e0..c6b2df13 100644 --- a/src/wiki/views/article.py +++ b/src/wiki/views/article.py @@ -891,76 +891,76 @@ class DiffView(DetailView): {'diff': list(diff), 'other_changes': other_changes} ) -# TODO: Throw in a class-based view +class MergeView(View): + preview = False + template_name = "wiki/preview_inline.html" + template_error_name = "wiki/error.html" + urlpath = None -@get_article(can_write=True) -def merge( - request, - article, - revision_id, - urlpath=None, - template_file="wiki/preview_inline.html", - preview=False): - - 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 preview: - old_revision = article.current_revision + @method_decorator(get_article(can_write=True)) + def dispatch(self, request, article, revision_id, *args, **kwargs): + return super().dispatch(request, article, revision_id, *args, **kwargs) - if revision.deleted: - c = { - 'error_msg': _('You cannot merge with a deleted revision'), - 'article': article, - 'urlpath': urlpath - } - return render(request, "wiki/error.html", 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) + 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 urlpath: - return redirect('wiki:edit', path=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, template_file, c) + 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) class CreateRootView(FormView): -- 2.45.2