Esempio n. 1
0
def import_comparison(request):

    if request.method == "POST":
        form = ComparisonForm(request.POST)
        if form.is_valid():
            parser = getattr(parsers, "parse_%s" % form.cleaned_data['seller'].code)
            if parser:
                parser(form.cleaned_data['seller'], form.cleaned_data['json'])
                messages.success(request, 'Record has been saved successfully.')
            else:
                messages.error(request, 'Can not find proper parser for the seller.', extra_tags='danger')
            return HttpResponseRedirect(".")
        else:
            messages.error(request, 'Failed to save record. Please correct the errors below.', extra_tags='danger')
    else:
        form = ComparisonForm()

    context = {
        'page_header': "Import Comparison",
        'page_title': "Import Comparison",
        'form': form
    }

    return render(
        request,
        'comparisons/import_comparison.html',
        context
    )
Esempio n. 2
0
def comparison(request, pk):
    comparison = Comparison.objects.get(pk=pk)
    form = ComparisonForm(request.POST or None, instance=comparison)
    # print("lifespan comparison")
    if request.POST and form.is_valid():
        # print("lifespan.views.comparison: form.is_valid()")
        form.save()
        msg = "Successfully changed comparison"
        messages.add_message(request, messages.SUCCESS, _(msg))
        redirect("/comparison/%s" % pk)
    ctx = {"comparison": comparison, "form": form}
    return render_to_response("lifespan/comparison.html", ctx, context_instance=RequestContext(request))
Esempio n. 3
0
def comparison(request, pk):
    comparison = Comparison.objects.get(pk=pk)
    form = ComparisonForm(request.POST or None, instance=comparison)
    #print("lifespan comparison")
    if request.POST and form.is_valid():
        #print("lifespan.views.comparison: form.is_valid()")
        form.save()
        msg = "Successfully changed comparison"
        messages.add_message(request, messages.SUCCESS, _(msg))
        redirect('/comparison/%s' % pk)
    ctx = {'comparison': comparison, 'form': form}
    return render_to_response('lifespan/comparison.html',
                              ctx,
                              context_instance=RequestContext(request))
