<commit_before>from collections import defaultdict
from contextlib import contextmanager
import os
import smtplib
import database as db

from email.mime.text import MIMEText

from jinja2 import Template
from database.model import Team
from geotools import openroute_link
from geotools.routing import MapPoint

from webapp.cfg import config


@contextmanager
def smtp_session():
    session = smtplib.SMTP(config.MAIL_SERVER, config.MAIL_PORT)
    if config.MAIL_USERNAME is not None:
        session.starttls()
        session.login(config.MAIL_USERNAME, config.MAIL_PASSWORD)

    try:
        yield session
    finally:
        session.quit()


def get_template(name):
    filename = "%s.txt" % name
    filepath = os.path.join(os.path.dirname(__file__), "templates", filename)
    if not os.path.isfile(filepath):
        raise Exception("File not found: %s!" % filepath)

    with open(filepath, "r") as fn:
        return Template(unicode(fn.read(), "utf8"))


def informal_to_teams(template_name, subject, debug=True):
    template = get_template(template_name)
    sender = "meet&eat Orga <%s>" % config.MAIL_DEFAULT_SENDER
    envelope = config.MAIL_DEFAULT_SENDER

    print "connect to SMTP...."
    with smtp_session() as session:
        print "Send Mails..."
        i = 0
        for team in db.session.query(Team).filter_by(deleted=False).filter_by(confirmed=True):
            content = template.render(name=team.name)
            msg = MIMEText(content, "plain", "utf8")

            rcpt = team.email
            if debug:
                rcpt = config.MAIL_DEFAULT_SENDER

            msg['Subject'] = subject
            msg['From'] = sender
            msg['To'] = rcpt

            session.sendmail(envelope, [rcpt] + ["<EMAIL>"], msg.as_string())
            i += 1
        print "Mails sent: %d" % i


def plans_to_teams(plan_results, debug=True):
    guestentry = """
%(time)s Uhr - %(roundname)s bei "%(host)s"
%(address)s
Klingeln bei: %(bell)s
Bei Problemen erreicht ihr dieses Team unter: %(phone)s
Routenlink: %(link)s"""

    hostentry = """
%(time)s Uhr - %(roundname)s wird von Euch zubereitet. (%(teamname)s)

zu Gast sind bei Euch:
%(guests)s"""

    hostguests = """
  Team "%(guestname)s"
  (Allergien: %(allergies)s)
  (Vegeratier dabei: %(vegetarians)s)
  Telefon: %(guestphone)s
"""
    subject = "Meet&Eat - Abendplanung Team %s"
    round_datas = ({"time": "18:00", "name": "Vorspeise"},
                   {"time": "20:00", "name": "Hauptgericht"},
                   {"time": "21:30", "name": "Dessert"})

    print "Preprocess plan..."
    guestmap = defaultdict(list)
    for team in plan_results:
        for host in plan_results[team]:
            if team != host:
                guestmap[host].append(team)

    template = get_template("plan")
    sender = "meet&eat Orga <%s>" % config.MAIL_DEFAULT_SENDER
    envelope = config.MAIL_DEFAULT_SENDER

    print "Fetch data..."
    teams = {}
    for team in db.session.query(Team).filter_by(deleted=False).filter_by(confirmed=True):
        teams[str(team.id)] = team

    i = 0
    print "Connect to smtp..."
    with smtp_session() as session:
        print "Send mails..."
        for team in plan_results:
            plan_detail = []
            start_point = MapPoint.from_team(teams[team])
            for (round_idx, host) in enumerate(plan_results[team]):
                round_data = round_datas[round_idx]
                end_point = MapPoint.from_team(teams[host])
                route = openroute_link([start_point, end_point])
                if team != host:
                    detail = guestentry % {"time": round_data["time"],
                                           "roundname": round_data["name"],
                                           "host": teams[host].name,
                                           "address": teams[host].location.street,
                                           "bell": teams[host].location.extra,
                                           "phone": teams[host].phone,
                                           "link": route}
                    plan_detail.append(detail)
                else:
                    guest_details = []
                    for guest in guestmap[team]:
                        guest_detail = hostguests % {"guestname": teams[guest].name,
                                                     "allergies": teams[guest].allergies,
                                                     "vegetarians": teams[guest].vegetarians,
                                                     "guestphone": teams[guest].phone}
                        guest_details.append(guest_detail)
                    detail = hostentry % {"time": round_data["time"],
                                          "roundname": round_data["name"],
                                          "teamname": teams[team].name,
                                          "guests": "\n".join(guest_details)}
                    plan_detail.append(detail)
            plan = "\n\n".join(plan_detail)
            text = template.render(eventdate=config.EVENT_DATE, teamname=teams[team].name, plan=plan)
            msg = MIMEText(text, "plain", "utf8")

            rcpt = teams[team].email
            if debug:
                rcpt = config.MAIL_DEFAULT_SENDER

            msg['Subject'] = subject % teams[team].name
            msg['From'] = sender
            msg['To'] = rcpt

            session.sendmail(envelope, [rcpt] + ["<EMAIL>"], msg.as_string())
            i += 1
        print "Mails sent: %d" % i
<commit_msg>Bugfix for broken routes.<commit_after>from collections import defaultdict
from contextlib import contextmanager
import os
import smtplib
import database as db

from email.mime.text import MIMEText

