Commit 4ddbddd7 authored by Matthieu Boileau's avatar Matthieu Boileau
Browse files

Address #57

parent f9c32fb3
Pipeline #3693 failed
......@@ -72,3 +72,12 @@ update-issue:
script:
- echo "Updating issue \#49"
- python3 utils/follow_transfer.py --issue
notify-job-offer-author:
only:
- master
- develop
variables:
- $CI_COMMIT_MESSAGE =~ /^Adding new job offer job_.*$/
script:
- python3 utils/notify_job_offer_author.py --commit_message "$CI_COMMIT_MESSAGE" --password $SENDER_PASSWORD
#!/usr/bin/env python3
"""Email belenios vote credentials to voters"""
import argparse
import smtplib
from email.message import EmailMessage
from email.headerregistry import Address
from string import Template
import getpass
import os
import re
import sys
import datetime
import locale
locale.setlocale(locale.LC_TIME, '')
script_path = os.path.dirname(sys.argv[0])
sys.path.append(os.path.join(script_path, '..'))
from publishconf import SITEURL
# Email
SUBJECT = "Votre annonce d'offre d'emploi a été validée"
MAILSERVER = 'mailserver.u-strasbg.fr'
SENDER_ACCOUNT = 'math-gitlab-incoming'
SENDER_MAIL = "calcul-contact@math.cnrs.fr"
SENDER_NAME = "Bureau du groupe Calcul"
SENDER_ACCOUNT_DISPLAY = "calcul-contact"
SENDER_DOMAIN = "math.cnrs.fr"
class Bcolors:
"""For printing colored output to terminal"""
OKGREEN = '\033[32m'
WARNING = '\033[33m'
FAIL = '\033[31m'
ENDC = '\033[0m'
STATUS_OK = "{}{:<4}{}".format(Bcolors.OKGREEN, "OK", Bcolors.ENDC)
STATUS_WARN = "{}{:<4}{}".format(Bcolors.WARNING, "Warn", Bcolors.ENDC)
STATUS_KO = "{}{:<4}{}".format(Bcolors.FAIL, "KO", Bcolors.ENDC)
TEMPLATE = Template("""\
Bonjour,
Merci d'avoir déposé une annonce d'offre d'emploi sur le site web du groupe Calcul.
Cette annonce a été validée par le bureau du groupe Calcul et vient d'être :
- publiée sur $JOB_URL
- diffusée sur la liste calcul@listes.math.cnrs.fr.
Sans demande de votre part, elle restera en ligne jusqu'au $DATE_PUB.
Cordialement,
Le bureau du groupe Calcul.
""")
def send_email(commit_message, password=None, ):
"""Send notification email using smtplib"""
def prompt_for_password():
return getpass.getpass(f"Please type email password for {SENDER_MAIL}: ")
if not password:
password = prompt_for_password()
# Connect to SMTP
with smtplib.SMTP_SSL(MAILSERVER) as smtp:
while True:
try:
smtp.login(SENDER_ACCOUNT, password)
break
except smtplib.SMTPAuthenticationError:
print("Authentication error. Please retry.")
password = prompt_for_password()
d = {}
m = re.match("^Adding new job offer job_(.*)$", commit_message)
job_id = m.group(1)
d['JOB_URL'] = f"{SITEURL}/job_{job_id}.html"
filename = f"../content/job_offers/job_{job_id}.md"
filepath = os.path.join(script_path, filename)
with open(filepath) as f:
s = f.read()
# TODO: When expire_date will be in job_*.md file
# m = re.search("Date: (.*)$", s, re.MULTILINE)
# date = m.group(1)
#expire_date to format...
m = re.search("Authors: (.*)$", s, re.MULTILINE)
author_name = m.group(1)
m = re.search("Email: (.*)$", s, re.MULTILINE)
author_email = m.group(1)
expire_date = datetime.date.today() + datetime.timedelta(days=90)
d['DATE_PUB'] = '{d.day} {d:%B} {d.year}'.format(d=expire_date)
msg = EmailMessage()
msg['Subject'] = SUBJECT
msg['From'] = Address(SENDER_NAME, SENDER_ACCOUNT_DISPLAY, SENDER_DOMAIN)
msg['To'] = author_email
body = TEMPLATE.substitute(d)
msg.set_content(body)
print(body)
print(f">>> Sending notification to {author_name} <{author_email}>")
#smtp.sendmail(SENDER_MAIL, author_email, msg.as_string())
def main():
"""Read CLI to send email"""
parser = argparse.ArgumentParser(description="Send email notification to the author of job offer")
parser.add_argument('--commit_message', nargs=1, metavar='commit-message', required=True)
parser.add_argument('--password', nargs=1, metavar="account-password", help="password for sender email account")
args = parser.parse_args()
if args.password:
send_email(args.commit_message[0], password=args.password[0])
else:
send_email(args.job_url[0])
if __name__ == '__main__':
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment