def __create_lineups_with_fantasy_points(self, contest_pool, lineup_points=[]): """ contest is the contest to associate lineups with lineup_points is an array of the points to give to the lineups in creation order. """ max = contest_pool.entries for i in range(1, max + 1): # get the user for the lineup user = self.get_user(username=str(i)) self.fund_user_account(user) # set the rest of the lineup properties lineup = Lineup() lineup.fantasy_points = lineup_points[i - 1] lineup.user = user lineup.draft_group = self.draftgroup lineup.save() # buy this lineup into the contest bm = BuyinManager(lineup.user) bm.buyin(self.contest_pool, lineup) Entry.objects.filter(contest_pool=self.contest_pool).update( contest=self.contest) # set the contest as payout-able self.contest.status = Contest.COMPLETED self.contest.save()
def create_last_place_tie_teams_three_way(self): """ create Lineups such that there is a 3 way tie amongst the last 3 ranks. """ max = 6 tie_amount = 10.0 for i in range(1, max + 1): user = self.get_user(username=str(i)) self.fund_user_account(user) lineup = Lineup() if i <= 3: # for 1, 2, 3 lineup.test_fantasy_points = tie_amount else: # teams 4, 5, 6 should have unique test_fantasy_points lineup.test_fantasy_points = tie_amount + i lineup.user = user lineup.draft_group = self.draftgroup lineup.save() bm = BuyinManager(lineup.user) bm.buyin(self.contest_pool, lineup) Entry.objects.filter(contest_pool=self.contest_pool).update( contest=self.contest) self.contest.status = Contest.COMPLETED self.contest.save()
def __refund_entry(self, entry): """ refund a single entry. THIS DOES NOT remove the entry from a contest! :param entry: :return: """ buyin = entry.contest_pool.prize_structure.buyin bm = BuyinManager(entry.user) transaction = None # Create a cash or ticket deposit as a refund, # based on what the user used to get into the contest if bm.entry_did_use_ticket(entry): tm = TicketManager(entry.user) tm.deposit(buyin) transaction = tm.transaction refund = self.__create_refund(transaction, entry) else: ct = CashTransaction(entry.user) ct.deposit(buyin) transaction = ct.transaction refund = self.__create_refund(transaction, entry) # Create refund transaction from escrow escrow_ct = CashTransaction(self.get_escrow_user()) escrow_ct.withdraw(buyin, trans=transaction) return refund
def create_simple_teams(self, max=6): # # create Lineups for i in range(1, max + 1): user = self.get_user(username=str(i)) self.fund_user_account(user) lineup = Lineup() lineup.fantasy_points = max - i lineup.user = self.get_user(username=str(i)) lineup.draft_group = self.draftgroup lineup.save() bm = BuyinManager(lineup.user) bm.buyin(self.contest_pool, lineup) Entry.objects.filter(contest_pool=self.contest_pool).update( contest=self.contest) self.contest.status = Contest.COMPLETED self.contest.save()
def setUp(self): super().setUp() self.user1 = self.get_basic_user("test1") self.user2 = self.get_basic_user("test2") self.user3 = self.get_basic_user("test3") self.build_world() self.user1_ct = CashTransaction(self.user1) self.user1_ct.deposit(100) self.user2_ct = CashTransaction(self.user2) self.user2_ct.deposit(50) ta = TicketAmount.objects.get(amount=10.00) self.user3_tm = TicketManager(self.user3) self.user3_tm.deposit(10) self.escrow_user = self.user2_ct.get_escrow_user() self.escrow_ct = CashTransaction(self.escrow_user) bm = BuyinManager(self.user1) bm.buyin(self.contest_pool) bm = BuyinManager(self.user2) bm.buyin(self.contest_pool) bm = BuyinManager(self.user3) bm.buyin(self.contest_pool) Entry.objects.filter(contest_pool=self.contest_pool).update( contest=self.contest)
def post(self, request, format=None): lineup_id = request.data.get('lineup') contest_pool_id = request.data.get('contest_pool') # ensure the ContestPool exists try: contest_pool = ContestPool.objects.get(pk=contest_pool_id) except ContestPool.DoesNotExist: # return Response( 'ContestPool does not exist', status=status.HTTP_403_FORBIDDEN ) raise APIException('ContestPool does not exist') # ensure the lineup is valid for this user try: lineup = Lineup.objects.get(pk=lineup_id, user=request.user) except Lineup.DoesNotExist: # return Response( 'Lineup does not exist', status=status.HTTP_403_FORBIDDEN ) raise APIException('Lineup does not exist') # check if this user can enter this skill level skill_level_manager = SkillLevelManager() try: skill_level_manager.validate_can_enter(request.user, contest_pool) except SkillLevelManager.CanNotEnterSkillLevel: raise APIException('You may not enter this Skill Level.') try: contest_entry_alert = request.user.limits.get( type=Limit.ENTRY_ALERT) entries_count = Entry.objects.filter( user=request.user, created__range=contest_entry_alert.time_period_boundaries, contest_pool__draft_group=contest_pool.draft_group, ).count() if entries_count == contest_entry_alert.value: send_entry_alert_email.delay(user=request.user) except Limit.DoesNotExist: pass try: contest_entry_limit = request.user.limits.get( type=Limit.ENTRY_LIMIT) entries_count = Entry.objects.filter( user=request.user, created__range=contest_entry_limit.time_period_boundaries, contest_pool__draft_group=contest_pool.draft_group, ).count() if entries_count == contest_entry_limit.value: raise APIException( 'You have reached your contest entry limit of {} entries.'. format(contest_entry_limit.value)) except Limit.DoesNotExist: pass try: entry_fee_limit = request.user.limits.get(type=Limit.ENTRY_FEE) if contest_pool.prize_structure.buyin > entry_fee_limit.value: raise APIException( 'You have reached your entry fee limit of {}.'.format( entry_fee_limit.value)) except Limit.DoesNotExist: pass try: bm = BuyinManager(request.user) buyin_result = bm.buyin(contest_pool=contest_pool, lineup=lineup) except OverdraftException: raise ValidationError({ "detail": "You do not have the necessary funds for this action." }) except ContestMaxEntriesReachedException as e: raise ValidationError({"detail": "%s" % e}) except UserDoesNotHaveTicketException: raise ValidationError( {"detail": "You do not have the necessary funds."}) except Exception as e: logger.error("EnterLineupAPIView: %s" % str(e)) client.captureException() raise APIException({"detail": "Unable to enter contest."}) # Create a user log entry. create_user_log(request=request, user=request.user, type=_account_const.CONTEST, action=_account_const.CONTEST_ENTERED, metadata={ 'detail': 'Contest Entered.', 'contest_pool': contest_pool_id, 'lineup': lineup_id, }) serializer = self.serializer_class(buyin_result) return Response(serializer.data, status=status.HTTP_200_OK)