<commit_before>from datetime import datetime
from optparse import make_option

from go.billing.models import Statement, Account
from go.billing.tasks import month_range, archive_transactions
from go.base.command_utils import BaseGoCommand, get_user_by_email


class Command(BaseGoCommand):
    help = (
        "Archive transactions for an account to S3 for the specified months.")

    option_list = BaseGoCommand.option_list + (
        make_option(
            '--email-address', dest='email_address',
            help="Email address of the account to archive transactions for."),
        make_option(
            '--month', dest='months', action='append',
            help="Month to generate statements for in the form YYYY-MM, "
            "e.g. 2014-01. Multiple may be specified."),
        make_option(
            '--delete', dest='delete', action="store_true", default=False,
            help=("Delete the transactions after uploading the archive."
                  " Default: FALSE.")),
        make_option(
            '--no-statement',
            dest='no_statement', action="store_true", default=False,
            help=("Allow transactions to be archived even if no equivalent "
                  "billing statement is found. Default: FALSE.")))


    def statement_exists(self, account, from_date, to_date):
        statements = Statement.objects.filter(
            account=account,
            from_date=from_date,
            to_date=to_date)

        return statements.exists()

    def missing_statement_months(self, account, months):
        return [
            m for m in months
            if not self.statement_exists(account, *month_range(0, m))]

    def archive_month_transactions(self, account, month, delete):
        from_date, to_date = month_range(0, month)

        archive = archive_transactions(
            account.id, from_date, to_date, delete=delete)

        self.stdout.write(
            "Archiving transactions that occured in %s..."
            % (datetime.strftime(month, '%Y-%m'),))

        self.stdout.write("Archived to S3 as %s." % (archive.filename,))
        self.stdout.write("Archive status is: %s." % (archive.status,))
        self.stdout.write("")

    def archive_transactions(self, account, months, delete):
        for month in months:
            self.archive_month_transactions(account, month, delete)

    def handle(self, *args, **opts):
        user = get_user_by_email(opts['email_address'])
        account_number = user.get_profile().user_account
        account = Account.objects.get(account_number=account_number)

        months = [datetime.strptime(m, '%Y-%m') for m in opts['months']]

        if opts['no_statement']:
            missing_months = []
        else:
            missing_months = self.missing_statement_months(account, months)

        if missing_months:
            self.stderr.write(
                "Aborting archiving, no statements found for the "
                "following months:")

            for m in missing_months:
                self.stderr.write(datetime.strftime(m, '%Y-%m'))
        else:<commit_msg>Print that we are about to archive transactions before we start archiving them.<commit_after>from datetime import datetime
from optparse import make_option

from go.billing.models import Statement, Account
from go.billing.tasks import month_range, archive_transactions
from go.base.command_utils import BaseGoCommand, get_user_by_email


class Command(BaseGoCommand):
    help = (
        "Archive transactions for an account to S3 for the specified months.")

    option_list = BaseGoCommand.option_list + (
        make_option(
            '--email-address', dest='email_address',
            help="Email address of the account to archive transactions for."),
        make_option(
            '--month', dest='months', action='append',
            help="Month to generate statements for in the form YYYY-MM, "
            "e.g. 2014-01. Multiple may be specified."),
        make_option(
            '--delete', dest='delete', action="store_true", default=False,
            help=("Delete the transactions after uploading the archive."
                  " Default: FALSE.")),
        make_option(
            '--no-statement',
            dest='no_statement', action="store_true", default=False,
            help=("Allow transactions to be archived even if no equivalent "
                  "billing statement is found. Default: FALSE.")))

    def statement_exists(self, account, from_date, to_date):
        statements = Statement.objects.filter(
            account=account,
            from_date=from_date,
            to_date=to_date)

        return statements.exists()

    def missing_statement_months(self, account, months):
        return [
            m for m in months
            if not self.statement_exists(account, *month_range(0, m))]

    def archive_month_transactions(self, account, month, delete):
        from_date, to_date = month_range(0, month)

        self.stdout.write(
            "Archiving transactions that occured in %s..."
            % (datetime.strftime(month, '%Y-%m'),))

        archive = archive_transactions(
            account.id, from_date, to_date, delete=delete)

        self.stdout.write("Archived to S3 as %s." % (archive.filename,))
        self.stdout.write("Archive status is: %s." % (archive.status,))
        self.stdout.write("")

    def archive_transactions(self, account, months, delete):
        for month in months:
            self.archive_month_transactions(account, month, delete)

    def handle(self, *args, **opts):
        user = get_user_by_email(opts['email_address'])
        account_number = user.get_profile().user_account
        account = Account.objects.get(account_number=account_number)

        months = [datetime.strptime(m, '%Y-%m') for m in opts['months']]

        if opts['no_statement']:
            missing_months = []
        else:
            missing_months = self.missing_statement_months(account, months)

        if missing_months:
            self.stderr.write(
                "Aborting archiving, no statements found for the "
                "following months:")

            for m in missing_months:
                self.stderr.write(datetime.strftime(m, '%Y-%m'))
        else:
{'fraction_non_alphanumeric': 0.10517104216388226, 'fraction_numerical': 0.0031821797931583136, 'mean_word_length': 2.471010491441193, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6342925', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1651, 'n_words': 455}