def main(filename: str, hackathon: str, limit: int): registrants = csv.DictReader(filename) count = 0 for registrant in registrants: registrant["hackathon"] = hackathon click.secho(f"Registering {registrant['email']}", fg="green") try: looker.register_user( hackathon=hackathon, first_name=registrant["first_name"], last_name=registrant["last_name"], email=registrant["email"], ) except looker.RegisterError as ex: click.secho( f"Failed to add to Looker. Stopping after {count} users", fg="red" ) raise ex count += 1 if limit and count == int(limit): break time.sleep(60) click.secho(f"Registered {count} users", fg="green")
def main(filename: str, hackathon: str, enable: bool, limit: int): f = open(filename) registrants = csv.DictReader(f) sheets_client = sheets.Sheets( spreadsheet_id=os.environ["GOOGLE_SHEET_ID"], cred_file=os.environ["GOOGLE_APPLICATION_CREDENTIALS"], ) count = 0 for registrant in registrants: registrant["hackathon"] = hackathon click.secho(f"Registering {registrant['email']}", fg="green") register_user = sheets.RegisterUser(**registrant) try: sheets_user = sheets_client.register_user(register_user) except sheets.SheetError as ex: click.secho( f"Failed to add to sheet. Stopping after {count} users", fg="red") f.close() raise ex else: try: client_id = looker.register_user( hackathon=hackathon, first_name=register_user.first_name, last_name=register_user.last_name, email=register_user.email, ) except looker.RegisterError as ex: f.close() click.secho( f"Failed to add to Looker. Stopping after {count} users", fg="red") raise ex sheets_user.client_id = client_id sheets_client.users.save(sheets_user) count += 1 if limit and count == int(limit): break click.secho(f"Registered {count} users", fg="green") if enable: for email, reset in looker.enable_users_by_hackathons([hackathon ]).items(): sheets_user = sheets_client.users.find(email) if not sheets_user: click.secho(f"Failed to find {email} in spreadsheet", fg="red") continue sheets_user.setup_link = reset sheets_client.users.save(sheets_user) time.sleep(1) click.secho(f"Enabled {count} users", fg="green") f.close()
def test_enable_users_by_hackathons( looker_test_users: List[sheets.User], sdk: methods.LookerSDK ): test_user1, test_user2, test_user3, test_user4 = looker_test_users looker.register_user( hackathon="hack_1", first_name=test_user1.first_name, last_name=test_user1.last_name, email=test_user1.email, ) looker.register_user( hackathon="hack_2", first_name=test_user2.first_name, last_name=test_user2.last_name, email=test_user2.email, ) looker.register_user( hackathon="hack_1", first_name=test_user3.first_name, last_name=test_user3.last_name, email=test_user3.email, ) looker.register_user( hackathon="hack_2", first_name=test_user4.first_name, last_name=test_user4.last_name, email=test_user4.email, ) assert sdk.search_users(fields="is_disabled", email=test_user1.email)[0].is_disabled assert sdk.search_users(fields="is_disabled", email=test_user2.email)[0].is_disabled assert sdk.search_users(fields="is_disabled", email=test_user3.email)[0].is_disabled assert sdk.search_users(fields="is_disabled", email=test_user4.email)[0].is_disabled looker.enable_users_by_hackathons(hackathons=["hack_1", "hack_2"]) assert not sdk.search_users(fields="is_disabled", email=test_user1.email)[ 0 ].is_disabled assert not sdk.search_users(fields="is_disabled", email=test_user2.email)[ 0 ].is_disabled assert not sdk.search_users(fields="is_disabled", email=test_user3.email)[ 0 ].is_disabled assert not sdk.search_users(fields="is_disabled", email=test_user4.email)[ 0 ].is_disabled
def register() -> Any: form = RegistrationForm() if not form.validate_on_submit(): errors = {} for field, field_errors in form.errors.items(): if field == "csrf_token": field = "validation" field_errors = ["Form is invalid"] errors[field] = ", ".join(field_errors) return { "ok": False, "message": "; ".join(f"{k}: {v}" for k, v in errors.items()), } response = {"ok": True, "message": "Congratulations!"} hackathon = form.data["hackathon"] first_name = form.data["first_name"] last_name = form.data["last_name"] email = form.data["email"] email_verified = form.data["email_verified"] register_user = sheets.RegisterUser( hackathon=hackathon, first_name=first_name, last_name=last_name, email=email, organization=form.data["organization"], role=form.data["role"], tshirt_size=form.data["tshirt_size"], ) sheets_client = sheets.Sheets( spreadsheet_id=app.config["GOOGLE_SHEET_ID"], cred_file=app.config["GOOGLE_APPLICATION_CREDENTIALS"], ) try: sheets_user = sheets_client.register_user(register_user) except sheets.SheetError as ex: app.logger.error(ex, exc_info=True) response = { "ok": False, "message": "There was a problem, try again later." } else: try: client_id = looker.register_user( hackathon=hackathon, first_name=first_name, last_name=last_name, email=email, ) except looker.RegisterError as ex: app.logger.error(ex, exc_info=True) response = { "ok": False, "message": "There was a problem, try again later.", } else: try: sheets_user.client_id = client_id sheets_client.users.save(sheets_user) except sheets.SheetError as ex: app.logger.error(ex, exc_info=True) response = { "ok": False, "message": "There was a problem, try again later.", } resp = flask.jsonify(response) if response["ok"]: auth_service = authentication.Authentication.configure( crypto_key=app.config["SECRET_KEY"], from_email=app.config["FROM_EMAIL"], email_key=app.config["SENDGRID_API_KEY"], sheet=sheets_client, ) if email_verified: resp.set_cookie("looker_hackathon_auth", auth_service.get_user_auth_code(sheets_user)) else: auth_service.send_auth_message(sheets_user, flask.request.host_url) return resp
def test_register_user( looker_test_users: List[sheets.User], sdk: methods.LookerSDK, register_twice: bool ): test_hackathon = "Some Hackathon" test_user = looker_test_users[0] looker.register_user( hackathon=test_hackathon, first_name=test_user.first_name, last_name=test_user.last_name, email=test_user.email, ) if register_twice: looker.register_user( hackathon=test_hackathon, first_name=test_user.first_name, last_name=test_user.last_name, email=test_user.email, ) users = sdk.search_users(email=test_user.email) assert len(users) > 0 actual_user = users[0] assert actual_user.first_name == test_user.first_name assert actual_user.last_name == test_user.last_name assert actual_user.credentials_email assert actual_user.credentials_api3 assert len(actual_user.credentials_api3) == 1 assert actual_user.group_ids assert len(actual_user.group_ids) == 2 assert actual_user.is_disabled groups = sdk.all_groups(ids=models.DelimSequence(actual_user.group_ids)) for group in groups: if group.name == f"Looker_Hack: {test_hackathon}": break else: pytest.fail(f"Failed to find or create 'Looker_Hack: {test_hackathon}'") for role in sdk.all_roles(fields="name,id"): if role.name == "Hackathon": break else: pytest.fail("Bad test setup, failed to find 'Hackathon' role") assert role.id role_groups = sdk.role_groups(role_id=role.id, fields="id") for role_group in role_groups: if role_group.id == group.id: break else: pytest.fail( f"Failed to assign group 'Looker_Hack: {test_hackathon}' to role 'Hackathon'" ) assert actual_user.id actual_attributes = sdk.user_attribute_user_values(user_id=actual_user.id) assert actual_attributes for actual_attribute in actual_attributes: if actual_attribute.name == "hackathon": assert actual_attribute.value == test_hackathon break else: assert False, "Not assigned hackathon role"