Example #1
0
def create_league():
    admin = g.player
    form = CreateLeagueForm()
    if form.validate_on_submit():
        league = League(form.name.data, admin, int(form.match_type.data))
        if int(form.match_type.data) == TEAM:
            league.num_teams = int(form.num_teams.data)
            league.team_size = int(form.team_size.data)
        else:
            raise ValueError("Invalid Match Type!")
        db.session.add(league)
        db.session.commit()
        return redirect(url_for("index"))
    return render_template("create_league.html", form=form)
def main(args):
    league = League(args[1])
    teams = league.get_members()

    set_size = 2
    schedule = set()
    teamrange = range(len(teams))
    for comb in itertools.product(teamrange, repeat=set_size):
        comb = sorted(list(comb))
        if len(set(comb)) == set_size:
            schedule.add(tuple(comb))

    schedule = list(schedule)
    random.shuffle(schedule)
    print schedule
Example #3
0
def _leagues_data():
    _leagues = League.get_all()

    if len(_leagues) == 0:
        sentry.captureMessage("Leagues.get_all() returned an empty list.")
        return []

    replay_counts = {
        x.league_id: x.count
        for x in db.engine.execute(
            text(
                """
            SELECT
                r.league_id as league_id,
                count(*) as count
            FROM {replay_table} r
            WHERE
                r.league_id in ({league_id_csv}) AND
                r.state = "ARCHIVED"
            GROUP BY r.league_id
            """.format(
                    replay_table=Replay.__tablename__, league_id_csv=",".join(str(x.id) for x in _leagues)
                )
            )
        )
    }

    leagues_with_replays = []
    for _league in _leagues:
        if replay_counts.get(_league.id) > 0:
            _league.count = replay_counts.get(_league.id)
            leagues_with_replays.append(_league)

    # Sort by archived count
    return sorted(leagues_with_replays, key=lambda r: r.count, reverse=True)
Example #4
0
def league(_id, page=1):
    _league = League.get_by_id(_id)
    if _league is None:
        abort(404)

    _replays = Replay.query.filter(Replay.league_id == _id).paginate(
        page, current_app.config["REPLAYS_PER_PAGE"], False
    )

    return render_template(
        "leagues/league.html", title="{} - Dotabank".format(_league.name), league=_league, replays=_replays
    )
Example #5
0
def add_or_edit_league(request, league=None):
    existing = ""
    if league:
        league = League.objects.get(pk=league)
    if request.method == "POST":
        if league:
            form = LeagueForm(request.POST)
        else:
            form = NewLeagueForm(request.POST)
        if form.is_valid():
            if form.cleaned_data["join_leagues"]:
                for league in form.cleaned_data["join_leagues"]:
                    # apply to join these leagues
                    application = LeagueJoinStates.joined.apply_to_league(league, request.user)
                url = reverse("logged_in")
                return redirect(url)
            else:
                could_join = Set([])
                applied = Set([])
                invited = Set([])
                # first, build three lists (applied, invited,
                # could_join) of leagues that people we're inviting
                # are already in
                for row in form.members.cleaned_data:
                    if row:
                        try:
                            member = CustomUser.objects.get(email=row["email"])
                        except CustomUser.DoesNotExist:
                            continue

                        user = request.user
                        leagues = member.leagues_by_state(STATE_ACCEPTED)
                        # don't include leagues we've already decided
                        # not to join (or have been rejected from)
                        leagues = leagues.exclude(
                            leaguejoinstates__state=STATE_REJECTED,
                            # leaguejoinstates__state=STATE_DECLINED,
                            leaguejoinstates__user=user,
                        )
                        if leagues:
                            applied.update(
                                leagues.filter(leaguejoinstates__state=STATE_APPLIED, leaguejoinstates__user=user)
                            )
                            invited.update(
                                leagues.filter(leaguejoinstates__state=STATE_INVITED, leaguejoinstates__user=user)
                            )
                            could_join.update(leagues.exclude(leaguejoinstates__user=user))
                if not request.POST.get("yes_really", "") and (could_join or applied or invited):
                    # present the user with a choice to join other
                    # leagues instead of making a new one
                    #
                    # XXX this is horrendous but I don't know how to
                    # do it better
                    choices = [(x.id, x.name) for x in could_join]
                    form.fields["join_leagues"].choices = choices
                    return locals()
                else:
                    # create a new league and invite people to join it
                    if not league:
                        league = League(name=form.cleaned_data["name"], owner=request.user)
                        league.save()
                        # the current user will be a member, of course
                        application = LeagueJoinStates(league=league, user=request.user, state=STATE_ACCEPTED)
                        application.save()
                    for row in form.members.cleaned_data:
                        if not row:
                            continue
                        email = row["email"]
                        try:
                            member = CustomUser.objects.get(email=email)
                        except CustomUser.DoesNotExist:
                            create_user = CustomUser.objects.create_user
                            member = create_user(username=email, email=email)
                            member.save()
                        try:
                            profile = RegistrationProfile.objects.filter(user=member).get()
                        except RegistrationProfile.DoesNotExist:
                            profile = RegistrationProfile.objects.create_profile(member, email=False)
                        invite = LeagueJoinStates.joined.invite_user(league, member)
                    url = reverse("logged_in")
                    return redirect(url)

    else:
        if league:
            form = LeagueForm()
        else:
            form = NewLeagueForm()
    return locals()