Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4144 from safwanrahman/compare
Browse files Browse the repository at this point in the history
[Bug 1269104] Compare a new translation to the English document
  • Loading branch information
jwhitlock committed Apr 11, 2017
2 parents 210a04a + 11ffe71 commit a5bee8e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 62 deletions.
3 changes: 3 additions & 0 deletions kuma/wiki/jinja2/wiki/list/revisions.html
Expand Up @@ -63,6 +63,9 @@ <h1>{{ _('History of') }} <a href="{{ document.get_absolute_url() }}">{{ documen
<a href="{{ rev.creator.get_absolute_url() }}">{{ rev.creator }}</a>
</div>
<div class="revision-list-cell revision-list-comment">
{% if rev.document.locale != document.locale %}
<em>{{ get_locale_localized(rev.document.locale) }}</em>
{% endif %}
{{ format_comment(rev) }}
</div>
{% if document.current_revision.id != rev.id and user_can_delete %}
Expand Down
44 changes: 0 additions & 44 deletions kuma/wiki/queries.py
@@ -1,50 +1,6 @@
from django.db import models


class MultiQuerySet(object):
def __init__(self, *args, **kwargs):
self.querysets = args
self._count = None

def _clone(self):
querysets = [qs._clone() for qs in self.querysets]
return MultiQuerySet(*querysets)

def __repr__(self):
return repr(list(self.querysets))

def count(self):
if not self._count:
self._count = sum([qs.count() for qs in self.querysets])
return self._count

def __len__(self):
return self.count()

def exists(self):
return self.count() > 0

def __iter__(self):
for qs in self.querysets:
for item in qs.all():
yield item

def __getitem__(self, item):
offset, stop, step = item.indices(self.count())
items = []
total_len = stop - offset
for qs in self.querysets:
if len(qs) < offset:
offset -= len(qs)
else:
items += list(qs[offset:offset + total_len - len(items)])
if len(items) >= total_len:
return items
else:
offset = 0
continue


class TransformQuerySet(models.query.QuerySet):
def __init__(self, *args, **kwargs):
super(TransformQuerySet, self).__init__(*args, **kwargs)
Expand Down
18 changes: 18 additions & 0 deletions kuma/wiki/tests/test_views.py
Expand Up @@ -215,6 +215,24 @@ def test_history_view(self):
resp = self.client.get(all_url)
eq_(200, resp.status_code)

def test_translation_history_has_based_on_revision_to_compare(self):
"""In the history of a translation, there should be based on revision
So that its possible to compare a new translation with its based on"""

eng_rev = revision(is_approved=True, save=True)
trans_doc = document(locale='bn-BD', parent=eng_rev.document, save=True)
revision(document=trans_doc, based_on=eng_rev, save=True)

url = reverse('wiki.document_revisions', args=(trans_doc.slug,), locale=trans_doc.locale)
resp = self.client.get(url)
eq_(200, resp.status_code)
data = pq(resp.content)
list_content = data('.revision-list-contain').find('li')
# Check there are 2 revision in the list
eq_(2, len(list_content))
# Check parent_revision id is below to compare from
eq_(str(eng_rev.id), list_content.find('input[name=from]')[1].attrib['value'])

def test_toc_view(self):
slug = 'toc_test_doc'
html = '<h2>Head 2</h2><h3>Head 3</h3>'
Expand Down
35 changes: 18 additions & 17 deletions kuma/wiki/views/list.py
Expand Up @@ -8,9 +8,8 @@

from ..constants import DOCUMENTS_PER_PAGE
from ..decorators import process_document_path, prevent_indexing
from ..models import (Document, DocumentTag, Revision, ReviewTag,
from ..models import (Document, DocumentTag, ReviewTag,
LocalizationTag)
from ..queries import MultiQuerySet


@block_user_agents
Expand Down Expand Up @@ -190,33 +189,35 @@ def get_previous(revisions):

# Grab revisions, but defer summary and content because they can lead to
# attempts to cache more than memcached allows.
revisions = MultiQuerySet(
(Revision.objects.filter(pk=document.current_revision.pk)
.prefetch_related('creator', 'document')
.transform(get_previous)),
(Revision.objects.filter(document=document)
.order_by('-created', '-id')
.exclude(pk=document.current_revision.pk)
.prefetch_related('creator', 'document')
.transform(get_previous))
)

if not revisions.exists():
all_revisions = (document.revisions.defer('summary', 'content').order_by('created', 'id')
.select_related('creator').reverse().transform(get_previous))

if not all_revisions.exists():
raise Http404

if per_page == 'all':
page = None
all_revisions = list(all_revisions)
else:
try:
per_page = int(per_page)
except ValueError:
per_page = DOCUMENTS_PER_PAGE

page = paginate(request, revisions, per_page)
revisions = page.object_list
page = paginate(request, all_revisions, per_page)
all_revisions = list(page.object_list)
# In order to compare the first revision of a translation, need to insert its parent revision to the list
# The parent revision should stay at last page in order to compare. So insert only if there are no next page or
# all revisions are showing
if (not page or not page.has_next()) and document.parent:
# *all_revisions are in descending order. so call last() in order to get first revision
first_rev_based_on = all_revisions[-1].based_on
# Translation can be orphan so that first revision does not have any english based on. So handle the situation.
if first_rev_based_on:
all_revisions.append(first_rev_based_on)

context = {
'revisions': revisions,
'revisions': all_revisions,
'document': document,
'page': page,
}
Expand Down
7 changes: 6 additions & 1 deletion kuma/wiki/views/revision.py
Expand Up @@ -94,7 +94,12 @@ def compare(request, document_slug, document_locale):
to_id = smart_int(request.GET.get('to'))

revisions = Revision.objects.prefetch_related('document')
revision_from = get_object_or_404(revisions, id=from_id, document=doc)
# It should also be possible to compare from the parent document revision
try:
revision_from = revisions.get(id=from_id, document=doc)
except Revision.DoesNotExist:
revision_from = get_object_or_404(revisions, id=from_id, document=doc.parent)

revision_to = get_object_or_404(revisions, id=to_id, document=doc)

context = {
Expand Down

0 comments on commit a5bee8e

Please sign in to comment.