Esempio n. 4
0
def show_team_comparisons():
    rd = setup_nav()
    cpg = ComparisonGraphForm()
    teamgames = get_r_seasons()
    tablecolumns = [
        0,
    ]
    form = ComparisonForm(request.form)
    form.startingSeason.choices = [
        (x, str(x)[0:4] + "-" + str(x)[4:])
        for x in sorted(teamgames.keys(), reverse=True)
    ]
    form.endingSeason.choices = [
        (x, str(x)[0:4] + "-" + str(x)[4:])
        for x in sorted(teamgames.keys(), reverse=True)
    ]
    filterteams = form.filterTeams.data
    if filterteams is None:
        filterteams = []
    allteams = form.filterTeams.choices

    try:
        oldStart = form.startingSeason.data
        oldEnd = form.endingSeason.data
        form.startingSeason.data = int(form.startingSeason.data)
        form.endingSeason.data = int(form.endingSeason.data)
    except:
        pass
    allseasons = [(x, str(x)[0:4] + "-" + str(x)[4:])
                  for x in sorted(teamgames.keys(), reverse=True)]
    usedates = False

    if request.method == "POST" and form.validate():
        startingSeason = form.startingSeason.data
        endingSeason = form.endingSeason.data
        form.startingSeason.data = oldStart
        form.endingSeason.data = oldEnd
        startingDate = form.startingDate.data
        endingDate = form.endingDate.data
        usedates = form.bydate.data
        if not usedates:
            if endingSeason > startingSeason:
                begin = startingSeason
                end = endingSeason
            else:
                begin = endingSeason
                end = startingSeason
            currSeason = begin
            seasons = [
                begin,
            ]
            # TODO: Error check this?
            while begin != end:
                bsplit = str(begin)[0:4]
                begin = int(bsplit) * 10000 + 10001 + int(bsplit) + 1
                seasons.append(begin)
        else:
            if endingDate < startingDate:
                temp = endingDate
                endingDate = startingDate
                startingDate = temp
    else:
        smax = max(teamgames.keys())
        seasons = [
            smax,
        ]
    # Filter teamrun based on form data
    teamstrengths = int(form.teamstrengths.data)
    if teamstrengths == 7:
        teamstrengths = [
            constants.strength_situations_dict[x]["value"]
            for x in constants.strength_situations_dict
        ]
    else:
        teamstrengths = [
            teamstrengths,
        ]
    scoresituations = int(form.scoresituations.data)
    if scoresituations == 7:
        scoresituations = [
            constants.score_situations_dict[x]["value"]
            for x in constants.score_situations_dict
        ]
    homeaway = form.homeAway.data
    if homeaway == "all":
        homeaway = [0, 1]
    else:
        homeaway = [
            int(homeaway),
        ]
    periods = constants.periods_options[form.period.data]["value"]
    if 0 in periods:
        periods = [
            0,
        ]
    if 7 in scoresituations:
        scoresituations = [
            7,
        ]
    if 7 in teamstrengths:
        teamstrengths = [
            7,
        ]

    if not usedates:
        if len(filterteams) == 0:
            teamrun = TeamRun.query.filter(
                TeamRun.season.in_(seasons),
                TeamRun.gamestate.in_(teamstrengths),
                TeamRun.scorediffcat.in_(scoresituations),
                TeamRun.home.in_(homeaway), TeamRun.period.in_(periods)).all()
        else:
            teamrun = TeamRun.query.filter(
                TeamRun.season.in_(seasons),
                TeamRun.gamestate.in_(teamstrengths),
                TeamRun.scorediffcat.in_(scoresituations),
                TeamRun.home.in_(homeaway), TeamRun.Team.in_(filterteams),
                TeamRun.period.in_(periods)).all()
    else:
        if len(filterteams) == 0:
            teamrun = TeamRun.query.filter(
                TeamRun.Date >= startingDate, TeamRun.Date <= endingDate,
                TeamRun.gamestate.in_(teamstrengths),
                TeamRun.scorediffcat.in_(scoresituations),
                TeamRun.home.in_(homeaway), TeamRun.period.in_(periods)).all()
        else:
            teamrun = TeamRun.query.filter(
                TeamRun.Date >= startingDate, TeamRun.Date <= endingDate,
                TeamRun.gamestate.in_(teamstrengths),
                TeamRun.scorediffcat.in_(scoresituations),
                TeamRun.home.in_(homeaway), TeamRun.Team.in_(filterteams),
                TeamRun.period.in_(periods)).all()

    games, seasons = helpers.calculate(teamrun, form.divideSeason.data)
    if form.splitgame.data == True:
        summaries = games
    else:
        summaries = seasons

    return render_template('teams/teamcomparison.html',
                           rd=rd,
                           form=form,
                           summaries=summaries,
                           cpg=cpg,
                           filterteams=filterteams,
                           allteams=allteams)
