forked from bcgov/lear
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
11790 Add jupyter notebook for backfilling lear user data from auth (b…
…cgov#1489) * 11790 Add jupyter notebook for backfilling lear user data from auth * 11850 Added missing param
- Loading branch information
Showing
2 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Backfill Lear User Data from Auth\n", | ||
"This notebook updates entries in the `users` table in lear db where first/last name are missing with data from the auth api." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Setup environment\n", | ||
"Before running following environment setup snippets, ensure environment variables found in `default-bcr-business-setup-TEST` notebook contain the correct values." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%run /workspaces/lear/tests/data/default-bcr-business-setup-TEST.ipynb" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%run /workspaces/lear/tests/data/common/legal_api_utils.ipynb\n", | ||
"%run /workspaces/lear/tests/data/common/auth_api_utils.ipynb" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import json\n", | ||
"from sqlalchemy import and_, or_\n", | ||
"from legal_api.models import User" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Input Values for filing\n", | ||
"Please update following values to appropriate values before running subsequent code snippets!!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"verify_ssl=True # set to False if using proxy to debug requests" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Tokens" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"auth_token = get_auth_token(verify_ssl)\n", | ||
"assert auth_token\n", | ||
"# auth_token" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Update Lear Users" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# get lear users with no first/last names and has a username\n", | ||
"query = db.session.query(User) \\\n", | ||
" .filter(\n", | ||
" or_(\n", | ||
" User.firstname == None,\n", | ||
" User.firstname == ''\n", | ||
" ),\n", | ||
" or_(\n", | ||
" User.lastname == None,\n", | ||
" User.lastname == ''\n", | ||
" ),\n", | ||
" User.username != None,\n", | ||
" User.username != ''\n", | ||
" )\n", | ||
"\n", | ||
"lear_users = query.all()\n", | ||
"assert(lear_users)\n", | ||
"lear_users_count = len(lear_users)\n", | ||
"print(f'lear users to process: {lear_users_count}')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# loop through lear users with missing info and make call to auth users endpoint. if valid response comes back, populate lear user\n", | ||
"# with missing info(first name, last name, email) where possible\n", | ||
"for lear_user in lear_users:\n", | ||
"\n", | ||
" try:\n", | ||
" user_name = lear_user.username\n", | ||
" r = get_user_by_username(auth_token, user_name, verify_ssl)\n", | ||
"\n", | ||
" if r.status_code != 200:\n", | ||
" raise Exception(f'error {r.status_code}, {r.text}')\n", | ||
"\n", | ||
" auth_user_dict = json.loads(r.text)\n", | ||
" first_name = auth_user_dict.get('firstname')\n", | ||
" last_name = auth_user_dict.get('lastname')\n", | ||
" email = auth_user_dict.get('email')\n", | ||
" if first_name or last_name or email:\n", | ||
" lear_user.firstname = first_name\n", | ||
" lear_user.lastname = last_name\n", | ||
" lear_user.email = email\n", | ||
" lear_user.save()\n", | ||
" else:\n", | ||
" raise Exception('no first name or last name provided by auth response')\n", | ||
"\n", | ||
" print(f'lear user user_name: {user_name} updated succeeded')\n", | ||
" except Exception as err:\n", | ||
" print(f'lear user user_name: {user_name} updated failed, {err}')\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |