From 27019279c5eab94b26b018c6037443f1e8694d04 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Sat, 17 Feb 2018 19:55:18 +0100 Subject: [PATCH] Refactored diff view to CBV. Added tests for the diff JSON response. --- src/wiki/urls.py | 8 ++--- src/wiki/views/article.py | 33 +++++++++++---------- testproject/testproject/db/prepopulated.db | Bin 375808 -> 375808 bytes tests/core/test_views.py | 23 +++++++++++++- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/wiki/urls.py b/src/wiki/urls.py index 45851267..fd4882fb 100644 --- a/src/wiki/urls.py +++ b/src/wiki/urls.py @@ -9,13 +9,13 @@ from wiki.views import accounts, article, deleted_list class WikiURLPatterns(object): - ''' + """ configurator for wiki urls. To customize, you can define your own subclass, either overriding the view providers, or overriding the functions that collect views. - ''' + """ # basic views article_view_class = article.ArticleView @@ -34,7 +34,7 @@ class WikiURLPatterns(object): revision_merge_view = staticmethod(article.merge) search_view_class = settings.SEARCH_VIEW - article_diff_view = staticmethod(article.diff) + article_diff_view_class = article.DiffView # account views signup_view_class = accounts.Signup @@ -74,7 +74,7 @@ class WikiURLPatterns(object): get_class_from_str(self.search_view_class).as_view(), name='search'), url(r'^_revision/diff/(?P[0-9]+)/$', - self.article_diff_view, + self.article_diff_view_class.as_view(), name='diff'), ] return urlpatterns diff --git a/src/wiki/views/article.py b/src/wiki/views/article.py index 8ad6ee29..9e287c27 100644 --- a/src/wiki/views/article.py +++ b/src/wiki/views/article.py @@ -12,6 +12,7 @@ from django.shortcuts import get_object_or_404, redirect, render from django.utils.decorators import method_decorator from django.utils.translation import ugettext as _ from django.utils.translation import ungettext +from django.views.generic import DetailView from django.views.generic.base import RedirectView, TemplateView, View from django.views.generic.edit import FormView from django.views.generic.list import ListView @@ -869,27 +870,29 @@ class Preview(ArticleMixin, TemplateView): return ArticleMixin.get_context_data(self, **kwargs) -def diff(request, revision_id, other_revision_id=None): +class DiffView(DetailView): + model = models.ArticleRevision + pk_url_kwarg = 'revision_id' - revision = get_object_or_404(models.ArticleRevision, id=revision_id) - - if not other_revision_id: + def render_to_response(self, context, **response_kwargs): + revision = self.get_object() other_revision = revision.previous_revision - baseText = other_revision.content if other_revision else "" - newText = revision.content - - differ = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK) - diff = differ.compare(baseText.splitlines(1), newText.splitlines(1)) + baseText = other_revision.content if other_revision is not None else "" + newText = revision.content - other_changes = [] + differ = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK) + diff = differ.compare( + baseText.splitlines(keepends=True), newText.splitlines(keepends=True) + ) + other_changes = [] - if not other_revision or other_revision.title != revision.title: - other_changes.append((_('New title'), revision.title)) + if not other_revision or other_revision.title != revision.title: + other_changes.append((_('New title'), revision.title)) - return object_to_json_response( - dict(diff=list(diff), other_changes=other_changes) - ) + return object_to_json_response( + {'diff': list(diff), 'other_changes': other_changes} + ) # TODO: Throw in a class-based view diff --git a/testproject/testproject/db/prepopulated.db b/testproject/testproject/db/prepopulated.db index 6ba0f27ec9cd394ca17ae6b4b805bab4228f07e8..c6629ccb3939765e4dfcedd835022a1d80a4dfcd 100644 GIT binary patch delta 805 zcmah{+iTNc6wj9|Oi*x$o(o3VQYFQnt1^+K5uL2#BCFGqGMAS>C;Qx{DbOSrFh>f-(IGKSnk&;Wc+L_F{f46kln6LI(?C0x`| zb_D`P9&3t5Xu3uuM-@tra=z^6TOgmzm0gd75u{-xF-cKcR;LMB%sOXH z)p-~CxvJ5PU*UabeEk`I1C^Swg7=NDdLa~rwiFP5L2>?f2`J89yFQV{;kqwebUsCs zJvrIb%o~j1){1t>=P*xX-o|{J@fSlf<5h;Wj2jrc8Pga#7#$g97%nk