Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Django views implementing the XBlock workbench.
- This code is in the Workbench layer.
- """
- import json
- import logging
- import mimetypes
- from django.http import HttpResponse, Http404
- from django.shortcuts import redirect, render_to_response
- from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie
- from xblock.core import XBlock, XBlockAside
- from xblock.django.request import webob_to_django_response, django_to_webob_request
- from xblock.exceptions import NoSuchUsage
- from .models import XBlockState
- from .runtime import WorkbenchRuntime, reset_global_state
- from .scenarios import get_scenarios
- from xblock.plugin import PluginMissingError
- # Imports for google drive api
- import requests
- import httplib2
- from oauth2client import client
- from apiclient import discovery
- log = logging.getLogger(__name__)
- scen_id = 'drivepy.0'
- # We don't really have authentication and multiple students, just accept their
- # id on the URL.
- def get_student_id(request):
- """Get the student_id from the given request."""
- student_id = request.GET.get('student', 'student_1')
- return student_id
- # ---- Views -----
- def index(_request):
- """Render `index.html`"""
- the_scenarios = sorted(get_scenarios().items())
- return render_to_response('workbench/index.html', {
- 'scenarios': [(desc, scenario.description) for desc, scenario in the_scenarios]
- })
- @ensure_csrf_cookie
- def show_scenario(request, scenario_id, view_name='student_view', template='workbench/block.html'):
- """
- Render the given `scenario_id` for the given `view_name`, on the provided `template`.
- `view_name` defaults to 'student_view'.
- `template` defaults to 'block.html'.
- """
- if scenario_id == 'drivepy.0':
- scen_id = 'drivepy.0'
- elif scenario_id == 'drivepy.1':
- scen_id = 'drivepy.1'
- else:
- scen_id = ''
- if scenario_id == scen_id:
- if 'credentials' not in request.session:
- return redirect('http://drivepython.10.0.2.186.xip.io:8020/oauth2callback')
- credentials = client.OAuth2Credentials.from_json(request.session['credentials'])
- if credentials.access_token_expired:
- return redirect('http://drivepython.10.0.2.186.xip.io:8020/oauth2callback')
- else:
- http_auth = credentials.authorize(httplib2.Http())
- drive_service = discovery.build('drive', 'v3', http_auth)
- files = drive_service.files().list(q="'root' in parents",fields="nextPageToken, files(id, name, mimeType, trashed, webViewLink, fileExtension)").execute()
- items = files.get('files', [])
- student_id = get_student_id(request)
- log.info("Start show_scenario %r for student %s", scenario_id, student_id)
- try:
- scenario = get_scenarios()[scenario_id]
- except KeyError:
- raise Http404
- usage_id = scenario.usage_id
- runtime = WorkbenchRuntime(student_id)
- block = runtime.get_block(usage_id)
- render_context = {
- 'activate_block_id': request.GET.get('activate_block_id', None),
- 'files': items
- }
- frag = block.render(view_name, render_context)
- log.info("End show_scenario %s", scenario_id)
- return render_to_response('drivepy/drivepy.html', {
- 'scenario': scenario,
- 'block': block,
- 'body': frag.body_html(),
- 'head_html': frag.head_html(),
- 'foot_html': frag.foot_html(),
- 'student_id': student_id,
- 'files': items
- })
- else:
- student_id = get_student_id(request)
- log.info("Start show_scenario %r for student %s", scenario_id, student_id)
- try:
- scenario = get_scenarios()[scenario_id]
- except KeyError:
- raise Http404
- usage_id = scenario.usage_id
- runtime = WorkbenchRuntime(student_id)
- block = runtime.get_block(usage_id)
- render_context = {
- 'activate_block_id': request.GET.get('activate_block_id', None)
- }
- frag = block.render(view_name, render_context)
- log.info("End show_scenario %s", scenario_id)
- return render_to_response(template, {
- 'scenario': scenario,
- 'block': block,
- 'body': frag.body_html(),
- 'head_html': frag.head_html(),
- 'foot_html': frag.foot_html(),
- 'student_id': student_id
- })
- def user_list(_request):
- """
- This will return a list of all users in the database
- """
- # We'd really like to do .distinct, but sqlite+django does not support this;
- # hence the hack with sorted(set(...))
- users = sorted(
- user_id[0]
- for user_id in set(XBlockState.objects.values_list('user_id'))
- )
- return HttpResponse(
- json.dumps(users, indent=2),
- content_type='application/json',
- )
- # View for the user authentication
- def oauth2callback(request):
- flow = client.flow_from_clientsecrets('/var/www/credentials/client_secrets.json', scope='https://www.googleapis.com/auth/drive', redirect_uri='http://drivepython.10.0.2.186.xip.io:8020/oauth2callback')
- if 'code' not in request.GET:
- auth_uri = flow.step1_get_authorize_url()
- return redirect(auth_uri)
- else:
- auth_code = request.GET.get('code')
- credentials = flow.step2_exchange(auth_code)
- request.session['credentials'] = credentials.to_json()
- return redirect("http://drivepython.10.0.2.186.xip.io:8020/scenario/"+scen_id)
- # View for create new File
- def new(request):
- credentials = client.OAuth2Credentials.from_json(request.session['credentials'])
- http_auth = credentials.authorize(httplib2.Http())
- drive_service = discovery.build('drive', 'v3', http_auth)
- if 'folder_id' in request.GET:
- folder_id = request.GET.get('folder_id')
- else:
- folder_id = 'root'
- # Metadata of the new file to create
- file_metadata = {
- 'mimeType' : 'application/vnd.google-apps.'+request.GET.get('type'),
- 'parents': [ folder_id ]
- }
- # Create New File
- file = drive_service.files().create(body=file_metadata, fields='id, webViewLink').execute()
- file_url = file.get("webViewLink")
- # Obtain Updated List of Files
- files = drive_service.files().list(q="'{0}' in parents".format(folder_id), fields="nextPageToken, files(id, name, mimeType, trashed, webViewLink, fileExtension)").execute()
- items = files.get('files', [])
- if request.GET.get('folder_id'):
- # Render view to create a file into a Folder
- student_id = get_student_id(request)
- try:
- scenario = get_scenarios()[scen_id]
- except KeyError:
- raise Http404
- usage_id = scenario.usage_id
- runtime = WorkbenchRuntime(student_id)
- block = runtime.get_block(usage_id)
- render_context = {
- 'activate_block_id': request.GET.get('activate_block_id', None)
- }
- frag = block.render('student_view', render_context)
- return render_to_response('drivepy/folder.html', {
- "folder_id": folder_id,
- "file_url": request.GET.get('file_url'),
- 'head_html': frag.head_html(),
- 'foot_html': frag.foot_html(),
- 'files': items
- })
- return redirect('http://drivepython.10.0.2.186.xip.io:8020/scenario/'+scen_id, file_url=file_url)
- # View for obtain the files of a Folder
- def folder(request):
- folder_id = request.GET.get('id')
- folder_name = request.GET.get('name')
- credentials = client.OAuth2Credentials.from_json(request.session['credentials'])
- http_auth = credentials.authorize(httplib2.Http())
- drive_service = discovery.build('drive', 'v3', http_auth)
- files = drive_service.files().list(q="'{0}' in parents".format(folder_id), fields="nextPageToken, files(id, name, mimeType, trashed, webViewLink, fileExtension)").execute()
- items = files.get('files', [])
- student_id = get_student_id(request)
- try:
- scenario = get_scenarios()[scen_id]
- except KeyError:
- raise Http404
- usage_id = scenario.usage_id
- runtime = WorkbenchRuntime(student_id)
- block = runtime.get_block(usage_id)
- render_context = {
- 'activate_block_id': request.GET.get('activate_block_id', None)
- }
- frag = block.render('student_view', render_context)
- return render_to_response('drivepy/folder.html', {
- "files": items,
- "folder_name": folder_name,
- "folder_id": folder_id,
- "file_url": request.GET.get('file_url'),
- 'head_html': frag.head_html(),
- 'foot_html': frag.foot_html(),
- 'files': items
- })
- def handler(request, usage_id, handler_slug, suffix='', authenticated=True):
- """The view function for authenticated handler requests."""
- if authenticated:
- student_id = get_student_id(request)
- log.info("Start handler %s/%s for student %s", usage_id, handler_slug, student_id)
- else:
- student_id = "none"
- log.info("Start handler %s/%s", usage_id, handler_slug)
- runtime = WorkbenchRuntime(student_id)
- try:
- block = runtime.get_block(usage_id)
- except NoSuchUsage:
- raise Http404
- request = django_to_webob_request(request)
- request.path_info_pop()
- request.path_info_pop()
- result = block.runtime.handle(block, handler_slug, request, suffix)
- log.info("End handler %s/%s", usage_id, handler_slug)
- return webob_to_django_response(result)
- def aside_handler(request, aside_id, handler_slug, suffix='', authenticated=True):
- """The view function for authenticated handler requests."""
- if authenticated:
- student_id = get_student_id(request)
- log.info("Start handler %s/%s for student %s", aside_id, handler_slug, student_id)
- else:
- student_id = "none"
- log.info("Start handler %s/%s", aside_id, handler_slug)
- runtime = WorkbenchRuntime(student_id)
- try:
- block = runtime.get_aside(aside_id)
- except NoSuchUsage:
- raise Http404
- request = django_to_webob_request(request)
- request.path_info_pop()
- request.path_info_pop()
- result = block.runtime.handle(block, handler_slug, request, suffix)
- log.info("End handler %s/%s", aside_id, handler_slug)
- return webob_to_django_response(result)
- def package_resource(_request, block_type, resource):
- """
- Wrapper for `pkg_resources` that tries to access a resource and, if it
- is not found, raises an Http404 error.
- """
- try:
- xblock_class = XBlock.load_class(block_type)
- except PluginMissingError:
- try:
- xblock_class = XBlockAside.load_class(block_type)
- except PluginMissingError:
- raise Http404
- try:
- content = xblock_class.open_local_resource(resource)
- except Exception: # pylint: disable-msg=broad-except
- raise Http404
- mimetype, _ = mimetypes.guess_type(resource)
- return HttpResponse(content, content_type=mimetype)
- @csrf_exempt
- def reset_state(request):
- """Delete all state and reload the scenarios."""
- log.info("RESETTING ALL STATE")
- reset_global_state()
- referrer_url = request.META['HTTP_REFERER']
- return redirect(referrer_url)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement