def register_with_google(self, json_data): google_response = urllib.request.urlopen( "https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=" + json_data["access_token"]).read() google_response_json = json.loads(google_response.decode("UTF-8")) google_profile_response_json = json.loads( urllib.request.urlopen( "https://www.googleapis.com/oauth2/v3/userinfo?access_token=" + json_data["access_token"]).read().decode('utf-8').replace( '\n', '')) account = Account.query.filter_by( email=google_response_json['email']).first() if account: return { "status": "failed", "message": get_request_message(request, "EXISTENT_ACCOUNT") }, 409 if (json_data["is_artist"]): artist = Artist.query.filter_by( artistic_name=json_data["artistic_name"]).first() if artist: return { "status": "failed", "message": get_request_message(request, "EXISTENT_ARTIST") }, 409 account = Account( email=google_response_json['email'], password=hashlib.sha512( "googleaccountpass_OXI8SV7E".encode()).hexdigest(), name=google_profile_response_json['given_name'], last_name=google_profile_response_json['family_name'], creation_date=date.today().strftime("%Y/%m/%d")) database.session.add(account) database.session.commit() result = account_schema.dump(account).data if (json_data["is_artist"]): artist = Artist(result["account_id"], json_data["artistic_name"]) database.session.add(artist) database.session.commit() return {"status": "success", "data": result}, 201
def post(self): current_app.logger.info('Processing Account POST') json_data = request.get_json(force=True) if not json_data: current_app.logger.error('No input data given to Account POST') return {'message': 'No input data provided', 'error': 'true'}, 400 # Validate and deserialize input data, errors = account_schema.load(json_data) if errors: current_app.logger.error('Bad data given to Account POST') return errors, 422 # account = Account.query.filter_by(name=data['name']).first() # if account: # return {'message': 'Account already exists', 'error': 'true'}, 400 account = Account(name=json_data['name'], plan_level=json_data['plan_level']) db.session.add(account) db.session.commit() result = account_schema.dump(account).data current_app.logger.info('Successful Account POST') return {"status": 'success', 'data': result}, 201
def post(self, request_type, method=None): json_data = request.get_json() if not json_data: return { "status": "failed", "message": get_request_message(request, "NO_INPUT_DATA_PROVIDED") }, 400 if request_type == "login": if method and method == "google": return self.login_with_google(json_data) account = Account.query.filter_by( email=json_data["email"], password=hashlib.sha512( json_data["password"].encode()).hexdigest()).first() if not account: return { "status": "failed", 'message': get_request_message(request, "NON_EXISTENT_ACCOUNT") }, 422 access_token = jwt.encode( { "account_id": account.account_id, "exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1) }, config.SECRET_KEY) return { "status": "success", "data": account_schema.dump(account).data, "access_token": access_token.decode("UTF-8") }, 200 elif request_type == "register": if method and method == "google": return self.register_with_google(json_data) data, errors = account_schema.load(json_data) if errors: return errors, 422 account = Account.query.filter_by(email=data['email']).first() if account: return { "status": "failed", 'message': get_request_message(request, "EXISTENT_ACCOUNT") }, 409 if (json_data["is_artist"]): artist = Artist.query.filter_by( artistic_name=json_data["artistic_name"]).first() if artist: return { "status": "failed", 'message': get_request_message(request, "EXISTENT_ARTIST") }, 409 account = Account(email=data['email'], password=hashlib.sha512( data['password'].encode()).hexdigest(), name=data['name'], last_name=data['last_name'], creation_date=date.today().strftime("%Y/%m/%d")) database.session.add(account) database.session.commit() result = account_schema.dump(account).data if (json_data["is_artist"]): artist = Artist(result["account_id"], json_data["artistic_name"]) database.session.add(artist) database.session.commit() return {"status": "success", "data": result}, 201 else: return { "status": "failed", "message": get_request_message(request, "INVALID_REQUEST_PROVIDED") }, 400