Ejemplo n.º 1
0
def bid(request, nfl_id):
    u = request.user
    allow_bids = u.is_authenticated() and (not settings.LOCK_BIDS) and util.is_1_waiver_period()
    if allow_bids:
        p = Player.objects.get(nfl_id=nfl_id)
        team = Team.objects.get(owner=u)
        roster = Player.objects.filter(dflteam=team).order_by("position")
        if request.method == "POST":
            val = int(request.POST.get("bidvalue"))
            if val < 0:
                val = 0
            if val > team.account:
                val = team.account
            pk_to_drop = request.POST.get("Drop")
            dropee = Player.objects.get(pk=pk_to_drop)
            priority = int(request.POST.get("Priority"))
            # priority can not be the same as any other unprocessed bids with same drop
            same_drop = Bid.objects.filter(team=team).filter(processed=False).filter(drop=dropee)
            other_prios = []
            for sd in same_drop:
                other_prios.append(sd.priority)
            while priority in other_prios:
                priority += 1
            b = Bid(team=team, amount=val, player=p, drop=dropee, priority=priority)
            b.save()

            # if bid-sum is larger than account, all bids must have unique prio
            bidsum = Bid.objects.filter(team=team).filter(processed=False).aggregate(Sum("amount"))

            if bidsum["amount__sum"] > team.account:
                active_bids = Bid.objects.filter(team=team).filter(processed=False).order_by("priority", "date")
                count = 0
                for b in active_bids:
                    count += 1
                    b.priority = count
                    b.save()

            return HttpResponseRedirect("/team")
        else:
            return render(request, "bid.html", {"player": p, "roster": roster, "team": team})
    else:
        return HttpResponseRedirect("/")