<commit_before>import factory
import random
from datetime import timedelta
from datetime import datetime

from django.contrib.auth.models import User
from competition.models import (Competition, Game, Avatar, Team,
                                Score, Organizer, OrganizerRole,
                                Registration)
from competition.models import RegistrationQuestion as Question
from competition.models import RegistrationQuestionChoice as Choice
from competition.models import RegistrationQuestionResponse as Response


def now(_=None):
    """Returns the current time. Takes a single optional argument,
    which gets thrown away. The argument is to make factory_boy happy.
    """
    return datetime.now()


def later(_=None):
    """Returns the current time + 12 hours. Takes a single optional
    argument, which gets thrown away. The argument is to make
    factory_boy happy.
    """
    return datetime.now() + timedelta(hours=12)


class UserFactory(factory.Factory):
    FACTORY_FOR = User

    username = factory.Sequence(lambda n: 'user' + n)
    password = "<PASSWORD>"

    @classmethod
    def _prepare(cls, create, **kwargs):
        password = kwargs.pop('password', None)
        user = super(UserFactory, cls)._prepare(create, **kwargs)
        if password:
            user.set_password(password)
            if create:
                user.save()
        return user


class CompetitionFactory(factory.Factory):
    FACTORY_FOR = Competition

    name = factory.Sequence(lambda n: "MegaMinerAI: %s" % n)
    start_time = factory.LazyAttribute(now)
    end_time = factory.LazyAttribute(later)
    cost_per_person = 8.00
    min_num_team_members = 1
    max_num_team_members = 3
    description = "This is the best MegaMinerAI ever yay!"


class GameFactory(factory.Factory):
    FACTORY_FOR = Game


class AvatarFactory(factory.Factory):
    FACTORY_FOR = Avatar


class TeamFactory(factory.Factory):
    FACTORY_FOR = Team

    competition = factory.SubFactory(CompetitionFactory)
    name = factory.Sequence(lambda n: "Team #%s" % n)

    @classmethod
    def _prepare(cls, create, **kwargs):
        """Register some users and add them as members of the new team"""
        num_choices = int(kwargs.pop('num_members', 3))
        team = super(TeamFactory, cls)._prepare(create, **kwargs)
        if team.members.count() == 0:
            for _i in range(num_choices):
                u = UserFactory.create()
                RegistrationFactory.create(user=u, competition=team.competition)
                team.members.add(u)
        return team


class ScoreFactory(factory.Factory):
    FACTORY_FOR = Score


class OrganizerFactory(factory.Factory):
    FACTORY_FOR = Organizer


class OrganizerRoleFactory(factory.Factory):
    FACTORY_FOR = OrganizerRole


class RegistrationFactory(factory.Factory):
    FACTORY_FOR = Registration

    user = factory.SubFactory(UserFactory)
    competition = factory.SubFactory(CompetitionFactory)


class RegistrationQuestionFactory(factory.Factory):
    FACTORY_FOR = Question

    question = factory.Sequence(lambda n: "Question #%s" % n)
    question_type = factory.LazyAttribute(
        lambda _: random.choice(Question.QUESTION_TYPES)[0]
    )

    @classmethod
    def _prepare(cls, create, **kwargs):
        num_choices = int(kwargs.pop('num_choices', 4))
        q = super(RegistrationQuestionFactory, cls)._prepare(create, **kwargs)
        if q.question_type in ('SC', 'MC'):
            for _i in range(num_choices):
                RegistrationQuestionChoiceFactory.create(question=q)
        return q


class RegistrationQuestionChoiceFactory(factory.Factory):
    FACTORY_FOR = Choice

    question = factory.SubFactory(RegistrationQuestionFactory)<commit_msg>Fixed Organizer(Role) factories<commit_after>import factory
import random
from datetime import timedelta
from datetime import datetime

from django.contrib.auth.models import User
from competition.models import (Competition, Game, Avatar, Team,
                                Score, Organizer, OrganizerRole,
                                Registration)
from competition.models import RegistrationQuestion as Question
from competition.models import RegistrationQuestionChoice as Choice
from competition.models import RegistrationQuestionResponse as Response


