<commit_before>import os
from tempfile import NamedTemporaryFile
from rivr import serve
from invoke import task, run
from sotu.models import Entrant, Invitation
from sotu.middleware import middleware
from sotu.email import send_invitation


@task
def migrate():
    Entrant.create_table()
    Invitation.create_table()


@task
def test():
    with NamedTemporaryFile() as fp:
        os.environ['DATABASE_URL'] = 'sqlite:///' + fp.name
        os.environ['GITHUB_CLIENT_ID'] = 'test_id'
        os.environ['GITHUB_CLIENT_SECRET'] = 'test_secret'
        os.environ['GITHUB_BASE_URI'] = 'http://localhost:5959'
        os.environ['GITHUB_API_BASE_URI'] = 'http://localhost:5959'
        run('invoke migrate')
        run('python -m unittest discover')


@task
def status():
    entrants = Entrant.select().count()
    invited = Invitation.select().where(Invitation.state == Invitation.INVITED_STATE).count()
    accepted = Invitation.select().where(Invitation.state == Invitation.ACCEPTED_STATE).count()
    rejected = Invitation.select().where(Invitation.state == Invitation.REJECTED_STATE).count()
    print('Entrants: {}\n---'.format(entrants))
    print('Invited: {}'.format(invited))
    print('Accepted: {}'.format(accepted))
    print('Entrants: {}'.format(rejected))


@task
def invite(username, force=False):
    try:
        entrant = Entrant.select().where(Entrant.github_username == username).get()
    except Entrant.DoesNotExist:
        print('Username {} does not exist.'.format(username))
        return

    if entrant.invitation_set.exists() and not force:
        print('{} already has an invitation.'.format(username))
        return

    if force:
        invitation = entrant.invitation_set.get()
    else:
        invitation = entrant.invite()

    send_invitation(invitation)

<commit_msg>[tasks] Add random invitation command<commit_after>import os
from tempfile import NamedTemporaryFile
import peewee
from rivr import serve
from invoke import task, run
from sotu.models import Entrant, Invitation
from sotu.middleware import middleware
from sotu.email import send_invitation


@task
def migrate():
    Entrant.create_table()
    Invitation.create_table()


@task
def test():
    with NamedTemporaryFile() as fp:
        os.environ['DATABASE_URL'] = 'sqlite:///' + fp.name
        os.environ['GITHUB_CLIENT_ID'] = 'test_id'
        os.environ['GITHUB_CLIENT_SECRET'] = 'test_secret'
        os.environ['GITHUB_BASE_URI'] = 'http://localhost:5959'
        os.environ['GITHUB_API_BASE_URI'] = 'http://localhost:5959'
        run('invoke migrate')
        run('python -m unittest discover')


@task
def status():
    entrants = Entrant.select().count()
    invited = Invitation.select().where(Invitation.state == Invitation.INVITED_STATE).count()
    accepted = Invitation.select().where(Invitation.state == Invitation.ACCEPTED_STATE).count()
    rejected = Invitation.select().where(Invitation.state == Invitation.REJECTED_STATE).count()
    print('Entrants: {}\n---'.format(entrants))
    print('Invited: {}'.format(invited))
    print('Accepted: {}'.format(accepted))
    print('Entrants: {}'.format(rejected))


@task
def invite(username, force=False):
    try:
        entrant = Entrant.select().where(Entrant.github_username == username).get()
    except Entrant.DoesNotExist:
        print('Username {} does not exist.'.format(username))
        return

    if entrant.invitation_set.exists() and not force:
        print('{} already has an invitation.'.format(username))
        return

    if force:
        invitation = entrant.invitation_set.get()
    else:
        invitation = entrant.invite()

    send_invitation(invitation)


@task
def lottery(amount):
    amount = int(amount)

    entrants = Entrant.select().order_by(peewee.fn.Random())
    entrants = filter(lambda e: e.invitation_set.count() == 0, entrants)[:amount]

    print('Inviting {} entrants.'.format(len(entrants)))

    for entrant in entrants:
        print(entrant.github_username)
        entrant.invite()
        send_invitation(invitation)


{'fraction_non_alphanumeric': 0.1345679012345679, 'fraction_numerical': 0.004197530864197531, 'mean_word_length': 3.7380116959064327, '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': '151837', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1285, 'n_words': 254}