Skip to content

Commit

Permalink
only show MT-button if user has publish rights for a page
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbulz committed Jan 27, 2025
1 parent c73bfb8 commit 776711e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
22 changes: 21 additions & 1 deletion integreat_cms/cms/forms/pages/page_translation_form.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import annotations

import logging
from typing import TYPE_CHECKING

from ...models import PageTranslation
if TYPE_CHECKING:
from treebeard.ns_tree import NS_NodeQuerySet

from ...models import PageTranslation, LanguageTreeNode
from ..machine_translation_form import MachineTranslationForm

logger = logging.getLogger(__name__)
Expand All @@ -14,6 +18,22 @@ class PageTranslationForm(MachineTranslationForm):
Form for creating and modifying page translation objects
"""

def user_has_publish_rights(self) -> bool:
"""
Helper method to check if the current user has permission to publish the page
"""
if page_obj := self.instance.page:
return self.request.user.has_perm("cms.publish_page_object", page_obj)
return self.request.user.has_perm("cms.publish_page")

def mt_form_is_enabled(self) -> NS_NodeQuerySet:
"""
For pages, machine translations should only be enabled if the user has publishing rights
"""
if not self.user_has_publish_rights():
return LanguageTreeNode.objects.none()
return super().mt_form_is_enabled()

class Meta:
"""
This class contains additional meta configuration of the form class, see the :class:`django.forms.ModelForm`
Expand Down
4 changes: 2 additions & 2 deletions tests/mt_api/deepl_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from integreat_cms.cms.models import Page
from tests.mock import MockServer

from ..conftest import AUTHOR, EDITOR, MANAGEMENT, PRIV_STAFF_ROLES
from ..conftest import EDITOR, MANAGEMENT, PRIV_STAFF_ROLES
from ..utils import assert_message_in_log
from .utils import get_content_translations

