def handle(self, *args, **options):

        try:
            self.game = Game.imminent_game()
        except Game.DoesNotExist:
            self.stderr.write("No currently-running game found.")
            call_command('newgame', stdout=self.stdout, stderr=self.stderr)
            self.game = Game.imminent_game()

        self.schools = list(School.objects.all())
        self.dorms = list(Building.dorms().all())

        if len(self.schools) == 0 or len(self.dorms) == 0:
            call_command(
                'loaddata',
                os.path.join('HVZ', 'main', 'fixtures', 'production.json'),
                stdout=self.stdout,
                stderr=self.stderr,
            )
            self.schools = list(School.objects.all())
            self.dorms = list(Building.dorms().all())

        num_players = options.get('players') or NUM_PLAYERS
        num_ozs = options.get('ozs') or NUM_OZS

        self.password = options.get('password') or PASSWORD

        self.year = settings.NOW().year

        players = self.make_players(num_players)
        Player.objects.bulk_create(players)

        self.pick_ozs(num_ozs)
import string
import itertools
import random
from optparse import make_option

from django.core.management.base import BaseCommand
from django.conf import settings
from django.contrib.auth.models import User

from HVZ.main.models import Player, Game, School, Building


PASSWORD = "******"
SCHOOLS = School.objects.all()
DORMS = Building.dorms()
FEEDS = itertools.permutations(settings.VALID_CHARS, r=settings.FEED_LEN)

NUM_OZS = 7


def random_name():

    firstname = "".join(
        [random.choice(string.ascii_uppercase)] +
        [random.choice(string.ascii_lowercase) for x in xrange(6)],
    )

    lastname = "".join(
        [random.choice(string.ascii_uppercase)] +
        [random.choice(string.ascii_lowercase) for x in xrange(8)],
    )
Exemple #3
0
class RegisterForm(forms.ModelForm):

    error_messages = {
        'duplicate_user': _("You have already registered for this game!"),
        'duplicate_feed': _("That feed code is already in use for this game!"),
        'password_mismatch': _("The two password fields didn't match!"),
    }

    first_name = forms.CharField(label=_("First name"), required=True)

    last_name = forms.CharField(label=_("Last name"), required=True)

    email = forms.EmailField(required=True)

    password1 = forms.CharField(label=_("Create a password"),
                                widget=forms.PasswordInput,
                                required=True)

    password2 = forms.CharField(
        label=_("Retype the password"),
        widget=forms.PasswordInput,
        required=True,
    )

    #mailbox = forms.CharField(
    #    label=_("Mailbox Number")
    #)

    school = forms.ModelChoiceField(
        queryset=School.objects,
        required=True,
        empty_label=_("Select a school"),
    )

    dorm = forms.ModelChoiceField(
        queryset=Building.dorms().order_by("name"),
        required=True,
        empty_label=_("Select a dorm"),
    )

    grad_year = forms.IntegerField(
        label=_("Expected graduation year"),
        required=True,
        min_value=2000,
        max_value=2100,
    )

    # cell = USPhoneNumberField(
    #     label=_("Cell Number"),
    #     required=False,
    # )

    can_oz = forms.BooleanField(
        label=_("Candidate for Original Zombie"),
        required=False,
    )

    feed = FeedCodeField(
        label="Enter the Feed Code on your index card (not case sensitive)",
        required=True)

    waiver_box = forms.BooleanField(
        label=mark_safe(
            'I have read and agree to the <a href='
            '"https://drive.google.com/file/d/0B78zrV_AHqA4VHJLdzRQOFpfT28/view">'
            'HvZ Waiver of Liability</a>'),
        required=True,
    )

    class Meta:
        model = Player
        fields = (
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2',
            'school',
            'dorm',
            'grad_year',
            'can_oz',
            'feed',
            'waiver_box',
        )

    def clean_email(self):
        """Ensure that a user does not register twice for the same game."""
        email = self.cleaned_data['email']

        if Player.current_players().filter(
                Q(user__username=email) | Q(user__email=email)).exists():
            raise ValidationError(self.error_messages['duplicate_user'])

        return email

    def clean_feed(self):
        """Ensure that the same feed code is not used twice in the same game."""
        feedcode = self.cleaned_data['feed']

        if Player.current_players().filter(feed=feedcode).exists():
            raise ValidationError(self.error_messages['duplicate_feed'])

        return feedcode

    def clean_password2(self):
        """Ensure that the two password fields match."""
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')

        if password1 and password2 and password1 != password2:
            raise forms.ValidationError(
                self.error_messages['password_mismatch'])

        return password2

    def save(self, commit=True):
        """Save the player, creating or updating the user if necessary."""
        player = super(RegisterForm, self).save(commit=False)

        def grab(s):
            return self.cleaned_data.get(s)

        email = grab('email')
        password = grab('password1')

        try:
            user = User.objects.select_for_update().get(email=email)
            user.set_password(password)

        except User.DoesNotExist:

            # Slice is there because of a 30 character limit on
            # usernames... we need a custom user model in the future.
            # FIXED: the longerusernameandemail app makes usernames and
            # emails the same length and that length is 255 characters
            user = User.objects.create_user(
                email=email,
                username=email,
                password=password,
            )

        user.first_name = grab("first_name")
        user.last_name = grab("last_name")
        user.full_clean()
        user.save()

        player.user = user
        player.game = Game.imminent_game()

        if commit == True:
            player.save()

        return player