Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
update email feedback and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed May 2, 2016
1 parent 1dce7c5 commit 979703b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
Binary file modified _unittests/ut_automation_students/data/groupes_eleves_pitch.xlsx
Binary file not shown.
25 changes: 21 additions & 4 deletions _unittests/ut_automation_students/test_pitch_automatique.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@

from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder
from src.ensae_teaching_cs.automation_students import enumerate_feedback
from src.ensae_teaching_cs.automation_students import enumerate_feedback, enumerate_send_email


class TestFeedback(unittest.TestCase):
Expand All @@ -118,13 +118,30 @@ def test_enumerate_feedback(self):
fLOG("------------", i)
name = os.path.join(temp, "m%d.html" % i)
with open(name, "w", encoding="utf-8") as f:
f.write(
'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>\n')
f.write(m[1])
f.write("\n</body></html>")
if i < len(exp):
assert exp[i] in m[1]

def test_enumerate_send_email(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

data = os.path.abspath(os.path.dirname(__file__))
data = os.path.join(data, "data")
xls = os.path.join(data, "groupes_eleves_pitch.xlsx")
mailbox = None # pymmails.sender.create_smtp_server("gmail", login, pwd)
df = pandas.read_excel(xls, sheetname=0, index=False)
comment = pandas.read_excel(xls, sheetname=1, header=None, index=False)
mails = list(enumerate_send_email(mailbox, "subject", "me", df, comment, exc=False, fLOG=fLOG, delay_sending=True))
for i, m in enumerate(mails):
fLOG("------------", m)
try:
m()
except AttributeError:
continue


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion src/ensae_teaching_cs/automation_students/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from .projects_repository import ProjectsRepository
from .mail_helper import grab_addresses, extract_students_mail_and_name_from_gmail
from .projects_helper import extract_students_mails_from_gmail_and_stores_in_folders
from .send_feedback import enumerate_feedback
from .send_feedback import enumerate_feedback, enumerate_send_email
46 changes: 42 additions & 4 deletions src/ensae_teaching_cs/automation_students/send_feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
@brief Some automation helpers to grab mails from students about projects.
"""

import time
import random
from pyquickhelper.loghelper import noLOG
from pyquickhelper.texthelper.templating import apply_template
from pymmails.sender import send_email


template_mail_feedback = """
{{ begin }}
Expand All @@ -28,7 +32,7 @@ def enumerate_feedback(df1, df2, col_group="Groupe", col_subject="Sujet",
col_pitch="Pitch", col_code="Code", col_mail="Mail",
col_name="Name", subject="Projet informatique, feedback sur le pitch",
begin="Bonjour,\n\nVoici mon feedback sur votre pitch. Ce mail est automatisé. Veuillez vérifier les informations.\n\n",
end="Xavier", text_comments="Remarques générales", cc=None,
end="Xavier", text_comments="Remarques générales",
template=template_mail_feedback, engine="jinja2", exc=True, fLOG=noLOG):
"""
sends feedback to students
Expand All @@ -43,12 +47,11 @@ def enumerate_feedback(df1, df2, col_group="Groupe", col_subject="Sujet",
@param col_name name of the column which contains the names of the members
@param subject subject of the mail
@param intro beginning of the mail
@param cc list of ccs
@param template template of the mail
@param text_comments sentance before the general comments
@param engine engine for the template
@param exc raise an exception if there is no mail
@return list of mails
@return enumerate mails content as tuple *(mail, html, text)*
Example of dataframe containing feedback:
Expand Down Expand Up @@ -114,4 +117,39 @@ def sums2(spl):
raise ValueError("No mail for:\n" + text)
else:
fLOG("No mail for:\n" + text)
yield (mail, text)
html = ('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>\n' + text + "\n</body></html>\n")
text = text.replace("<b>", "").replace("</b>", "").replace("<br />", "\n")
yield (mail, html, text)


def enumerate_send_email(mailbox, subject, fr, df1, df2, cc=None, delay=[500, 1000], delay_sending=False, exc=True, **params):
"""
Send feedback to students
@param mailbox mailbox, see `create_smtp_server <http://www.xavierdupre.fr/app/pymmails/helpsphinx/pymmails/sender/email_sender.html?pymmails.sender.email_sender.create_smtp_server>`_
@param fr from
@param df1 first dataframe
@param df2 a draframe or a list or a list of general comments to add at the end
@param cc additional receivers
@param delay random delay between two mails
@param delay_sending returns functions
@param exc raise exception when mail is empty
@param params see @see fn enumerate_feedback
@return enumerate mails
"""
for mails, html, text in enumerate_feedback(df1, df2, exc=exc, **params):
if mails is None or "@" not in mails:
# if there is an issue, it should been cautch by the previous function (we skip)
continue
res = send_email(mailbox, fr=fr, to=mails.split(";"), cc=cc, delay_sending=delay_sending,
body_html=html, body_text=text, subject=subject)
if delay_sending:
def delay():
res()
rnd = random.randint(*delay)
time.sleep(rnd)
yield delay
else:
yield res
rnd = random.randint(*delay)
time.sleep(rnd)

0 comments on commit 979703b

Please sign in to comment.