|
33 | 33 | STATE_NEEDS_REWRITING, |
34 | 34 | STATE_TRANSLATED, |
35 | 35 | ) |
36 | | -from weblate.utils.stats import ProjectLanguage |
| 36 | +from weblate.utils.stats import CategoryLanguage, ProjectLanguage |
| 37 | +from weblate.utils.views import get_sort_name |
37 | 38 |
|
38 | 39 | if TYPE_CHECKING: |
39 | 40 | from weblate.checks.base import BaseCheck |
@@ -1354,6 +1355,99 @@ def test_revert_history_after_component_move_uses_current_translate_url( |
1354 | 1355 | f'href="{translate_url}?checksum={unit.checksum}&revert={change.id}"', |
1355 | 1356 | ) |
1356 | 1357 |
|
| 1358 | + def test_project_language_warns_when_switching_component(self) -> None: |
| 1359 | + Component.objects.filter(pk=self.component.pk).update(priority=120) |
| 1360 | + high_component = self.create_po(name="High", priority=80, project=self.project) |
| 1361 | + high_translation = high_component.translation_set.get( |
| 1362 | + language=self.translation.language |
| 1363 | + ) |
| 1364 | + translate_url = ProjectLanguage( |
| 1365 | + self.project, self.translation.language |
| 1366 | + ).get_translate_url() |
| 1367 | + high_component_offset = high_translation.unit_set.count() |
| 1368 | + |
| 1369 | + response = self.client.get(translate_url, {"offset": high_component_offset}) |
| 1370 | + self.assertNotContains(response, "You have shifted from") |
| 1371 | + self.assertContains(response, 'value="component,-priority"') |
| 1372 | + |
| 1373 | + response = self.client.get(translate_url, {"offset": high_component_offset + 1}) |
| 1374 | + self.assertContains(response, "You have shifted from") |
| 1375 | + |
| 1376 | + def test_language_scope_sort_defaults_to_component_priority(self) -> None: |
| 1377 | + request = SimpleNamespace(GET={}) |
| 1378 | + category = self.create_category(self.project) |
| 1379 | + |
| 1380 | + self.assertEqual( |
| 1381 | + get_sort_name( |
| 1382 | + request, ProjectLanguage(self.project, self.translation.language) |
| 1383 | + )["query"], |
| 1384 | + "component,-priority", |
| 1385 | + ) |
| 1386 | + self.assertEqual( |
| 1387 | + get_sort_name( |
| 1388 | + request, CategoryLanguage(category, self.translation.language) |
| 1389 | + )["query"], |
| 1390 | + "component,-priority", |
| 1391 | + ) |
| 1392 | + |
| 1393 | + def test_project_language_submitted_search_keeps_component_order(self) -> None: |
| 1394 | + Component.objects.filter(pk=self.component.pk).update(priority=120) |
| 1395 | + high_component = self.create_po( |
| 1396 | + name="High", locked=True, priority=80, project=self.project |
| 1397 | + ) |
| 1398 | + high_translation = high_component.translation_set.get( |
| 1399 | + language=self.translation.language |
| 1400 | + ) |
| 1401 | + translate_url = ProjectLanguage( |
| 1402 | + self.project, self.translation.language |
| 1403 | + ).get_translate_url() |
| 1404 | + |
| 1405 | + self.client.get(translate_url, {"sort_by": "component,-priority", "offset": 1}) |
| 1406 | + |
| 1407 | + session = self.client.session |
| 1408 | + session_keys = session.keys() |
| 1409 | + search_key = next(key for key in session_keys if key.startswith("search_")) |
| 1410 | + expected_ids = [ |
| 1411 | + *self.translation.unit_set.order_by("position").values_list( |
| 1412 | + "pk", flat=True |
| 1413 | + ), |
| 1414 | + *high_translation.unit_set.order_by("position").values_list( |
| 1415 | + "pk", flat=True |
| 1416 | + ), |
| 1417 | + ] |
| 1418 | + self.assertEqual(session[search_key]["ids"], expected_ids) |
| 1419 | + |
| 1420 | + def test_project_language_ignores_stale_component_shift_unit(self) -> None: |
| 1421 | + Component.objects.filter(pk=self.component.pk).update(priority=120) |
| 1422 | + high_component = self.create_po(name="High", priority=80, project=self.project) |
| 1423 | + high_translation = high_component.translation_set.get( |
| 1424 | + language=self.translation.language |
| 1425 | + ) |
| 1426 | + translate_url = ProjectLanguage( |
| 1427 | + self.project, self.translation.language |
| 1428 | + ).get_translate_url() |
| 1429 | + high_component_offset = high_translation.unit_set.count() |
| 1430 | + |
| 1431 | + response = self.client.get(translate_url, {"offset": high_component_offset}) |
| 1432 | + self.assertEqual(response.status_code, 200) |
| 1433 | + |
| 1434 | + last_unit_id = Unit.objects.order_by("-pk").values_list("pk", flat=True)[0] |
| 1435 | + stale_unit_id = last_unit_id + 1 |
| 1436 | + session = self.client.session |
| 1437 | + session_keys = session.keys() |
| 1438 | + search_key = next(key for key in session_keys if key.startswith("search_")) |
| 1439 | + session_result = session[search_key] |
| 1440 | + session_result["last_viewed_unit_id"] = stale_unit_id |
| 1441 | + session[search_key] = session_result |
| 1442 | + session.save() |
| 1443 | + |
| 1444 | + response = self.client.get(translate_url, {"offset": high_component_offset + 1}) |
| 1445 | + self.assertEqual(response.status_code, 200) |
| 1446 | + self.assertNotContains(response, "You have shifted from") |
| 1447 | + self.assertNotEqual( |
| 1448 | + self.client.session[search_key]["last_viewed_unit_id"], stale_unit_id |
| 1449 | + ) |
| 1450 | + |
1357 | 1451 | def test_revert_plural(self) -> None: |
1358 | 1452 | source = "Orangutan has %d banana.\n" |
1359 | 1453 | target = [ |
|
0 commit comments