Added error handling, send email to user if adding fails

This commit is contained in:
Ojakoo
2022-07-31 11:12:51 +03:00
parent 53c3acd39f
commit 3f6a719e9d
+13 -3
View File
@@ -125,7 +125,7 @@ def admin_send_email_signupees(list, subject, content):
send_email(to.email, subject, markdown.markdown(content), True) send_email(to.email, subject, markdown.markdown(content), True)
def add_to_mailinglist(email): def add_to_mailinglist(email: str):
try: try:
# get data # get data
SCOPES = ["https://www.googleapis.com/auth/admin.directory.group"] SCOPES = ["https://www.googleapis.com/auth/admin.directory.group"]
@@ -140,6 +140,16 @@ def add_to_mailinglist(email):
service = build("admin", "directory_v1", credentials=credentials) service = build("admin", "directory_v1", credentials=credentials)
service.members().insert(groupKey=GROUP_KEY, body={"email": email}).execute() service.members().insert(groupKey=GROUP_KEY, body={"email": email}).execute()
except HttpError as err: except HttpError as err:
logging.exception("Something fukd: {}".format(err.error_details)) # Already in list, do nothing
return err.error_details.message if err.status_code == 409:
pass
# Something went wrong, send notification to maintainer.
else:
logging.exception("Failed adding user to list")
to = "ilari.ojakorpi@sahkoinsinoorikilta.fi"
subject = "Web error: Failed adding to google groups"
body = "Error code: {}\nError details: {}\nEmail that was not added: {}\n\nAdd user manually to jäsenet groups.".format(err.status_code, err.error_details, email)
send_email(to, subject, body)