def now(_=None):
    """Returns the current time. Takes a single optional argument,
    which gets thrown away. The argument is to make factory_boy happy.
    """
    return datetime.now()


def later(_=None):
    """Returns the current time + 12 hours. Takes a single optional
    argument, which gets thrown away. The argument is to make
    factory_boy happy.
    """
    return datetime.now() + timedelta(hours=12)


class UserFactory(factory.Factory):
    FACTORY_FOR = User

    username = factory.Sequence(lambda n: 'user' + n)
    password = "<PASSWORD>"

    @classmethod
    def _prepare(cls, create, **kwargs):
        password = kwargs.pop('password', None)
        user = super(UserFactory, cls)._prepare(create, **kwargs)
        if password:
            user.set_password(password)
            if create:
                user.save()
        return user


class CompetitionFactory(factory.Factory):
    FACTORY_FOR = Competition

    name = factory.Sequence(lambda n: "MegaMinerAI: %s" % n)
    start_time = factory.LazyAttribute(now)
    end_time = factory.LazyAttribute(later)
    cost_per_person = 8.00
    min_num_team_members = 1
    max_num_team_members = 3
    description = "This is the best MegaMinerAI ever yay!"


class GameFactory(factory.Factory):
    FACTORY_FOR = Game


class AvatarFactory(factory.Factory):
    FACTORY_FOR = Avatar


class TeamFactory(factory.Factory):
    FACTORY_FOR = Team

    competition = factory.SubFactory(CompetitionFactory)
    name = factory.Sequence(lambda n: "Team #%s" % n)

    @classmethod
    def _prepare(cls, create, **kwargs):
        """Register some users and add them as members of the new team"""
        num_choices = int(kwargs.pop('num_members', 3))
        team = super(TeamFactory, cls)._prepare(create, **kwargs)
        if team.members.count() == 0:
            for _i in range(num_choices):
                u = UserFactory.create()
                RegistrationFactory.create(user=u, competition=team.competition)
                team.add_team_member(u)
        return team


class ScoreFactory(factory.Factory):
    FACTORY_FOR = Score


class OrganizerRoleFactory(factory.Factory):
    FACTORY_FOR = OrganizerRole

    name = factory.Sequence(lambda n: "Role #%s" % n)
    description = "Role description."


class OrganizerFactory(factory.Factory):
    FACTORY_FOR = Organizer

    user = factory.SubFactory(UserFactory)
    competition = factory.SubFactory(CompetitionFactory)

    @classmethod
    def _prepare(cls, create, **kwargs):
        """Add a role to the Organizer"""
        num_roles = int(kwargs.pop('num_roles', 1))
        organizer = super(OrganizerFactory, cls)._prepare(create, **kwargs)
        if num_roles > 0:
            for _i in range(num_roles):
                organizer.role.add(OrganizerRoleFactory.create())
        return organizer


class RegistrationFactory(factory.Factory):
    FACTORY_FOR = Registration

    user = factory.SubFactory(UserFactory)
    competition = factory.SubFactory(CompetitionFactory)


class RegistrationQuestionFactory(factory.Factory):
    FACTORY_FOR = Question

    question = factory.Sequence(lambda n: "Question #%s" % n)
    question_type = factory.LazyAttribute(
        lambda _: random.choice(Question.QUESTION_TYPES)[0]
    )

    @classmethod
    def _prepare(cls, create, **kwargs):
        num_choices = int(kwargs.pop('num_choices', 4))
        q = super(RegistrationQuestionFactory, cls)._prepare(create, **kwargs)
        if q.question_type in ('SC', 'MC'):
            for _i in range(num_choices):
                RegistrationQuestionChoiceFactory.create(question=q)
        return q


class RegistrationQuestionChoiceFactory(factory.Factory):
    FACTORY_FOR = Choice

    question = factory.SubFactory(RegistrationQuestionFactory)
{'fraction_non_alphanumeric': 0.09057255676209279, 'fraction_numerical': 0.0034550839091806516, 'mean_word_length': 3.1886304909560725, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '2472455', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2203, 'n_words': 596}