program.py 1.94 KB
Newer Older
1 2 3
"""Programs views for use with Studio."""
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
4
from django.http import Http404, JsonResponse
5 6 7 8 9
from django.utils.decorators import method_decorator
from django.views.generic import View

from edxmako.shortcuts import render_to_response
from openedx.core.djangoapps.programs.models import ProgramsApiConfig
10
from openedx.core.lib.token_utils import get_id_token
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34


class ProgramAuthoringView(View):
    """View rendering a template which hosts the Programs authoring app.

    The Programs authoring app is a Backbone SPA maintained in a separate repository.
    The app handles its own routing and provides a UI which can be used to create and
    publish new Programs (e.g, XSeries).
    """

    @method_decorator(login_required)
    def get(self, request, *args, **kwargs):
        """Populate the template context with values required for the authoring app to run."""
        programs_config = ProgramsApiConfig.current()

        if programs_config.is_studio_tab_enabled and request.user.is_staff:
            return render_to_response('program_authoring.html', {
                'show_programs_header': programs_config.is_studio_tab_enabled,
                'authoring_app_config': programs_config.authoring_app_config,
                'programs_api_url': programs_config.public_api_url,
                'studio_home_url': reverse('home'),
            })
        else:
            raise Http404
35 36 37 38 39 40 41 42 43 44 45 46 47


class ProgramsIdTokenView(View):
    """Provides id tokens to JavaScript clients for use with the Programs API."""

    @method_decorator(login_required)
    def get(self, request, *args, **kwargs):
        """Generate and return a token, if the integration is enabled."""
        if ProgramsApiConfig.current().is_studio_tab_enabled:
            id_token = get_id_token(request.user, 'programs')
            return JsonResponse({'id_token': id_token})
        else:
            raise Http404