Esempio n. 5
0
def show_team_comparisons():
    rd = setup_nav()
    cpg = ComparisonGraphForm()
    teamgames = get_r_seasons()
    tablecolumns = [0, ]
    form = ComparisonForm(request.form)
    form.startingSeason.choices = [(x, str(x)[0:4] + "-" + str(x)[4:]) for x in sorted(teamgames.keys(), reverse=True)]
    form.endingSeason.choices = [(x, str(x)[0:4] + "-" + str(x)[4:]) for x in sorted(teamgames.keys(), reverse=True)]
    filterteams = form.filterTeams.data
    if filterteams is None:
        filterteams = []
    allteams = form.filterTeams.choices

    try:
        oldStart = form.startingSeason.data
        oldEnd = form.endingSeason.data
        form.startingSeason.data = int(form.startingSeason.data)
        form.endingSeason.data = int(form.endingSeason.data)
    except:
        pass
    allseasons = [(x, str(x)[0:4] + "-" + str(x)[4:]) for x in sorted(teamgames.keys(), reverse=True)]
    usedates = False

    if request.method == "POST" and form.validate():
        startingSeason = form.startingSeason.data
        endingSeason = form.endingSeason.data
        form.startingSeason.data = oldStart
        form.endingSeason.data = oldEnd
        startingDate = form.startingDate.data
        endingDate = form.endingDate.data
        usedates = form.bydate.data
        if not usedates:
            if endingSeason > startingSeason:
                begin = startingSeason
                end = endingSeason
            else:
                begin = endingSeason
                end = startingSeason
            currSeason = begin
            seasons = [begin, ]
            # TODO: Error check this?
            while begin != end:
                bsplit = str(begin)[0:4]
                begin = int(bsplit) * 10000 + 10001 + int(bsplit) + 1
                seasons.append(begin)
        else:
            if endingDate < startingDate:
                temp = endingDate
                endingDate = startingDate
                startingDate = temp
    else:
        smax = max(teamgames.keys())
        seasons = [smax, ]
    # Filter teamrun based on form data
    teamstrengths = int(form.teamstrengths.data)
    if teamstrengths == 7:
        teamstrengths = [constants.strength_situations_dict[x]["value"] for x in constants.strength_situations_dict]
    else:
        teamstrengths = [teamstrengths, ]
    scoresituations = int(form.scoresituations.data)
    if scoresituations == 7:
        scoresituations = [constants.score_situations_dict[x]["value"] for x in constants.score_situations_dict]
    homeaway = form.homeAway.data
    if homeaway == "all":
        homeaway = [0, 1]
    else:
        homeaway = [int(homeaway), ]
    periods = constants.periods_options[form.period.data]["value"]
    if 0 in periods:
        periods = [0, ]
    if 7 in scoresituations:
        scoresituations = [7, ]
    if 7 in teamstrengths:
        teamstrengths = [7, ]

    if not usedates:
        if len(filterteams) == 0:
            teamrun = TeamRun.query.filter(TeamRun.season.in_(seasons), TeamRun.gamestate.in_(teamstrengths),
                TeamRun.scorediffcat.in_(scoresituations), TeamRun.home.in_(homeaway),
                TeamRun.period.in_(periods)).all()
        else:
            teamrun = TeamRun.query.filter(TeamRun.season.in_(seasons), TeamRun.gamestate.in_(teamstrengths),
                TeamRun.scorediffcat.in_(scoresituations), TeamRun.home.in_(homeaway),
                TeamRun.Team.in_(filterteams),
                TeamRun.period.in_(periods)).all()
    else:
        if len(filterteams) == 0:
            teamrun = TeamRun.query.filter(TeamRun.Date >= startingDate, TeamRun.Date <= endingDate,
                TeamRun.gamestate.in_(teamstrengths),
                TeamRun.scorediffcat.in_(scoresituations), TeamRun.home.in_(homeaway),
                TeamRun.period.in_(periods)).all()
        else:
            teamrun = TeamRun.query.filter(TeamRun.Date >= startingDate, TeamRun.Date <= endingDate,
                TeamRun.gamestate.in_(teamstrengths),
                TeamRun.scorediffcat.in_(scoresituations), TeamRun.home.in_(homeaway),
                TeamRun.Team.in_(filterteams),
                TeamRun.period.in_(periods)).all()

    games, seasons = helpers.calculate(teamrun, form.divideSeason.data)
    if form.splitgame.data == True:
        summaries = games
    else:
        summaries = seasons

    return render_template('teams/teamcomparison.html',
        rd=rd,
        form=form,
        summaries=summaries,
        cpg=cpg,
        filterteams=filterteams,
        allteams=allteams)