feat: Created API for programs live page zoom lti (#29763)

* feat: Created API for programs live page zoom lti

* refactor: Merged similar code by inheritance

* refactor: removed duplicates and resolved tight coupling issues

* refactor: Decoupled views
This commit is contained in:
Ahtisham Shahid
2022-01-21 12:26:47 +05:00
committed by GitHub
parent 4e22a38ca5
commit e63fb2e01a
8 changed files with 262 additions and 37 deletions

View File

@@ -3,23 +3,27 @@ Fragments for rendering programs.
"""
import json
from abc import ABC, abstractmethod
from urllib.parse import quote
from django.contrib.sites.shortcuts import get_current_site
from django.http import Http404
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import get_language, to_locale, gettext_lazy as _ # lint-amnesty, pylint: disable=unused-import
from django.utils.translation import get_language
from django.utils.translation import gettext_lazy as _ # lint-amnesty, pylint: disable=unused-import
from django.utils.translation import to_locale
from lti_consumer.lti_1p1.contrib.django import lti_embed
from web_fragments.fragment import Fragment
from common.djangoapps.student.models import anonymous_id_for_user
from common.djangoapps.student.roles import GlobalStaff
from lms.djangoapps.commerce.utils import EcommerceService
from lms.djangoapps.learner_dashboard.utils import FAKE_COURSE_KEY, program_tab_view_is_enabled, strip_course_id
from openedx.core.djangoapps.catalog.constants import PathwayType
from openedx.core.djangoapps.catalog.utils import get_pathways, get_programs
from openedx.core.djangoapps.credentials.utils import get_credentials_records_url
from openedx.core.djangoapps.discussions.models import ProgramDiscussionsConfiguration
from openedx.core.djangoapps.discussions.models import ProgramDiscussionsConfiguration, ProgramLiveConfiguration
from openedx.core.djangoapps.plugin_api.views import EdxFragmentView
from openedx.core.djangoapps.programs.models import ProgramsApiConfig
from openedx.core.djangoapps.programs.utils import (
@@ -30,7 +34,6 @@ from openedx.core.djangoapps.programs.utils import (
)
from openedx.core.djangoapps.user_api.preferences.api import get_user_preferences
from openedx.core.djangolib.markup import HTML
from common.djangoapps.student.models import anonymous_id_for_user
class ProgramsFragmentView(EdxFragmentView):
@@ -167,9 +170,9 @@ class ProgramDetailsFragmentView(EdxFragmentView):
return _('Program Details')
class ProgramDiscussionLTI:
class ProgramLTI(ABC):
"""
Encapsulates methods for program discussion iframe rendering.
Encapsulates methods for program LTI iframe rendering.
"""
DEFAULT_ROLE = 'Student'
ADMIN_ROLE = 'Administrator'
@@ -178,7 +181,11 @@ class ProgramDiscussionLTI:
self.program_uuid = program_uuid
self.program = get_programs(uuid=self.program_uuid)
self.request = request
self.configuration = ProgramDiscussionsConfiguration.get(self.program_uuid)
self.configuration = self.get_configuration()
@abstractmethod
def get_configuration(self):
return
@property
def is_configured(self):
@@ -264,7 +271,7 @@ class ProgramDiscussionLTI:
def render_iframe(self) -> str:
"""
Returns the program discussion fragment if program discussions configuration exists for a program uuid
Returns the program LTI iframe if program Lti configuration exists for a program uuid
"""
if not self.is_configured:
return ''
@@ -285,3 +292,13 @@ class ProgramDiscussionLTI:
)
)
return fragment.content
class ProgramDiscussionLTI(ProgramLTI):
def get_configuration(self):
return ProgramDiscussionsConfiguration.get(self.program_uuid)
class ProgramLiveLTI(ProgramLTI):
def get_configuration(self):
return ProgramLiveConfiguration.get(self.program_uuid)

View File

@@ -9,6 +9,8 @@ urlpatterns = [
re_path(r'^programs/(?P<program_uuid>[0-9a-f-]+)/$', views.program_details, name='program_details_view'),
re_path(r'^programs/(?P<program_uuid>[0-9a-f-]+)/discussion/$', views.ProgramDiscussionIframeView.as_view(),
name='program_discussion'),
re_path(r'^programs/(?P<program_uuid>[0-9a-f-]+)/live/$', views.ProgramLiveIframeView.as_view(),
name='program_live'),
path('programs_fragment/', programs.ProgramsFragmentView.as_view(), name='program_listing_fragment_view'),
re_path(r'^programs/(?P<program_uuid>[0-9a-f-]+)/details_fragment/$', programs.ProgramDetailsFragmentView.as_view(),
name='program_details_fragment_view'),

View File

@@ -2,9 +2,12 @@
The utility methods and functions to help the djangoapp logic
"""
from django.core.exceptions import ObjectDoesNotExist
from opaque_keys.edx.keys import CourseKey
from lms.djangoapps.learner_dashboard.config.waffle import ENABLE_PROGRAM_TAB_VIEW, ENABLE_MASTERS_PROGRAM_TAB_VIEW
from common.djangoapps.student.roles import GlobalStaff
from lms.djangoapps.learner_dashboard.config.waffle import ENABLE_MASTERS_PROGRAM_TAB_VIEW, ENABLE_PROGRAM_TAB_VIEW
from lms.djangoapps.program_enrollments.api import get_program_enrollment
FAKE_COURSE_KEY = CourseKey.from_string('course-v1:fake+course+run')
@@ -30,3 +33,16 @@ def masters_program_tab_view_is_enabled() -> bool:
check if masters program discussion is enabled.
"""
return ENABLE_MASTERS_PROGRAM_TAB_VIEW.is_enabled()
def is_enrolled_or_staff(request, program_uuid):
"""Returns true if the user is enrolled in the program or staff"""
if GlobalStaff().has_user(request.user):
return True
try:
get_program_enrollment(program_uuid=program_uuid, user=request.user)
except ObjectDoesNotExist:
return False
return True

View File

@@ -1,21 +1,18 @@
"""Learner dashboard views"""
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.views.decorators.http import require_GET
from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication
from rest_framework import permissions, status
from rest_framework.authentication import SessionAuthentication
from rest_framework.response import Response
from rest_framework.views import APIView
from lms.djangoapps.learner_dashboard.utils import masters_program_tab_view_is_enabled
from lms.djangoapps.program_enrollments.api import get_program_enrollment
from lms.djangoapps.learner_dashboard.utils import masters_program_tab_view_is_enabled, is_enrolled_or_staff
from common.djangoapps.edxmako.shortcuts import render_to_response
from common.djangoapps.student.roles import GlobalStaff
from lms.djangoapps.learner_dashboard.programs import (
ProgramDetailsFragmentView,
ProgramDiscussionLTI,
ProgramsFragmentView
ProgramsFragmentView, ProgramLiveLTI
)
from lms.djangoapps.program_enrollments.rest_api.v1.utils import ProgramSpecificViewMixin
from openedx.core.djangoapps.programs.models import ProgramsApiConfig
@@ -107,21 +104,9 @@ class ProgramDiscussionIframeView(APIView, ProgramSpecificViewMixin):
authentication_classes = (JwtAuthentication, BearerAuthentication, SessionAuthentication)
permission_classes = (permissions.IsAuthenticated,)
def is_enrolled_or_staff(self, request, program_uuid):
"""Returns true if the user is enrolled in the program or staff"""
if GlobalStaff().has_user(request.user):
return True
try:
get_program_enrollment(program_uuid=program_uuid, user=request.user)
except ObjectDoesNotExist:
return False
return True
def get(self, request, program_uuid):
""" GET handler """
if not self.is_enrolled_or_staff(request, program_uuid):
if not is_enrolled_or_staff(request, program_uuid):
default_response = {
'tab_view_enabled': False,
'discussion': {
@@ -139,3 +124,70 @@ class ProgramDiscussionIframeView(APIView, ProgramSpecificViewMixin):
}
}
return Response(response_data, status=status.HTTP_200_OK)
class ProgramLiveIframeView(APIView, ProgramSpecificViewMixin):
"""
A view for retrieving Program live IFrame .
Path: ``/dashboard/programs/{program_uuid}/live/``
Accepts: [GET]
------------------------------------------------------------------------------------
GET
------------------------------------------------------------------------------------
**Returns**
* 200: OK - Contains a program live zoom iframe.
* 401: The requesting user is not authenticated.
* 403: The requesting user lacks access to the program.
* 404: The requested program does not exist.
**Response**
In the case of a 200 response code, the response will be iframe HTML and status if discussion is configured
for the program.
**Example**
{
'tab_view_enabled': True,
'live': {
"iframe": "
<iframe
id='lti-tab-embed'
style='width: 100%; min-height: 800px; border: none'
srcdoc='{srcdoc}'
>
</iframe>
",
"configured": false
}
}
"""
authentication_classes = (JwtAuthentication, BearerAuthentication, SessionAuthentication)
permission_classes = (permissions.IsAuthenticated,)
def get(self, request, program_uuid):
""" GET handler """
if not is_enrolled_or_staff(request, program_uuid):
default_response = {
'tab_view_enabled': False,
'live': {
'configured': False,
'iframe': ''
}
}
return Response(default_response, status=status.HTTP_200_OK)
program_live_lti = ProgramLiveLTI(program_uuid, request)
response_data = {
'tab_view_enabled': masters_program_tab_view_is_enabled(),
'live': {
'iframe': program_live_lti.render_iframe(),
'configured': program_live_lti.is_configured,
}
}
return Response(response_data, status=status.HTTP_200_OK)