~netlandish/django-wiki

27019279c5eab94b26b018c6037443f1e8694d04 — Mads Jensen 6 years ago 7c37122
Refactored diff view to CBV.

Added tests for the diff JSON response.
M src/wiki/urls.py => src/wiki/urls.py +4 -4
@@ 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<revision_id>[0-9]+)/$',
                self.article_diff_view,
                self.article_diff_view_class.as_view(),
                name='diff'),
        ]
        return urlpatterns

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


M testproject/testproject/db/prepopulated.db => testproject/testproject/db/prepopulated.db +0 -0
M tests/core/test_views.py => tests/core/test_views.py +22 -1
@@ 3,12 3,13 @@ from __future__ import print_function, unicode_literals
import pprint

from django.contrib.auth import authenticate
from django.http import JsonResponse
from django.shortcuts import resolve_url
from django.utils.html import escape
from django_functest import FuncBaseMixin
from wiki import models
from wiki.forms import validate_slug_numbers
from wiki.models import URLPath
from wiki.models import URLPath, ArticleRevision, reverse

from ..base import (ArticleWebTestUtils, DjangoClientTestBase,
                    RequireRootArticleMixin, SeleniumBase, WebTestBase)


@@ 383,6 384,26 @@ class EditViewTest(RequireRootArticleMixin, ArticleWebTestUtils, DjangoClientTes
        )


class DiffViewTests(RequireRootArticleMixin, DjangoClientTestBase):

    def setUp(self):
        super().setUp()
        self.root_article.add_revision(ArticleRevision(
            title='New Revision'), save=True
        )
        self.new_revision = self.root_article.current_revision

    def test_diff(self):
        response = self.client.get(reverse('wiki:diff', kwargs={'revision_id': self.root_article.pk}))
        diff = {
            "diff": ["+ root article content"],
            "other_changes": [["New title", "Root Article"]]
        }
        self.assertJSONEqual(str(response.content, encoding='utf8'), diff)
        self.assertIsInstance(response, JsonResponse)
        self.assertEqual(response.status_code, 200)


class EditViewTestsBase(RequireRootArticleMixin, FuncBaseMixin):
    def test_edit_save(self):
        old_revision = URLPath.root().article.current_revision