Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save submissions in a proper order #4724

Merged
merged 3 commits into from
May 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions pootle/apps/pootle_store/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,25 @@ class Meta(object):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(UnitForm, self).__init__(*args, **kwargs)
self.updated_fields = []
self._updated_fields = []

self.fields['target_f'].widget.attrs['data-translation-aid'] = \
self['target_f'].value()

@property
def updated_fields(self):
order_dict = {
SubmissionFields.STATE: 0,
SubmissionFields.TARGET: 1,
}
return sorted(self._updated_fields, key=lambda x: order_dict[x[0]])

def clean_target_f(self):
value = self.cleaned_data['target_f']

if self.instance.target.strings != multistring(value or [u'']):
self.instance._target_updated = True
self.updated_fields.append((SubmissionFields.TARGET,
self._updated_fields.append((SubmissionFields.TARGET,
to_db(self.instance.target),
to_db(value)))

Expand Down Expand Up @@ -347,7 +355,7 @@ def clean(self):

if old_state not in [new_state, OBSOLETE]:
self.instance._state_updated = True
self.updated_fields.append((SubmissionFields.STATE,
self._updated_fields.append((SubmissionFields.STATE,
old_state, new_state))

self.cleaned_data['state'] = new_state
Expand Down
22 changes: 12 additions & 10 deletions pootle/apps/pootle_store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import os
from hashlib import md5

from collections import OrderedDict

from translate.filters.decorators import Category
from translate.storage import base

Expand Down Expand Up @@ -1138,11 +1140,11 @@ def accept_suggestion(self, suggestion, translation_project, reviewer):
suggestion.review_time = current_time
suggestion.save()

create_subs = {}
create_subs[SubmissionFields.TARGET] = [old_target, self.target]
create_subs = OrderedDict()
if old_state != self.state:
create_subs[SubmissionFields.STATE] = [old_state, self.state]
self.store.mark_dirty(CachedMethods.WORDCOUNT_STATS)
create_subs[SubmissionFields.TARGET] = [old_target, self.target]

for field in create_subs:
kwargs = {
Expand Down Expand Up @@ -1576,14 +1578,7 @@ def record_submissions(self, unit, old_target, old_state, current_time,
being available in `unit`. Let's look into replacing such members with
something saner (#3895).
"""
create_subs = {}

# FIXME: extreme implicit hazard
if unit._target_updated:
create_subs[SubmissionFields.TARGET] = [
old_target,
unit.target_f,
]
create_subs = OrderedDict()

# FIXME: extreme implicit hazard
if unit._state_updated:
Expand All @@ -1592,6 +1587,13 @@ def record_submissions(self, unit, old_target, old_state, current_time,
unit.state,
]

# FIXME: extreme implicit hazard
if unit._target_updated:
create_subs[SubmissionFields.TARGET] = [
old_target,
unit.target_f,
]

# FIXME: extreme implicit hazard
if unit._comment_updated:
create_subs[SubmissionFields.COMMENT] = [
Expand Down
6 changes: 6 additions & 0 deletions pootle/apps/pootle_store/unit/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,16 @@ def get_grouped_entries(self):
]
])
)

# Target field timeline entry should go first
def target_field_should_be_first(x):
return 0 if x["field"] == SubmissionFields.TARGET else 1

# Group by submitter id and creation_time because
# different submissions can have same creation time
for key, values in grouped_timeline:
entry_group = {'entries': []}
values = sorted(values, key=target_field_should_be_first)
for item in values:
if "submitter" not in entry_group:
entry_group['submitter'] = DisplayUser(
Expand Down
79 changes: 79 additions & 0 deletions tests/models/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
from django.utils import timezone

from pytest_pootle.factories import SubmissionFactory
from pytest_pootle.utils import create_store

from pootle_app.models.permissions import check_permission
from pootle_statistics.models import (Submission, SubmissionFields,
SubmissionTypes)
from pootle_store.models import Suggestion, Unit
from pootle_store.util import TRANSLATED, UNTRANSLATED


Expand Down Expand Up @@ -121,3 +124,79 @@ def test_needs_scorelog():
new_value=u'',
)
assert submission.needs_scorelog()


@pytest.mark.django_db
def test_update_submission_ordering():
unit = Unit.objects.filter(state=UNTRANSLATED).first()
unit.markfuzzy()
unit.target = "Fuzzy Translation for " + unit.source_f
unit.save()

store = create_store(
unit.store.pootle_path,
"0",
[(unit.source_f, "Translation for " + unit.source_f)]
)
unit.store.update(store)
submission_field = Submission.objects.filter(unit=unit).latest().field
assert submission_field == SubmissionFields.TARGET


@pytest.mark.django_db
def test_new_translation_submission_ordering(client, request_users, settings):
unit = Unit.objects.filter(state=UNTRANSLATED).first()
settings.POOTLE_CAPTCHA_ENABLED = False
user = request_users["user"]
if user.username != "nobody":
client.login(
username=user.username,
password=request_users["password"])

url = '/xhr/units/%d/' % unit.id

response = client.post(
url,
{
'state': False,
'target_f_0': "Translation for " + unit.source_f,
},
HTTP_X_REQUESTED_WITH='XMLHttpRequest'
)

if check_permission('translate', response.wsgi_request):
assert response.status_code == 200
submission_field = Submission.objects.filter(unit=unit).latest().field
assert submission_field == SubmissionFields.TARGET
else:
assert response.status_code == 403


@pytest.mark.django_db
def test_accept_sugg_submission_ordering(client, request_users, settings):
"""Tests suggestion can be accepted with a comment."""
settings.POOTLE_CAPTCHA_ENABLED = False
unit = Unit.objects.filter(suggestion__state='pending',
state=UNTRANSLATED)[0]
unit.markfuzzy()
unit.target = "Fuzzy Translation for " + unit.source_f
unit.save()
sugg = Suggestion.objects.filter(unit=unit, state='pending')[0]
user = request_users["user"]
if user.username != "nobody":
client.login(
username=user.username,
password=request_users["password"])

url = '/xhr/units/%d/suggestions/%d/' % (unit.id, sugg.id)
response = client.post(
url,
HTTP_X_REQUESTED_WITH='XMLHttpRequest'
)

if check_permission('review', response.wsgi_request):
assert response.status_code == 200
submission_field = Submission.objects.filter(unit=unit).latest().field
assert submission_field == SubmissionFields.TARGET
else:
assert response.status_code == 403