<commit_before>try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

import csv
import datetime
import calendar

from django.core.management.base import BaseCommand, CommandError
from django.core import mail
from timetracker.tracker.models import *

connection = mail.get_connection()


class Command(BaseCommand):
    help = 'Sends notifications of overtime balances to the all those ' \
           'who have balances over zero hours.'

    def handle(self, *args, **options):
        message = mail.EmailMessage(from_email="<EMAIL>")
        message.body = \
            "Hi,\n\n" \
            "Please see attached your month end overtime report for " \
            "your team.\n\n" \
            "If there are any errors, please inform the administrator" \
            "of the timetracker immediately.\n\n" \
            "Regards,\n" \
            "Timetracker team"

        buff = StringIO()
        csvout = csv.writer(buff, delimiter=';')
        users = filter(
            lambda user: user.get_total_balance(ret='num') == 0,
            Tbluser.objects.filter(market="BF", disabled=False)
            )

        # generate the dates for this month
        c = calendar.Calendar()
        now = datetime.datetime.now()
        dates = filter(
            lambda date: date.month == now.month,
            c.itermonthdates(now.year, now.month)
            )
        
        csvout.writerow(
            # write out the top heading
            ["Date"] + [', '.join([user.lastname, user.firstname]) for user in users]
            )
        csvout.writerow(
            # write out the top heading
            ["EmployeeID"] + [user.user_id for user in users]
            )
        csvout.writerow(
            # write out the top heading
            ["Balance"] + [user.get_total_balance(ret='num') for user in users]
            )
        for date in dates:
            current_line = [str(date)]
            for user in users:
                try:
                    entry = TrackingEntry.objects.get(user_id=user.id,
                                                      entry_date=date)
                except TrackingEntry.DoesNotExist:
                    current_line.append("")
                    continue

                if entry.is_overtime():
                    current_line.append(0-entry.time_difference())
                else:
                    current_line.append("")
            csvout.writerow(current_line)

        csvfile = buff.getvalue()
        message.attach(
            "overtimereport.csv",
            csvfile,
            "text/csv"
            )
        message.to = ["<EMAIL>", "<EMAIL>"]
#        message.send()

                        
                                        
            <commit_msg>Fixed up the report.<commit_after>try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

import csv
import datetime
import calendar

from django.core.management.base import BaseCommand, CommandError
from django.core import mail
from timetracker.tracker.models import *

connection = mail.get_connection()


class Command(BaseCommand):
    help = 'Sends notifications of overtime balances to the all those ' \
           'who have balances over zero hours.'

    def handle(self, *args, **options):
        message = mail.EmailMessage(from_email="<EMAIL>")
        message.body = \
            "Hi,\n\n" \
            "Please see attached your month end overtime report for " \
            "your team.\n\n" \
            "If there are any errors, please inform the administrator" \
            "of the timetracker immediately.\n\n" \
            "Regards,\n" \
            "Timetracker team"

        buff = StringIO()
        csvout = csv.writer(buff, delimiter=';')
        users = filter(
            lambda user: user.get_total_balance(ret='num') == 0,
            Tbluser.objects.filter(market="BF", disabled=False)
            )

        # generate the dates for this month
        c = calendar.Calendar()
        now = datetime.datetime.now()
        dates = filter(
            lambda date: date.month == now.month,
            c.itermonthdates(now.year, now.month)
            )
        
        csvout.writerow(
            # write out the top heading
            ["Date"] + [user.rev_name() for user in users]
            )
        csvout.writerow(
            # write out the settlement period row.
            ["Settlement Period"] + [user.job_code[-1] if user.job_code else "" for user in users]
            )
        csvout.writerow(
            # write out the e-mail row.
            ["EmployeeID"] + [user.user_id for user in users]
            )
        csvout.writerow(
            # write out the total balances.
            ["Balance"] + [user.get_total_balance(ret='num') for user in users]
            )
        for date in dates:
            current_line = [str(date)]
            for user in users:
                try:
                    entry = TrackingEntry.objects.get(user_id=user.id,
                                                      entry_date=date)
                except TrackingEntry.DoesNotExist:
                    current_line.append("")
                    continue

                if entry.is_overtime():
                    current_line.append(0-entry.time_difference())
                else:
                    current_line.append("")
            csvout.writerow(current_line)

        csvfile = buff.getvalue()
        message.attach(
            "overtimereport.csv",
            csvfile,
            "application/octet-stream"
            )
        message.to = ["<EMAIL>"]#, "<EMAIL>"]
        message.subject = "End of month Overtime Totals."
        message.send()
{'fraction_non_alphanumeric': 0.09596662030598054, 'fraction_numerical': 0.0008692628650904034, 'mean_word_length': 1.5591637010676156, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3217079', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1477, 'n_words': 397}