<commit_before>import sys

from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from account.models import SignupCode


def code_from_email(email):
    """
    Create a SignupCode given an email address.
    """
    code = SignupCode.create(email=email,
                             expiry=7 * 24,
                             max_uses=1,
                             notes='Invited by signup_codes.py.')

    code.save()

    return code


class Command(BaseCommand):
    """
    Generate signup codes.
    """
    help = 'Generate signup codes.'
    args = '<filename>'
    option_list = BaseCommand.option_list + (
        make_option('--send',
                    action='store_true',
                    dest='send',
                    default=False,
                    help='Send an invitation email'),
    )

    def handle(self, *args, **options):
        if not len(args) == 1:
            raise CommandError('Please specify one filename.')

        try:
            with open(args[0]) as email_file:
                for email in email_file.read().splitlines():
                    code = code_from_email(email)

                    if options['send']:
                        code.send()

                    sys.stdout.write('Created code for "{}"\n'.format(email))
        except IOError:
            raise CommandError('File "{}" does not exist.'.format(args[0]))<commit_msg>🔧 Skip sending code if account with that email exists<commit_after>import sys

from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from account.models import EmailAddress, SignupCode


def code_from_email(email):
    """
    Create a SignupCode given an email address.
    """
    code = SignupCode.create(email=email,
                             expiry=7 * 24,
                             max_uses=1,
                             notes='Invited by signup_codes.py.')

    code.save()

    return code


class Command(BaseCommand):
    """
    Generate signup codes.
    """
    help = 'Generate signup codes.'
    args = '<filename>'
    option_list = BaseCommand.option_list + (
        make_option('--send',
                    action='store_true',
                    dest='send',
                    default=False,
                    help='Send an invitation email'),
    )

    def handle(self, *args, **options):
        if not len(args) == 1:
            raise CommandError('Please specify one filename.')

        try:
            with open(args[0]) as email_file:
                for email in email_file.read().splitlines():
                    if EmailAddress.objects.filter(email=email):
                        continue
                    code = code_from_email(email)

                    if options['send']:
                        code.send()

                    sys.stdout.write('Created code for "{}"\n'.format(email))
        except IOError:
            raise CommandError('File "{}" does not exist.'.format(args[0]))
{'fraction_non_alphanumeric': 0.1036988110964333, 'fraction_numerical': 0.004623513870541612, 'mean_word_length': 1.6757950530035335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6639048', 'n_tokens_mistral': 811, 'n_tokens_neox': 763, 'n_words': 200}