def authenticate(request): """ Get credentials from Google using code from client, and then check if the user already exists in ndb. """ try: oauth_flow = OAuth2WebServerFlow( client_id=settings.GOOGLE_CLIENT['web']['client_id'], client_secret=settings.GOOGLE_CLIENT['web']['client_secret'], auth_uri=settings.GOOGLE_CLIENT['web']['auth_uri'], token_uri=settings.GOOGLE_CLIENT['web']['token_uri'], redirect_uri='postmessage', scope='openid email', ) credentials = json.loads(oauth_flow.step2_exchange(request.body).to_json()) except FlowExchangeError: return HttpResponse('{"result":"failure"}', content_type='application/json') else: user = User.get_by_id(credentials['id_token']['sub']) if not user: user = User( id = credentials['id_token']['sub'], email = credentials['id_token']['email'], refresh_token = credentials.get('refresh_token')) user.put() try: uid = user.key.id() session = label_api.create_session(user_id=uid, app_id=uid, device_id=uid) session_id = session.get('session_id') if not session_id: raise Exception # Must set profile before adding ingredients response = label_api.set_profile(session_id, user.get_profile()) if response.get('result') != 'success': raise Exception for label in Label.query(Label.user_id == uid, Label.sub_id != '').fetch(): label_api.add_ingredient(session_id, label.sub_id) response = HttpResponse(json.dumps({ "success": True, "euid": crypto.encrypt(uid) }), content_type='application/json') response.set_signed_cookie('session_id', session_id) return response except: pass return HttpResponse('{"success": false}', content_type='application/json')
def add_ingredient(request): """ Adds an ingredient to a user's ingredient list """ try: session_id = request.get_signed_cookie('session_id', default=None) user_id = crypto.decrypt(request.COOKIES.get('euid', '')) ingredient_id = request.POST.get('ingredient_id', '') ingredient_name = request.POST.get('ingredient_name', '') ingredient = Label.query( Label.user_id == user_id, Label.name == ingredient_name, Label.sub_id == ingredient_id).get(keys_only=True) if session_id and not ingredient: response = label_api.add_ingredient(session_id, ingredient_id) if response.get('result') == 'success': Label(user_id=user_id, name=ingredient_name, sub_id=ingredient_id).put_async() return HttpResponse('{"result": "success"}', content_type='application/json') except: pass return HttpResponse('{"result": "failure"}', content_type='application/json')
def authenticate(request): """ Get credentials from Google using code from client, and then check if the user already exists in ndb. """ try: oauth_flow = OAuth2WebServerFlow( client_id=settings.GOOGLE_CLIENT['web']['client_id'], client_secret=settings.GOOGLE_CLIENT['web']['client_secret'], auth_uri=settings.GOOGLE_CLIENT['web']['auth_uri'], token_uri=settings.GOOGLE_CLIENT['web']['token_uri'], redirect_uri='postmessage', scope='openid email', ) credentials = json.loads( oauth_flow.step2_exchange(request.body).to_json()) except FlowExchangeError: return HttpResponse('{"result":"failure"}', content_type='application/json') else: user = User.get_by_id(credentials['id_token']['sub']) if not user: user = User(id=credentials['id_token']['sub'], email=credentials['id_token']['email'], refresh_token=credentials.get('refresh_token')) user.put() try: uid = user.key.id() session = label_api.create_session(user_id=uid, app_id=uid, device_id=uid) session_id = session.get('session_id') if not session_id: raise Exception # Must set profile before adding ingredients response = label_api.set_profile(session_id, user.get_profile()) if response.get('result') != 'success': raise Exception for label in Label.query(Label.user_id == uid, Label.sub_id != '').fetch(): label_api.add_ingredient(session_id, label.sub_id) response = HttpResponse(json.dumps({ "success": True, "euid": crypto.encrypt(uid) }), content_type='application/json') response.set_signed_cookie('session_id', session_id) return response except: pass return HttpResponse('{"success": false}', content_type='application/json')