Expand Down Expand Up @@ -69,7 +69,7 @@ def setup_deepl_supported_languages(

@pytest.mark.django_db
@pytest.mark.parametrize(
"login_role_user", PRIV_STAFF_ROLES + [AUTHOR, MANAGEMENT, EDITOR], indirect=True
"login_role_user", PRIV_STAFF_ROLES + [MANAGEMENT, EDITOR], indirect=True
)
@pytest.mark.parametrize("error", api_errors)
def test_deepl_bulk_mt_api_error(
Expand Down
4 changes: 2 additions & 2 deletions tests/mt_api/google_translate_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.apps import apps
from django.urls import reverse

from ..conftest import AUTHOR, EDITOR, MANAGEMENT, PRIV_STAFF_ROLES
from ..conftest import EDITOR, MANAGEMENT, PRIV_STAFF_ROLES
from ..utils import assert_message_in_log

if TYPE_CHECKING:
Expand Down Expand Up @@ -80,7 +80,7 @@ def setup_fake_google_translate_api( # type: ignore[no-untyped-def]

@pytest.mark.django_db
@pytest.mark.parametrize(
"login_role_user", PRIV_STAFF_ROLES + [AUTHOR, MANAGEMENT, EDITOR], indirect=True
"login_role_user", PRIV_STAFF_ROLES + [MANAGEMENT, EDITOR], indirect=True
)
def test_google_translate_error(
login_role_user: tuple[Client, str],
Expand Down
48 changes: 33 additions & 15 deletions tests/mt_api/mt_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def mt_setup(
content_role_id_combination = [
(
Page,
PRIV_STAFF_ROLES + [AUTHOR, MANAGEMENT, EDITOR],
PRIV_STAFF_ROLES + [MANAGEMENT, EDITOR],
[28],
),
(
Expand Down Expand Up @@ -154,7 +154,22 @@ def test_bulk_mt(
response = client.post(machine_translation, data={"selected_ids[]": ids})
print(response.headers)

if role in entitled_roles:
if role == AUTHOR and content_type == Page:
assert response.status_code == 302
tree = reverse(
content_type._meta.default_related_name,
kwargs={
"region_slug": REGION_SLUG,
"language_slug": target_language_slug,
},
)
assert response.headers.get("Location") == tree
assert_message_in_log(
"ERROR Machine translations are not allowed for the current user or content type",
caplog,
)

elif role in entitled_roles:
# If the role should be allowed to access the view, we expect a successful result
assert response.status_code == 302
tree = reverse(
Expand Down Expand Up @@ -218,7 +233,7 @@ def test_bulk_mt(

@pytest.mark.django_db
@pytest.mark.parametrize(
"login_role_user", PRIV_STAFF_ROLES + [AUTHOR, MANAGEMENT, EDITOR], indirect=True
"login_role_user", PRIV_STAFF_ROLES + [MANAGEMENT, EDITOR], indirect=True
)
@pytest.mark.parametrize("provider_language_combination", provider_language_combination)
def test_bulk_mt_exceeds_limit(
Expand Down Expand Up @@ -299,7 +314,7 @@ def test_bulk_mt_exceeds_limit(

@pytest.mark.django_db
@pytest.mark.parametrize(
"login_role_user", PRIV_STAFF_ROLES + [AUTHOR, MANAGEMENT, EDITOR], indirect=True
"login_role_user", PRIV_STAFF_ROLES + [MANAGEMENT, EDITOR], indirect=True
)
@pytest.mark.parametrize("provider_language_combination", provider_language_combination)
def test_bulk_mt_up_to_date(
Expand Down Expand Up @@ -367,7 +382,7 @@ def test_bulk_mt_up_to_date(

@pytest.mark.django_db
@pytest.mark.parametrize(
"login_role_user", PRIV_STAFF_ROLES + [AUTHOR, MANAGEMENT, EDITOR], indirect=True
"login_role_user", PRIV_STAFF_ROLES + [MANAGEMENT, EDITOR], indirect=True
)
@pytest.mark.parametrize("provider_language_combination", provider_language_combination)
def test_bulk_mt_up_to_date_and_ready_for_mt(
Expand Down Expand Up @@ -457,7 +472,7 @@ def test_bulk_mt_up_to_date_and_ready_for_mt(
content_role_id_data_combination = [
(
Page,
PRIV_STAFF_ROLES + [AUTHOR, MANAGEMENT, EDITOR],
PRIV_STAFF_ROLES + [MANAGEMENT, EDITOR],
4,
{
"title": "Neuer Titel",
Expand Down Expand Up @@ -566,11 +581,7 @@ def test_automatic_translation(
+ create_or_update: Language.objects.filter(slug=target_language_slug)
.first()
.id,
"status": (
status.REVIEW
if content_type is Page and role is AUTHOR
else status.PUBLIC
),
"status": status.PUBLIC,
}
)

Expand All @@ -581,8 +592,15 @@ def test_automatic_translation(
edit_content,
**{"data": data}, # noqa: PIE804
)

if role in entitled_roles:
if role == AUTHOR and content_type == Page:
target_language_id = (
Language.objects.filter(slug=target_language_slug).first().id
)
assert_message_in_log(
f"ERROR Update existing translations:: Select a valid choice. {target_language_id} is not one of the available choices.",
caplog,
)
elif role in entitled_roles:
# If the role should be allowed to access the view, we expect a successful result
translations = get_content_translations(
content_type, [content_id], source_language_slug, target_language_slug
Expand Down Expand Up @@ -625,7 +643,7 @@ def test_automatic_translation(

@pytest.mark.django_db
@pytest.mark.parametrize(
"login_role_user", PRIV_STAFF_ROLES + [AUTHOR, MANAGEMENT, EDITOR], indirect=True
"login_role_user", PRIV_STAFF_ROLES + [MANAGEMENT, EDITOR], indirect=True
)
@pytest.mark.parametrize("provider_language_combination", provider_language_combination)
def test_bulk_mt_no_source_language(
Expand Down Expand Up @@ -700,7 +718,7 @@ def test_bulk_mt_no_source_language(

@pytest.mark.django_db
@pytest.mark.parametrize(
"login_role_user", PRIV_STAFF_ROLES + [AUTHOR, MANAGEMENT, EDITOR], indirect=True
"login_role_user", PRIV_STAFF_ROLES + [MANAGEMENT, EDITOR], indirect=True
)
@pytest.mark.parametrize("provider_language_combination", provider_language_combination)
def test_deepl_bulk_mt_no_target_language(
Expand Down

0 comments on commit 776711e

Please sign in to comment.