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 get_hackathons(): sheets_client = sheets.Sheets( spreadsheet_id=app.config["GOOGLE_SHEET_ID"], cred_file=app.config["GOOGLE_APPLICATION_CREDENTIALS"], ) try: hackathons = {h.name: h.label for h in sheets_client.get_hackathons()} except sheets.SheetError as ex: app.logger.error(ex, exc_info=True) hackathons = [""] return flask.jsonify(hackathons)
def auth(auth_code): sheets_client = sheets.Sheets( spreadsheet_id=app.config["GOOGLE_SHEET_ID"], cred_file=app.config["GOOGLE_APPLICATION_CREDENTIALS"], ) 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, ) user = auth_service.auth_user(auth_code) response = flask.make_response(flask.redirect("http://localhost:3000/")) if user: response.set_cookie("looker_hackathon_auth", auth_service.get_user_auth_code(user)) return response
def user_info(): response = {} if "looker_hackathon_auth" not in flask.request.cookies: return response auth_code = flask.request.cookies["looker_hackathon_auth"] sheets_client = sheets.Sheets( spreadsheet_id=app.config["GOOGLE_SHEET_ID"], cred_file=app.config["GOOGLE_APPLICATION_CREDENTIALS"], ) 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, ) user = auth_service.auth_user(auth_code) if user: response["first_name"] = user.first_name response["last_name"] = user.last_name return response
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