Skip to content
Open
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
36 changes: 33 additions & 3 deletions contentcuration/contentcuration/tests/viewsets/test_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,12 @@ def setUp(self):
self.channel.language_id = "en"
self.channel.save()

self.editor = testdata.user(email="language-editor@user.com")
self.channel.editors.add(self.editor)
self.viewer = testdata.user(email="language-viewer@user.com")
self.channel.viewers.add(self.viewer)
self.non_member = testdata.user(email="language-nonmember@user.com")

self.channel_id = self.channel.id
self.node_id = "00000000000000000000000000000003"
self.public_node = PublicContentNode.objects.create(
Expand Down Expand Up @@ -1276,9 +1282,33 @@ def test_get_languages_in_channel_invalid_request(self):
response = self._perform_action("channel-languages", None)
self.assertEqual(response.status_code, 404, response.content)

def _perform_action(self, url_path, channel_id):
user = testdata.user()
self.client.force_authenticate(user=user)
def test_language_actions_viewer_has_access(self):
ContentNode.objects.filter(node_id=self.public_node.id).update(language_id="en")
response = self._perform_action(
"channel-language-exists", self.channel.id, user=self.viewer
)
self.assertEqual(response.status_code, 200, response.content)
self.assertTrue(response.json()["exists"])

response = self._perform_action(
"channel-languages", self.channel.id, user=self.viewer
)
self.assertEqual(response.status_code, 200, response.content)

def test_language_actions_nonexistent_channel_matches_non_member(self):
for url_path in ("channel-language-exists", "channel-languages"):
missing = self._perform_action(
url_path, uuid.uuid4().hex, user=self.non_member
)
denied = self._perform_action(
url_path, self.channel.id, user=self.non_member
)
self.assertEqual(missing.status_code, 404, missing.content)
self.assertEqual(missing.status_code, denied.status_code)
self.assertEqual(missing.content, denied.content)

def _perform_action(self, url_path, channel_id, user=None):
self.client.force_authenticate(user=user or self.editor)
response = self.client.get(
reverse(url_path, kwargs={"pk": channel_id}), format="json"
)
Expand Down
69 changes: 20 additions & 49 deletions contentcuration/contentcuration/viewsets/channel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from functools import reduce
from operator import or_
from typing import Dict
from typing import List
from typing import Union

Expand All @@ -15,8 +14,8 @@
from django.db.models import Value
from django.db.models.functions import Coalesce
from django.http import HttpResponse
from django.http import HttpResponseNotFound
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django_cte import With
Expand Down Expand Up @@ -883,12 +882,17 @@ def channel_language_exists(
:return: JsonResponse with exists=True if the language exists, False otherwise
:rtype: JsonResponse
"""
if not self._channel_exists(pk):
return HttpResponseNotFound("No channel matching: {}".format(pk))
channel = get_object_or_404(
models.Channel.filter_view_queryset(
models.Channel.objects.all(), request.user
).values("id", "language_id", "main_tree_id"),
pk=pk,
)

channel_lang, main_tree_id = self._get_channel_details(pk).values()
langs_in_channel = self._get_channel_content_languages(pk, main_tree_id)
lang_exists = channel_lang in langs_in_channel
langs_in_channel = self._get_channel_content_languages(
channel["id"], channel["main_tree_id"]
)
lang_exists = channel["language_id"] in langs_in_channel

return JsonResponse({"exists": lang_exists})

Expand All @@ -904,12 +908,16 @@ def get_languages_in_channel(
:return: JsonResponse with a list of languages present in the channel
:rtype: JsonResponse
"""
if not self._channel_exists(pk):
return HttpResponseNotFound("No channel matching: {}".format(pk))
channel = get_object_or_404(
models.Channel.filter_view_queryset(
models.Channel.objects.all(), request.user
).values("id", "language_id", "main_tree_id"),
pk=pk,
)

channel_details = self._get_channel_details(pk)
main_tree_id = channel_details.get("main_tree_id")
langs_in_content = self._get_channel_content_languages(pk, main_tree_id)
langs_in_content = self._get_channel_content_languages(
channel["id"], channel["main_tree_id"]
)
return JsonResponse({"languages": langs_in_content})

@action(
Expand Down Expand Up @@ -979,43 +987,6 @@ def has_community_library_submission(self, request, pk=None) -> Response:
).exists()
return Response({"has_community_library_submission": has_submission})

def _channel_exists(self, channel_id) -> bool:
"""
Check if a channel exists.

:param channel_id: The ID of the channel
:return: True if the channel exists, False otherwise
:rtype: bool
"""
try:
return Channel.objects.filter(pk=channel_id).exists()
except Exception as e:
logging.error(f"Error checking if channel exists: {e}")
return False

def _get_channel_details(self, channel_id) -> Dict[str, any]:
"""
Get the language set for a channel.

:param channel_id: The ID of the channel
:return: The language code set for the channel
:rtype: str
"""
try:
channel_details = (
Channel.objects.filter(pk=channel_id)
.values("language_id", "main_tree_id")
.first()
)
except Channel.DoesNotExist as e:
logging.error(str(e))
channel_details = None

if not channel_details:
channel_details = dict(language_id=None, main_tree_id=None)

return channel_details

def _get_channel_content_languages(
self, channel_id, main_tree_id=None
) -> List[str]:
Expand Down
Loading