Advertisement
eduardoperezl

show_scenario Views

Jun 2nd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. @ensure_csrf_cookie
  2. def show_scenario(request, scenario_id, view_name='student_view', template='workbench/block.html'):
  3.     """
  4.    Render the given `scenario_id` for the given `view_name`, on the provided `template`.
  5.  
  6.    `view_name` defaults to 'student_view'.
  7.    `template` defaults to 'block.html'.
  8.  
  9.    """
  10.     if scenario_id == 'drivepy.0':
  11.         scen_id = 'drivepy.0'
  12.     elif scenario_id == 'drivepy.1':
  13.         scen_id = 'drivepy.1'
  14.     else:
  15.         scen_id = ''
  16.  
  17.     if scenario_id == scen_id:
  18.         if 'credentials' not in request.session:
  19.             return redirect('http://drivepython.10.0.2.186.xip.io:8020/oauth2callback')
  20.         credentials = client.OAuth2Credentials.from_json(request.session['credentials'])
  21.         if credentials.access_token_expired:
  22.             return redirect('http://drivepython.10.0.2.186.xip.io:8020/oauth2callback')
  23.         else:
  24.             http_auth = credentials.authorize(httplib2.Http())
  25.             drive_service = discovery.build('drive', 'v3', http_auth)
  26.             files = drive_service.files().list(q="'root' in parents",fields="nextPageToken, files(id, name, mimeType, trashed, webViewLink, fileExtension)").execute()
  27.             items = files.get('files', [])
  28.  
  29.             student_id = get_student_id(request)
  30.             log.info("Start show_scenario %r for student %s", scenario_id, student_id)
  31.  
  32.             try:
  33.                 scenario = get_scenarios()[scenario_id]
  34.             except KeyError:
  35.                 raise Http404
  36.  
  37.             usage_id = scenario.usage_id
  38.             runtime = WorkbenchRuntime(student_id)
  39.             block = runtime.get_block(usage_id)
  40.             render_context = {
  41.                 'activate_block_id': request.GET.get('activate_block_id', None),
  42.                 'files': items
  43.             }
  44.             frag = block.render(view_name, render_context)
  45.             log.info("End show_scenario %s", scenario_id)
  46.             return render_to_response('drivepy/drivepy.html', {
  47.                 'scenario': scenario,
  48.                 'block': block,
  49.                 'body': frag.body_html(),
  50.                 'head_html': frag.head_html(),
  51.                 'foot_html': frag.foot_html(),
  52.                 'student_id': student_id,
  53.                 'files': items
  54.             })
  55.     else:
  56.         student_id = get_student_id(request)
  57.         log.info("Start show_scenario %r for student %s", scenario_id, student_id)
  58.  
  59.         try:
  60.             scenario = get_scenarios()[scenario_id]
  61.         except KeyError:
  62.             raise Http404
  63.  
  64.         usage_id = scenario.usage_id
  65.         runtime = WorkbenchRuntime(student_id)
  66.         block = runtime.get_block(usage_id)
  67.         render_context = {
  68.             'activate_block_id': request.GET.get('activate_block_id', None)
  69.         }
  70.  
  71.         frag = block.render(view_name, render_context)
  72.         log.info("End show_scenario %s", scenario_id)
  73.         return render_to_response(template, {
  74.             'scenario': scenario,
  75.             'block': block,
  76.             'body': frag.body_html(),
  77.             'head_html': frag.head_html(),
  78.             'foot_html': frag.foot_html(),
  79.             'student_id': student_id
  80.         })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement