def SavePreferences(user, prefs_dct): hret = CU.HttpReturn() try: user_m = AM.User.objects.get(username = user) except AM.User.DoesNotExist: hret.results = "User not found." hret.status = 401 return hret try: prof_m = MT.Profile.objects.get(UserFK = user_m) except MT.Profile.DoesNotExist: hret.results = "Profile not found." hret.status = 401 return hret prof_m.Pref_FriendsRealName = prefs_dct['frdRealName'] prof_m.Pref_FriendsEmail = prefs_dct['frdEmail'] prof_m.save() # return succesfull results hret = CU.HttpReturn() hret.results = user_m.get_full_name() hret.status = 201 return hret
def SaveFavoriteClub(user, newClub): hret = CU.HttpReturn() try: user_m = AM.User.objects.get(username = user) except AM.User.DoesNotExist: hret.results = "User not found." hret.status = 201 return hret try: prof_m = MT.Profile.objects.get(UserFK = user_m) except MT.Profile.DoesNotExist: hret.results = "Profile not found." hret.status = 201 return hret try: club_m = FT.Club.objects.get(Club = newClub) except FT.Club.DoesNotExist: hret.results = "Club not found." hret.status = 201 return hret prof_m.FavClubFK = club_m prof_m.FavClubSet = True prof_m.save() # return succesfull results hret = CU.HttpReturn() hret.results = club_m.Club hret.status = 201 return hret
def SaveSlogan(user, newSlogan): hret = CU.HttpReturn() try: user_m = AM.User.objects.get(username = user) except AM.User.DoesNotExist: hret.results = "User not found." hret.status = 201 return hret try: prof_m = MT.Profile.objects.get(UserFK = user_m) except MT.Profile.DoesNotExist: hret.results = "Profile not found." hret.status = 201 return hret prof_m.Slogan = newSlogan prof_m.save() # return succesfull results hret = CU.HttpReturn() hret.results = newSlogan hret.status = 201 return hret
def pu_rewards_jx(request, command): prog_lg.info("ajax edit command: " + command) if command == 'get_pointsDist': season = request.GET.get('season') roundv = request.GET.get('roundv') results = PU.Reporter_Ranks.RunRewardsData(season, roundv) hret = CU.HttpReturn() hret.results = results hret.status = 201 return JsonResponse(hret.results, safe=False, status=hret.status) elif command == 'get_rewardStatus': season = request.GET.get('season') #roundv = request.GET.get('roundv') roundList = FM.Reports_General.GetRounds(season, "lastData") roundv = roundList[0] if roundList else '' charts = PU.Reporter_Ranks.RunRewardsData(season, roundv) status = PU.Reporter_Ranks.GetRewardStatus(season, roundv) results = { 'rounds': roundList, 'charts': charts, 'status': status if status['scoreSummary'] else "No score summary available", } hret = CU.HttpReturn() hret.results = results hret.status = 201 return JsonResponse(hret.results, safe=False, status=hret.status) elif command == 'update_rankRewards': season = request.POST.get('season') roundv = request.POST.get('roundv') hret = DM.PU_Editor.RunRewards(season, roundv) if hret.status < 400: roundList = FM.Reports_General.GetRounds(season, "lastData") roundLast = roundList[0] if roundList else '' hret = CU.HttpReturn() hret.results = PU.Reporter_Ranks.GetRewardStatus(season, roundLast) hret.status = 201 return JsonResponse(hret.results, safe=False, status=hret.status) else: msg = "command invalid: " + command excp_lg.error(msg) return JsonResponse(msg, safe=False, status=404)
def GetClubRanksFT(p_season): hret = CU.HttpReturn() rank_dt = Reports_Season.GetLastRanking(p_season) rank_dt = sorted(rank_dt, key=operator.attrgetter('rank')) if not rank_dt: return "No rankings found." ranks_dict = [] for rnk in rank_dt: rank_dx = OrderedDict() rank_dx['rank'] = rnk.rank rank_dx['club'] = rnk.club rank_dx['points'] = rnk.points rank_dx['G-Diff'] = rnk.goals_diff rank_dx['goals'] = rnk.goals_for ranks_dict.append(rank_dx) colFmt = OrderedDict() colFmt['rank'] = 'format_center' colFmt['club'] = 'format_fixline' colFmt['points'] = 'format_center' colFmt['G-Diff'] = 'format_center' colFmt['goals'] = 'format_center' ftable = { 'data': ranks_dict, 'colFmt': colFmt, } return ftable
def MakePredictions(p_season, p_round): bot_mdls = User.objects.filter(username__icontains="botuser") for bot_m in bot_mdls: prof = MT.Profile.objects.get(UserFK=bot_m) game_mdls = FT.Game.objects.filter(SeasonFK__Season=p_season, Round=p_round) # 1% chance of not making predictions for the week skip = randint(1, 100) if skip <= 1: continue # make predictions for each game for game_m in game_mdls: dates = PU.Reporter_Common.GetPredictionWindow(game_m.PlayDate) pred_m, created = PU.Univ_Prediction.objects.get_or_create( UserFK=bot_m, GameFK=game_m, OpenDate=dates['open'], CloseDate=dates['close']) pred_m.Result = TestUsers_Editor.GetRandomWinSpectrum( prof.BotAI) pred_m.save() hret = CU.HttpReturn() hret.results = "Test Users Predictions Made" hret.status = 201 return hret
def GetEventSummary(p_season): hret = CU.HttpReturn() eventCnt_dx = dict( FT.GameEvent.objects.filter( GameFK__SeasonFK__Season=p_season).values_list( 'EventFK__Type').annotate(event_count=Count('EventFK'))) if not eventCnt_dx: hret.results = "Event types not initialized." hret.status = 201 return hret evHome_dx = dict( FT.GameEvent.objects.filter( GameFK__SeasonFK__Season=p_season, GameFK__ClubHomeFK=F('EventClubFK')).values_list( 'EventFK__Type').annotate(event_count=Count('EventFK'))) evAway_dx = dict( FT.GameEvent.objects.filter( GameFK__SeasonFK__Season=p_season, GameFK__ClubAwayFK=F('EventClubFK')).values_list( 'EventFK__Type').annotate(event_count=Count('EventFK'))) types_dict = [] for evType in eventCnt_dx: # loop over dict keys type_dx = OrderedDict() type_dx['type'] = evType type_dx['total'] = eventCnt_dx[evType] if evType in evHome_dx: type_dx['HomeClub'] = evHome_dx[evType] else: type_dx['HomeClub'] = 0 if evType in evAway_dx: type_dx['AwayClub'] = evAway_dx[evType] else: type_dx['AwayClub'] = 0 types_dict.append(type_dx) types_dict = sorted(types_dict, key=lambda k: k['type']) colFormat = OrderedDict() colFormat['type'] = '' colFormat['Total'] = 'format_center' colFormat['HomeClub'] = 'format_center' colFormat['AwayClub'] = 'format_center' record = { 'data': types_dict, 'colFormat': colFormat, } hret.results = record hret.status = 201 return hret
def RemoveFriend(primaryUser, removed): removeUser = AM.User.objects.get(username=removed) MT.Relationship.objects.get(User1FK = primaryUser, User2FK = removeUser).delete() MT.Relationship.objects.get(User1FK = removeUser, User2FK = primaryUser).delete() hret = CU.HttpReturn() hret.results = "Friendship removed." hret.status = 201 return hret
def DeleteAccount(user): hret = CU.HttpReturn() try: user_m = AM.User.objects.get(username = user) except AM.User.DoesNotExist: hret.results = "User not found." hret.status = 401 return hret user_m.is_active = False user_m.save() # return succesfull results hret = CU.HttpReturn() hret.results = "User account inactivated." hret.status = 201 return hret
def SaveRealName(user, first, last): hret = CU.HttpReturn() try: user_m = AM.User.objects.get(username = user) except AM.User.DoesNotExist: hret.results = "User not found." hret.status = 201 return hret user_m.first_name = first user_m.last_name = last user_m.save() # return succesfull results hret = CU.HttpReturn() hret.results = user_m.get_full_name() hret.status = 201 return hret
def GetClubProperties(p_club): club_m = FT.Club.objects.get(Club=p_club) club_dx = OrderedDict() club_dx['club'] = club_m.Club club_dx['full_name'] = club_m.FullName club_dx['city'] = club_m.City club_dx['founded'] = club_m.Founded hret = CU.HttpReturn() hret.results = club_dx hret.status = 201 return hret
def SearchFriend(p_searcher, p_target): hret = CU.HttpReturn() # first check if there's a user at all try: targetData = Profile_Reporter.GetUserData(p_target) except Exception as ex: hret.results = "User not found." hret.status = 201 return hret # check the searcher's friend's list try: targetUser = AM.User.objects.get(username=p_target) except AM.User.DoesNotExist: targetUser = None if targetUser: try: rel_m = MT.Relationship.objects.get(User1FK = p_searcher, User2FK = targetUser) except MT.Relationship.DoesNotExist: rel_m = None if rel_m and rel_m.RelationValue == 1: hret.results = "User is in your friend list." hret.status = 201 return hret if rel_m and rel_m.RelationValue == -1: hret.results = "User is in your ignore list." hret.status = 201 return hret # check the target's ignore list try: relRev_m = MT.Relationship.objects.get(User1FK = targetUser, User2FK = p_searcher) except MT.Relationship.DoesNotExist: relRev_m = None if relRev_m and relRev_m.RelationValue == -1: hret.results = "User has you in their ignore list." hret.status = 201 return hret # the target is a potential friend, so return their data to be displayed hret.results = targetData hret.status = 201 return hret
def GetTestUserCount(): botusers = User.objects.filter(username__icontains="botuser").annotate( event_count=Count('id')) userCnt = len(botusers) # return results res = {'userCnt': userCnt} hret = CU.HttpReturn() hret.results = res hret.status = 201 return hret
def CreateUsers(p_userNum, p_season): userNum = int(p_userNum) + 1 for u in range(1, userNum, 1): # create base user userID = "botuser" + CU.Pad5(u) email = userID + "@ufo.com" newUser = User.objects.create_user(userID, email, userID) # add profile traits at random prof_m = MT.Profile.objects.get(UserFK=newUser) geo_dx = TestUsers_Editor.GetRandomGeoData() prof_m.Country = geo_dx['country'] prof_m.TimeZone = geo_dx['timezone'] prof_m.FavClubFK = TestUsers_Editor.GetRandomClub(p_season) prof_m.Slogan = TestUsers_Editor.GetRandomSlogan() prof_m.Icon = TestUsers_Editor.CreateRandomIcon(newUser) prof_m.BotAI = randint(0, 2) prof_m.save() # verify the user's email so they can log in emdd_m, crtd = LM.EmailAddress.objects.get_or_create(user=newUser, email=email, verified=True, primary=True) # create universal prediction roster as if they had gone to a page season_m = FT.Season.objects.get(Season=p_season) rost_m, crtd = PU.Univ_Roster.objects.get_or_create( UserFK=newUser, SeasonFK=season_m) TestUsers_Editor.CreateRandomRels() # return succesfull results hret = CU.HttpReturn() hret.results = "Test Users Created" hret.status = 201 return hret