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 28, 2025
1 parent c73bfb8 commit 4aad1f7
Showing 1 changed file with 21 additions and 1 deletion.
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

0 comments on commit 4aad1f7

Please sign in to comment.