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

[WIP] Add support for private redirecting link for studio admin #4919

Draft
wants to merge 8 commits into
base: unstable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions contentcuration/contentcuration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,15 @@ class Channel(models.Model):
verbose_name="secret tokens",
blank=True,
)
support_token = models.OneToOneField(
SecretToken,
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='support_channels',
verbose_name="support token",
help_text="Token for support access",
)
source_url = models.CharField(max_length=200, blank=True, null=True)
demo_server_url = models.CharField(max_length=200, blank=True, null=True)

Expand Down
1 change: 1 addition & 0 deletions contentcuration/contentcuration/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def get_redirect_url(self, *args, **kwargs):
# Add admin endpoints
urlpatterns += [
re_path(r'^api/send_custom_email/$', admin_views.send_custom_email, name='send_custom_email'),
re_path(r'^api/support_token_redirect/(?P<token>[^/]+)/$', admin_views.support_token_redirect, name='support_token_redirect'),
]

urlpatterns += [re_path(r'^jsreverse/$', django_js_reverse_views.urls_js, name='js_reverse')]
Expand Down
18 changes: 18 additions & 0 deletions contentcuration/contentcuration/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import redirect
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from rest_framework.authentication import BasicAuthentication
from rest_framework.authentication import SessionAuthentication
from rest_framework.authentication import TokenAuthentication
Expand All @@ -17,6 +19,7 @@
from contentcuration.tasks import sendcustomemails_task
from contentcuration.utils.messages import get_messages
from contentcuration.views.base import current_user_for_context
from contentcuration.models import SecretToken, Channel


@is_admin
Expand All @@ -30,6 +33,21 @@ def send_custom_email(request):

return Response({"success": True})

@is_admin
@api_view(['GET'])
def support_token_redirect(request, token):
try:
support_token = SecretToken.objects.get(token=token)
channel = get_object_or_404(Channel, support_token=support_token)

# Redirect to the channel edit page
return redirect("channels")

except SecretToken.DoesNotExist:
return Response({"error": "Invalid token"}, status=404)

except Channel.DoesNotExist:
return Response({"error": "Channel not found for token"}, status=404)

@login_required
@browser_is_supported
Expand Down
41 changes: 41 additions & 0 deletions contentcuration/contentcuration/viewsets/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,48 @@ def _get_channel_content_languages(self, channel_id, main_tree_id=None) -> List[
logging.error(str(e))
unique_lang_ids = []
return unique_lang_ids

@action(detail=True, methods=["get"], url_path="support_token", url_name="get-support-token")
def get_support_token(self, request, pk=None):
"""
Retrieve the existing support token for this channel, or null if none exists.
"""
channel = self.get_edit_queryset().get(pk=pk)
token_value = None
if channel.support_token:
token_value = channel.support_token.token

return Response({"support_token": token_value}, status=status.HTTP_200_OK)

@action(detail=True, methods=["post"], url_path="support_token", url_name="create-support-token")
def create_support_token(self, request, pk=None):
"""
Create a new support token for this channel.
"""
channel = self.get_edit_queryset().get(pk=pk)

if channel.support_token is not None:
return Response(
{"error": "Support token already exists for this channel."},
status=status.HTTP_409_CONFLICT
)

channel.support_token = proquint.generate()
channel.save(update_fields=["support_token"])

Change.create_change(
generate_update_event(
channel.id,
CHANNEL,
{"support_token": secret_token.token},
channel_id=channel.id,
),
applied=True,
created_by_id=request.user.id
)

data = self.serialize_object(pk=channel.pk)
return Response(data, status=status.HTTP_201_CREATED)

@method_decorator(
cache_page(
Expand Down
Loading