def send_salary_sheet(request, email, company_id, filename, filepath, force=False, message=None, subject=None): """ Send a salarysheet to the given company's e-mail :param obj request: A pyramid request object :param str company_mail: The mail to send it to :param int company_id: The id of the associated company :param str filepath: The path to the filename :param bool force: Whether to force sending this file again :param str message: The mail message :param str subject: The mail subject :returns: A MailHistory instance :TypeError UndeliveredMail: When the company has no mail :TypeError MailAlreadySent: if the file has already been sent and no force option was passed """ filebuf = file(filepath, 'r') filedatas = filebuf.read() if not force and check_if_mail_sent(filedatas, company_id): log.warn(u"Mail already sent : mail already sent") raise MailAlreadySent(u"Mail already sent") filebuf.seek(0) if email is None: log.warn( u"Undelivered email : no mail provided for company {0}".format( company_id)) raise UndeliveredMail( u"no mail provided for company {0}".format(company_id)) else: log.info('Sending the file %s' % filepath) log.info("Sending it to %s" % email) attachment = Attachment(filename, "application/pdf", filebuf) subject = subject or SALARYSHEET_MAIL_SUBJECT message = message or SALARYSHEET_MAIL_MESSAGE send_mail( request, email, message, subject, attachment, ) return store_sent_mail(filepath, filedatas, company_id)
def test_mail_treasury_files(dbsession, config, get_csrf_request, company_125): request = get_csrf_request() request.matchdict = {'filetype': 'salaire', 'year': '2010', 'month': '1'} view = MailTreasuryFilesView(None, request) result_dict = view() datas = result_dict['datas'] assert len(datas.keys()) == 1 for file_ in datas.values()[0]: assert file_['file'].code == file_['company'].code_compta form_datas = { 'force': False, 'mails': [ { 'company_id': company_125.id, 'attachment': '125_1_test.pdf' }, { 'company_id': company_125.id, 'attachment': '125_2_test.pdf' }, ], 'mail_subject': u"Sujet", "mail_message": u"Message {company.email} {year} {month}", } mails = view._prepare_mails(datas, form_datas, get_root_directory(request), '2010', '1') assert len(mails) == 2 assert mails[0]['message'] == u"Message [email protected] 2010 1" assert mails[0]['email'] == '*****@*****.**' sent_file = datas[company_125.id][0]['file'] from autonomie.models.files import store_sent_mail history = store_sent_mail(sent_file.path, sent_file.datas, company_125.id) dbsession.add(history) # Not force and already in history form_datas = { 'force': False, 'mails': [ { 'company_id': company_125.id, 'attachment': '125_0_test.pdf' }, { 'company_id': company_125.id, 'attachment': '125_1_test.pdf' }, { 'company_id': company_125.id, 'attachment': '125_2_test.pdf' }, { 'company_id': company_125.id, 'attachment': '125_3_test.pdf' }, ], 'mail_subject': u"Sujet", "mail_message": u"Message {company.email} {year} {month}", } mails = view._prepare_mails(datas, form_datas, get_root_directory(request), '2010', '1') assert len(mails) == 3 # Force and already in history form_datas = { 'force': True, 'mails': [ { 'company_id': company_125.id, 'attachment': '125_0_test.pdf' }, { 'company_id': company_125.id, 'attachment': '125_1_test.pdf' }, { 'company_id': company_125.id, 'attachment': '125_2_test.pdf' }, { 'company_id': company_125.id, 'attachment': '125_3_test.pdf' }, ], 'mail_subject': u"Sujet", "mail_message": u"Message {company.email} {year} {month}", } mails = view._prepare_mails(datas, form_datas, get_root_directory(request), '2010', '1') assert len(mails) == 4 # Invalid submitted datas form_datas = { 'force': True, 'mails': [ { 'company_id': -15, 'attachment': '125_3_test.pdf' }, ], 'mail_subject': u"Sujet", "mail_message": u"Message {company.email} {year} {month}", } with pytest.raises(Exception): mails = view._prepare_mails(datas, form_datas, get_root_directory(request), '2010', '1')
def test_mail_treasury_files(dbsession, config, get_csrf_request, company_125): request = get_csrf_request() request.matchdict = {'filetype': 'salaire', 'year': '2010', 'month': '1'} view = MailTreasuryFilesView(None, request) result_dict = view() datas = result_dict['datas'] assert len(datas.keys()) == 1 for file_ in datas.values()[0]: assert file_['file'].code == file_['company'].code_compta form_datas = { 'force': False, 'mails': [ {'company_id': company_125.id, 'attachment': '125_1_test.pdf'}, {'company_id': company_125.id, 'attachment': '125_2_test.pdf'}, ], 'mail_subject': u"Sujet", "mail_message": u"Message {company.email} {year} {month}", } mails = view._prepare_mails(datas, form_datas, get_root_directory(request), '2010', '1') assert len(mails) == 2 assert mails[0]['message'] == u"Message [email protected] 2010 1" assert mails[0]['email'] == '*****@*****.**' sent_file = datas[company_125.id][0]['file'] from autonomie.models.files import store_sent_mail history = store_sent_mail(sent_file.path, sent_file.datas, company_125.id) dbsession.add(history) # Not force and already in history form_datas = { 'force': False, 'mails': [ {'company_id': company_125.id, 'attachment': '125_0_test.pdf'}, {'company_id': company_125.id, 'attachment': '125_1_test.pdf'}, {'company_id': company_125.id, 'attachment': '125_2_test.pdf'}, {'company_id': company_125.id, 'attachment': '125_3_test.pdf'}, ], 'mail_subject': u"Sujet", "mail_message": u"Message {company.email} {year} {month}", } mails = view._prepare_mails( datas, form_datas, get_root_directory(request), '2010', '1') assert len(mails) == 3 # Force and already in history form_datas = { 'force': True, 'mails': [ {'company_id': company_125.id, 'attachment': '125_0_test.pdf'}, {'company_id': company_125.id, 'attachment': '125_1_test.pdf'}, {'company_id': company_125.id, 'attachment': '125_2_test.pdf'}, {'company_id': company_125.id, 'attachment': '125_3_test.pdf'}, ], 'mail_subject': u"Sujet", "mail_message": u"Message {company.email} {year} {month}", } mails = view._prepare_mails( datas, form_datas, get_root_directory(request), '2010', '1') assert len(mails) == 4 # Invalid submitted datas form_datas = { 'force': True, 'mails': [ {'company_id': -15, 'attachment': '125_3_test.pdf'}, ], 'mail_subject': u"Sujet", "mail_message": u"Message {company.email} {year} {month}", } with pytest.raises(Exception): mails = view._prepare_mails( datas, form_datas, get_root_directory(request), '2010', '1')