import pandas as pd from jinja2 import Template from datetime import date, timedelta from djagitit.mailing import smtp from djagitit.mailing import html import config import utils def build_email(email, distro_df, updated_playlists_df, tracks_df, no_news_df, mode, is_draft): add_form_link = config.Params.add_form_link rm_form_link = config.Params.rm_form_link logo_sme = html.Logos.sonymusic_redwhite() logo_spotify = html.Logos.spotify() current_date = date.today().strftime('%m-%d') offset = (date.today().weekday() - 4) % 7 or 7 last_friday = (date.today()-timedelta(days=offset)).strftime('%m-%d') tmp_distro_df = distro_df[distro_df['email'] == email] tmp_updated_playlists_df = pd.merge(tmp_distro_df, updated_playlists_df, on='playlist_id', how='inner') tmp_no_news_df = pd.merge(tmp_distro_df, no_news_df, on='playlist_id', how='inner') tmp_tracks_df = pd.merge(tmp_distro_df, tracks_df, on='playlist_id', how='inner') email_html = Template( open('../html/template.html').read() ).render( is_draft=is_draft, mode=mode, report_date=current_date, last_friday=last_friday, logo_sme=logo_sme, logo_spotify=logo_spotify, updated_playlists = tmp_updated_playlists_df, tracks = tmp_tracks_df, no_news = tmp_no_news_df, add_form_link = add_form_link, rm_form_link = rm_form_link ) return email_html def send_email(email, email_html, mode): subject = utils.get_subject(mode) sender = config.emailParams.sender sender_alias = config.emailParams.sender_alias reply_to = config.emailParams.reply_to bcc = config.emailParams.bcc sending_status = smtp.send_newsletter( subject=subject, html=email_html, sender=sender, sender_alias=sender_alias, reply_to=reply_to, bcc=bcc, to=email ) return sending_status