from jinja2 import Template
from database.model import Team
from geotools import openroute_link
from geotools.routing import MapPoint

from webapp.cfg import config


@contextmanager
def smtp_session():
    session = smtplib.SMTP(config.MAIL_SERVER, config.MAIL_PORT)
    if config.MAIL_USERNAME is not None:
        session.starttls()
        session.login(config.MAIL_USERNAME, config.MAIL_PASSWORD)

    try:
        yield session
    finally:
        session.quit()


def get_template(name):
    filename = "%s.txt" % name
    filepath = os.path.join(os.path.dirname(__file__), "templates", filename)
    if not os.path.isfile(filepath):
        raise Exception("File not found: %s!" % filepath)

    with open(filepath, "r") as fn:
        return Template(unicode(fn.read(), "utf8"))


def informal_to_teams(template_name, subject, debug=True):
    template = get_template(template_name)
    sender = "meet&eat Orga <%s>" % config.MAIL_DEFAULT_SENDER
    envelope = config.MAIL_DEFAULT_SENDER

    print "connect to SMTP...."
    with smtp_session() as session:
        print "Send Mails..."
        i = 0
        for team in db.session.query(Team).filter_by(deleted=False).filter_by(confirmed=True):
            content = template.render(name=team.name)
            msg = MIMEText(content, "plain", "utf8")

            rcpt = team.email
            if debug:
                rcpt = config.MAIL_DEFAULT_SENDER

            msg['Subject'] = subject
            msg['From'] = sender
            msg['To'] = rcpt

            session.sendmail(envelope, [rcpt] + ["<EMAIL>"], msg.as_string())
            i += 1
        print "Mails sent: %d" % i


def plans_to_teams(plan_results, debug=True):
    guestentry = """
%(time)s Uhr - %(roundname)s bei "%(host)s"
%(address)s
Klingeln bei: %(bell)s
Bei Problemen erreicht ihr dieses Team unter: %(phone)s
Routenlink: %(link)s"""

    hostentry = """
%(time)s Uhr - %(roundname)s wird von Euch zubereitet. (%(teamname)s)

zu Gast sind bei Euch:
%(guests)s"""

    hostguests = """
  Team "%(guestname)s"
  (Allergien: %(allergies)s)
  (Vegeratier dabei: %(vegetarians)s)
  Telefon: %(guestphone)s
"""
    subject = "Meet&Eat - Abendplanung Team %s"
    round_datas = ({"time": "18:00", "name": "Vorspeise"},
                   {"time": "20:00", "name": "Hauptgericht"},
                   {"time": "21:30", "name": "Dessert"})

    print "Preprocess plan..."
    guestmap = defaultdict(list)
    for team in plan_results:
        for host in plan_results[team]:
            if team != host:
                guestmap[host].append(team)

    template = get_template("plan")
    sender = "meet&eat Orga <%s>" % config.MAIL_DEFAULT_SENDER
    envelope = config.MAIL_DEFAULT_SENDER

    print "Fetch data..."
    teams = {}
    for team in db.session.query(Team).filter_by(deleted=False).filter_by(confirmed=True):
        teams[str(team.id)] = team

    i = 0
    print "Connect to smtp..."
    with smtp_session() as session:
        print "Send mails..."
        for team in plan_results:
            plan_detail = []
            start_point = MapPoint.from_team(teams[team])
            for (round_idx, host) in enumerate(plan_results[team]):
                round_data = round_datas[round_idx]
                end_point = MapPoint.from_team(teams[host])
                route = openroute_link([start_point, end_point])
                start_point = end_point
                if team != host:
                    detail = guestentry % {"time": round_data["time"],
                                           "roundname": round_data["name"],
                                           "host": teams[host].name,
                                           "address": teams[host].location.street,
                                           "bell": teams[host].location.extra,
                                           "phone": teams[host].phone,
                                           "link": route}
                    plan_detail.append(detail)
                else:
                    guest_details = []
                    for guest in guestmap[team]:
                        guest_detail = hostguests % {"guestname": teams[guest].name,
                                                     "allergies": teams[guest].allergies,
                                                     "vegetarians": teams[guest].vegetarians,
                                                     "guestphone": teams[guest].phone}
                        guest_details.append(guest_detail)
                    detail = hostentry % {"time": round_data["time"],
                                          "roundname": round_data["name"],
                                          "teamname": teams[team].name,
                                          "guests": "\n".join(guest_details)}
                    plan_detail.append(detail)
            plan = "\n\n".join(plan_detail)
            text = template.render(eventdate=config.EVENT_DATE, teamname=teams[team].name, plan=plan)
            msg = MIMEText(text, "plain", "utf8")

            rcpt = teams[team].email
            if debug:
                rcpt = config.MAIL_DEFAULT_SENDER

            msg['Subject'] = subject % teams[team].name
            msg['From'] = sender
            msg['To'] = rcpt

            session.sendmail(envelope, [rcpt] + ["<EMAIL>"], msg.as_string())
            i += 1
        print "Mails sent: %d" % i

{'fraction_non_alphanumeric': 0.11929887318905384, 'fraction_numerical': 0.003577177606868181, 'mean_word_length': 1.848446255731024, 'pattern_counts': {'":': 42, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5738491', 'n_tokens_mistral': 3254, 'n_tokens_neox': 3177, 'n_words': 725}