def start_auth(request): """ begin the oAuth protocol with the server expects either a record_id or carenet_id parameter, now that we are carenet-aware """ # do we have a record_id? record_id = request.GET.get('record_id', None) carenet_id = request.GET.get('carenet_id', None) if _loggedIn(request, record_id, carenet_id): sessionkey = _sessionkey(record_id=record_id, carenet_id=carenet_id) url = "%s/demographics" % sessionkey return HttpResponseRedirect(url) # create the client to Indivo client = get_indivo_client(request, with_session_token=False) # prepare request token parameters params = {'oauth_callback': 'oob'} if record_id: params['indivo_record_id'] = record_id if carenet_id: params['indivo_carenet_id'] = carenet_id # request a request token req_token = client.fetch_request_token(params) # store the request token in the session for when we return from auth request.session['request_token'] = req_token # redirect to the UI server return HttpResponseRedirect(client.auth_redirect_url)
def after_auth(request): """ after Indivo authorization, exchange the request token for an access token and store it in the web session. """ # get the token and verifier from the URL parameters oauth_token, oauth_verifier = request.GET['oauth_token'], request.GET['oauth_verifier'] # retrieve request token stored in the session token_in_session = request.session['request_token'] # is this the right token? if token_in_session['oauth_token'] != oauth_token: return HttpResponse("oh oh bad token") # get the indivo client and use the request token as the token for the exchange client = get_indivo_client(request, with_session_token=False) client.update_token(token_in_session) access_token = client.exchange_token(oauth_verifier) record_id = carenet_id = None if access_token.has_key('xoauth_indivo_record_id'): record_id = access_token['xoauth_indivo_record_id'] else: carenet_id = access_token['xoauth_indivo_carenet_id'] # store stuff in the session session = dict() session['access_token'] = access_token session['carenet_id'] = carenet_id session['record_id'] = record_id sessionkey = _sessionkey(record_id=record_id, carenet_id=carenet_id) request.session[sessionkey] = session url = "%s/demographics" % sessionkey return HttpResponseRedirect(url)
def start_auth(request): """ begin the oAuth protocol with the server expects either a record_id or carenet_id parameter, now that we are carenet-aware """ # do we have a record_id? record_id = request.GET.get('record_id', None) carenet_id = request.GET.get('carenet_id', None) if _loggedIn(request, record_id, carenet_id): sessionkey = _sessionkey(record_id=record_id, carenet_id=carenet_id) url = "%s/demographics" % sessionkey return HttpResponseRedirect(url) # create the client to Indivo client = get_indivo_client(request, with_session_token=False) # prepare request token parameters params = {'oauth_callback':'oob'} if record_id: params['indivo_record_id'] = record_id if carenet_id: params['indivo_carenet_id'] = carenet_id # request a request token req_token = client.fetch_request_token(params) # store the request token in the session for when we return from auth request.session['request_token'] = req_token # redirect to the UI server return HttpResponseRedirect(client.auth_redirect_url)
def get(self, *args, **kwargs): """ This is a very limited version of the Manager.get(). It only retrieves a single record. """ # only two options supported record_id= or carenet_id= record_id = kwargs.get('record_id') carenet_id = kwargs.get('carenet_id') assert record_id or carenet_id request = get_request() sessionkey = _sessionkey(record_id, carenet_id) session = request.session.get(sessionkey) client = get_indivo_client(request, with_session_token=False, token=session['access_token']) # I am using JSON to get the data from the server. I could also use XML. # The content of the GET document is totally different from the content of the PUT document. # Therefore there is no possibility of streamlineing the PUT and the GET stages. if record_id: resp, content = client.read_demographics( record_id=record_id, body={"response_format": "application/json"}) if resp['status'] != '200': raise Exception("Error fetching demographics from record: %s" % record_id) else: resp, content = client.read_demographics_carenet( carenet_id=carenet_id, body={"response_format": "application/json"}) if resp['status'] != '200': raise Exception( "Error fetching demographics from carenet: %s" % carenet_id) data = json.loads(content)[0] o = self.model() o.carenet_id = carenet_id o.record_id = record_id if record_id: o._is_record = True o._is_carenet = False else: o._is_record = False o._is_carenet = True for k, v in data.items(): if k != '__documentid__': setattr(o, k, v) return o
def save(self): carenet_id = self.carenet_id record_id = self.record_id assert record_id != None # Problem - XML format required d = self.to_xml() sessionkey = _sessionkey(record_id, carenet_id) request = get_request() session = request.session.get(sessionkey) client = get_indivo_client(request, with_session_token=False, token=session['access_token']) if record_id: resp, content = client.set_demographics(record_id=record_id, body=d) if resp['status'] != '200': logger.error( "Error %s updating demographics from record (%s): %s\n%s" % (resp['status'], record_id, content, d.replace( "><", ">\n<"))) raise Exception( "Error %s updating demographics from record (%s): %s" % (resp['status'], record_id, content))