Beispiel #1
0
class TeamForm(Form):
    name = StringField('Team Name', validators=[
        validators.required(),
        validators.Length(min=-1, max=Team.name.type.length)])

    tag = StringField('Team Tag', validators=[
        validators.required(), validators.Length(min=-1, max=Team.tag.type.length)])

    flag_choices = [('', 'None')] + countries.country_choices
    country_flag = SelectField(
        'Country Flag', choices=flag_choices, default='')

    logo_choices = logos.get_logo_choices()
    logo = SelectField('Logo Name', choices=logo_choices, default='')

    auth1 = StringField('Player 1', validators=[valid_auth])
    auth2 = StringField('Player 2', validators=[valid_auth])
    auth3 = StringField('Player 3', validators=[valid_auth])
    auth4 = StringField('Player 4', validators=[valid_auth])
    auth5 = StringField('Player 5', validators=[valid_auth])
    auth6 = StringField('Player 6', validators=[valid_auth])
    auth7 = StringField('Player 7', validators=[valid_auth])
    public_team = BooleanField('Public Team')

    def get_auth_list(self):
        auths = []
        for i in range(1, 8):
            key = 'auth{}'.format(i)
            auths.append(self.data[key])

        return auths
Beispiel #2
0
def team_create():
    mock = config_setting("TESTING")
    customNames = config_setting("CUSTOM_PLAYER_NAMES")
    if not g.user:
        return redirect('/login')
    form = TeamForm()
    # We wish to query this every time, since we can now upload photos.
    if not mock:
        form.logo.choices = logos.get_logo_choices()
    if request.method == 'POST':
        num_teams = g.user.teams.count()
        max_teams = config_setting('USER_MAX_TEAMS')
        if max_teams >= 0 and num_teams >= max_teams and not (util.is_admin(
                g.user) or util.is_super_admin(g.user)):
            flash('You already have the maximum number of teams ({}) stored'.
                  format(num_teams))

        elif form.validate():
            data = form.data
            auths = form.get_auth_list()
            pref_names = form.get_pref_list()
            name = data['name'].strip()
            tag = data['tag'].strip()
            flag = data['country_flag']
            logo = data['logo']

            # Update the logo. Passing validation we have the filename in the
            # list now.
            if not mock and (util.is_admin(g.user) or util.is_super_admin(
                    g.user)) and form.upload_logo.data:
                filename = secure_filename(form.upload_logo.data.filename)
                index_of_dot = filename.index('.')
                newLogoDetail = filename[:index_of_dot]
                # Reinit our logos.
                logos.add_new_logo(newLogoDetail)
                app.logger.info("Added new logo id {}".format(newLogoDetail))
                data['logo'] = newLogoDetail

            team = Team.create(
                g.user, name, tag, flag, logo, auths, data['public_team']
                and (util.is_admin(g.user) or util.is_super_admin(g.user)),
                pref_names)

            db.session.commit()
            app.logger.info('User {} created team {}'.format(
                g.user.id, team.id))

            return redirect('/teams/{}'.format(team.user_id))

        else:
            flash_errors(form)

    return render_template('team_create.html',
                           user=g.user,
                           form=form,
                           edit=False,
                           is_admin=(util.is_admin(g.user)
                                     or util.is_super_admin(g.user)),
                           MAXPLAYER=Team.MAXPLAYERS,
                           customNames=customNames)
Beispiel #3
0
class TeamForm(FlaskForm):
    mock = config_setting("TESTING")
    name = StringField('Team Name',
                       validators=[
                           validators.required(),
                           validators.Length(min=-1, max=Team.name.type.length)
                       ])

    tag = StringField('Team Tag',
                      validators=[
                          validators.optional(),
                          validators.Length(min=-1, max=Team.tag.type.length)
                      ])

    flag_choices = [('', 'None')] + countries.country_choices
    country_flag = SelectField('Country Flag',
                               choices=flag_choices,
                               default='')
    if mock:
        logo_choices = logos.get_logo_choices()
        logo = SelectField('Logo Name', choices=logo_choices, default='')
    else:
        logo = SelectField('Logo Name', default='')

    upload_logo = FileField(validators=[valid_file])

    public_team = BooleanField('Public Team')

    def get_auth_list(self):
        auths = []
        for i in range(1, Team.MAXPLAYERS + 1):
            key = 'auth{}'.format(i)
            auths.append(self.data[key])

        return auths

    def get_pref_list(self):
        prefs = []
        for i in range(1, Team.MAXPLAYERS + 1):
            key = 'pref_name{}'.format(i)
            prefs.append(self.data[key])

        return prefs
Beispiel #4
0
def team_edit(teamid):
    mock = config_setting("TESTING")
    customNames = config_setting("CUSTOM_PLAYER_NAMES")
    team = Team.query.get_or_404(teamid)
    if not team.can_edit(g.user):
        raise BadRequestError("Not your team.")
    form = TeamForm()
    # We wish to query this every time, since we can now upload photos.
    if not mock:
        form.logo.choices = logos.get_logo_choices()
    if request.method == 'GET':
        # Set values here, as per new FlaskForms.
        form.name.data = team.name
        form.tag.data = team.tag
        form.country_flag.data = team.flag
        form.logo.data = team.logo
        for field in form:
            if "auth" in field.name:
                try:
                    field.data = team.auths[
                        int(re.search(r'\d+', field.name).group()) - 1]
                except:
                    field.data = None
            if "pref_name" in field.name:
                try:
                    field.data = team.preferred_names[
                        int(re.search(r'\d+', field.name).group()) - 1]
                except:
                    field.data = None
        form.public_team.data = team.public_team
        return render_template('team_create.html',
                               user=g.user,
                               form=form,
                               edit=True,
                               is_admin=(g.user.admin or g.user.super_admin),
                               MAXPLAYER=Team.MAXPLAYERS,
                               customNames=customNames)

    elif request.method == 'POST':
        if form.validate():
            data = form.data
            public_team = team.public_team
            if (g.user.admin or g.user.super_admin):
                public_team = data['public_team']

            # Update the logo. Passing validation we have the filename in the
            # list now.
            if not mock and (g.user.admin
                             or g.user.super_admin) and form.upload_logo.data:
                filename = secure_filename(form.upload_logo.data.filename)
                index_of_dot = filename.index('.')
                newLogoDetail = filename[:index_of_dot]
                # Reinit our logos.
                logos.add_new_logo(newLogoDetail)
                data['logo'] = newLogoDetail
            allAuths = form.get_auth_list()
            allNames = form.get_pref_list()
            team.set_data(data['name'], data['tag'], data['country_flag'],
                          data['logo'], allAuths, public_team, allNames)
            for auth, name in itertools.izip_longest(allAuths, allNames):
                if auth:
                    teamNames = TeamAuthNames.set_or_create(teamid, auth, name)

            db.session.commit()
            return redirect('/teams/{}'.format(team.user_id))
        else:
            flash_errors(form)

    return render_template('team_create.html',
                           user=g.user,
                           form=form,
                           edit=True,
                           is_admin=g.user.admin,
                           MAXPLAYER=Team.MAXPLAYERS)