def whoami(request): """View function for whoami page of site.""" bb_json = request.session.get('bb_json') if (bb_json is None): bb = BbRest(KEY, SECRET, f"https://{LEARNFQDN}") bb_json = jsonpickle.encode(bb) print('pickled BbRest putting it on session') request.session['bb_json'] = bb_json request.session[ 'target_view'] = 'whoami' # So after we have the access token we know to come back here. # The following does maintain the https: scheme if that was used with the incomming request. # BUT because I'm terminating the tls at the ngrok server, my incomming request is http. # Hence the redirect to get_auth_code is http in development. But we want our redirect_uri to be # have a scheme of https so that the Learn server can redirect back through ngrok with our # secure SSL cert. We'll have to build a redirect_uri with the https scheme in the # get_auth_code function. return HttpResponseRedirect(reverse('get_auth_code')) else: print('got BbRest from session') bb = jsonpickle.decode(bb_json) if bb.is_expired(): print('expired token') request.session['bb_json'] = None whoami(request) bb.supported_functions() # This and the following are required after bb.method_generator() # unpickling the pickled object. print(f'expiration: {bb.expiration()}') resp = bb.call( 'GetUser', userId="me", params={ 'fields': 'id, userName, name.given, name.middle, name.family, externalId, contact.email, dataSourceId, created' }, sync=True) #Need BbRest to support "me" user_json = resp.json() dskresp = bb.call('GetDataSource', dataSourceId=user_json['dataSourceId'], sync=True) dsk_json = dskresp.json() user_json['dataSourceId'] = dsk_json['externalId'] context = { 'user_json': user_json, 'access_token': bb.token_info['access_token'] } # Render the HTML template index.html with the data in the context variable return render(request, 'whoami.html', context=context)
def courses(request): """View function for courses page of site.""" bb_json = request.session.get('bb_json') if (bb_json is None): bb = BbRest(KEY, SECRET, f"https://{LEARNFQDN}") bb_json = jsonpickle.encode(bb) print('pickled BbRest putting it on session') request.session['bb_json'] = bb_json request.session['target_view'] = 'courses' return HttpResponseRedirect(reverse('get_auth_code')) else: print('got BbRest from session') bb = jsonpickle.decode(bb_json) if bb.is_expired(): print('expired token') request.session['bb_json'] = None whoami(request) bb.supported_functions() # This and the following are required after bb.method_generator() # unpickling the pickled object. print(f'expiration: {bb.expiration()}') resp = bb.call('GetCourses', sync=True) courses_json = resp.json() context = { 'courses_json': courses_json, 'access_token': bb.token_info['access_token'] } # Render the HTML template index.html with the data in the context variable return render(request, 'courses.html', context=context)