Add mentor query

This commit is contained in:
Guilhem Lavaux 2024-08-20 15:51:52 +02:00
parent 65b79bb3a4
commit 7e94a15a05

View File

@ -27,19 +27,11 @@ Best regards,
url = "https://infinity-users.projet-horizon.fr/index.php" url = "https://infinity-users.projet-horizon.fr/index.php"
@click.group()
def main():
pass
@click.command() def get_data():
@click.argument("to_bug")# help="The login of the user to bug")
def main(to_bug):
"""Bug a user on the Infinity platform.
Args:
to_bug (str): The login of the user to bug.
"""
if to_bug is None:
print("Please provide the login of the user to bug")
raise SystemExit(1)
with requests.session() as s: with requests.session() as s:
response = s.post(url, data={"authpw": "astrorizon"}) response = s.post(url, data={"authpw": "astrorizon"})
if response.status_code != 200: if response.status_code != 200:
@ -86,37 +78,64 @@ def main(to_bug):
# Print the parsed data # Print the parsed data
mapped_login = {user["login"]: user for user in users_tbl} mapped_login = {user["login"]: user for user in users_tbl}
return mapped_login
# Get the current unix login of the user using python os @main.command()
current_login = os.getlogin() @click.argument("user")
def sponsor(user):
if user is None:
print("Please provide the login of the user to bug")
raise SystemExit(1)
if not current_login in mapped_login: mapped_login = get_data()
print(f"User {current_login} not found")
raise SystemExit(1) u = mapped_login[user]
from_user = mapped_login[current_login]
if not to_bug in mapped_login:
print(f"User {to_bug} not found")
raise SystemExit(1)
to_user = mapped_login[to_bug]
email_body = email_template.format( print(u["mentor"])
name=to_user["name"], complain_name=from_user["name"] print(mapped_login[u["mentor"]]["name"])
)
# Create a multipart message @main.command()
msg = MIMEMultipart() @click.argument("to_bug")# help="The login of the user to bug")
msg["From"] = from_user["email"] def go(to_bug):
msg["To"] = to_user["email"] """Bug a user on the Infinity platform.
msg["CC"] = from_user["email"]
msg["Subject"] = "Your usage of Infinity nodes" Args:
msg.attach(MIMEText(email_body, "plain")) to_bug (str): The login of the user to bug.
"""
if to_bug is None:
print("Please provide the login of the user to bug")
raise SystemExit(1)
mapped_login = get_data()
# Get the current unix login of the user using python os
current_login = os.getlogin()
text = msg.as_string() if not current_login in mapped_login:
print(textwrap.dedent(f""" print(f"User {current_login} not found")
raise SystemExit(1)
from_user = mapped_login[current_login]
if not to_bug in mapped_login:
print(f"User {to_bug} not found")
raise SystemExit(1)
to_user = mapped_login[to_bug]
email_body = email_template.format(
name=to_user["name"], complain_name=from_user["name"]
)
# Create a multipart message
msg = MIMEMultipart()
msg["From"] = from_user["email"]
msg["To"] = to_user["email"]
msg["CC"] = from_user["email"]
msg["Subject"] = "Your usage of Infinity nodes"
msg.attach(MIMEText(email_body, "plain"))
text = msg.as_string()
print(textwrap.dedent(f"""
Here is the draft of the email that is going to be sent: Here is the draft of the email that is going to be sent:
----------------------------------------------------- -----------------------------------------------------
{text} {text}
@ -124,15 +143,15 @@ Here is the draft of the email that is going to be sent:
If you agree with it, please enter your login name to confirm the sending of the email. If you agree with it, please enter your login name to confirm the sending of the email.
""")) """))
if input("Do you want to send the email? ") != current_login:
print("Email not sent")
raise SystemExit(0)
print("Sending email...") if input("Do you want to send the email? ") != current_login:
server = smtplib.SMTP('smtp.iap.fr', 587) print("Email not sent")
server.sendmail(from_user["email"], [to_user["email"], from_user["email"]], text) raise SystemExit(0)
server.quit()
print("Sending email...")
server = smtplib.SMTP('smtp.iap.fr', 587)
server.sendmail(from_user["email"], [to_user["email"], from_user["email"]], text)
server.quit()
if __name__ == "__main__": if __name__ == "__main__":
main() main()