def add(request, year, season_round, division_id): ladder_object = get_object_or_404(Ladder, division=division_id, season__start_date__year=year, season__season_round=season_round) result = Result(ladder=ladder_object, date_added=datetime.datetime.now()) try: next_ladder = Ladder.objects.get(season=ladder_object.season, division=ladder_object.division + 1) except Ladder.DoesNotExist: next_ladder = False try: previous_ladder = Ladder.objects.get(season=ladder_object.season, division=ladder_object.division - 1) except Ladder.DoesNotExist: previous_ladder = False # process or generate te score adding form if request.POST: form = AddResultForm(ladder_object, request.POST, instance=result) if form.is_valid(): losing_result = form.save(commit=False) # check for existing results, assume update if find a match aka delete try: existing_player_result = Result.objects.get(ladder=ladder_object, player=losing_result.player, opponent=losing_result.opponent) existing_player_result.delete() existing_opponent_result = Result.objects.get(ladder=ladder_object, player=losing_result.opponent, opponent=losing_result.player) existing_opponent_result.delete() except Result.DoesNotExist: pass # build the mirror result (aka winner) from losing form data winning_result = Result(ladder=losing_result.ladder, player=losing_result.opponent, opponent=losing_result.player, result=9, date_added=losing_result.date_added, inaccurate_flag=losing_result.inaccurate_flag) losing_result.save() winning_result.save() return HttpResponseRedirect(reverse('ladder:add', args=( ladder_object.season.start_date.year, ladder_object.season.season_round, ladder_object.division))) else: form = AddResultForm(ladder_object, instance=result) # prepare the results for displaying results = Result.objects.filter(ladder=ladder_object) results_dict = {} for result in results: results_dict.setdefault(result.player.id, []).append(result) return render(request, 'ladder/ladder/add.html', {'ladder': ladder_object, 'results_dict': results_dict, 'form': form, 'next_ladder': next_ladder, 'previous_ladder': previous_ladder})
def add_result(request, ladder_id): ladder = get_object_or_404(Ladder, pk=ladder_id) try: inaccurate = request.POST['checkbox_inaccurate'] except: inaccurate = 0 try: player_object = Player.objects.get(id=request.POST['player']) opponent_object = Player.objects.get(id=request.POST['opponent']) player_score = request.POST['player_score'] opponent_score = request.POST['opponent_score'] if int(player_score) != 9 and int(opponent_score) != 9: raise Exception("No winner selected") if int(player_score) == 9 and int(opponent_score) == 9: raise Exception("Can't have two winners") try: result_object = Result.objects.get(ladder=ladder, player=player_object, opponent=opponent_object) result_object.delete() raise Exception("want to add all the time") except: player_result_object = Result(ladder=ladder, player=player_object, opponent=opponent_object, result=player_score, date_added=datetime.datetime.now(), inaccurate_flag=inaccurate) player_result_object.save() try: result_object = Result.objects.get(ladder=ladder, player=opponent_object, opponent=player_object) result_object.delete() raise Exception("want to add all the time") except: opp_result_object = Result(ladder=ladder, player=opponent_object, opponent=player_object, result=opponent_score, date_added=datetime.datetime.now(), inaccurate_flag=inaccurate) opp_result_object.save() except Exception as e: return render(request, 'ladder/ladder/add.html', { 'ladder': ladder, 'error_message': e, 'points': range(10) }) else: return HttpResponseRedirect( reverse('ladder:add', args=(ladder.season.start_date.year, ladder.season.season_round, ladder.division)))
elif words[2] != '3': first_name = words[1] last_name = words[2] season_object = Season.objects.get(pk=season_pk) position = words[0] for k, r in results.iteritems(): opp_last_name = player_list[division_no][str(k)]['sname'] opp_first_name = player_list[division_no][str(k)]['fname'] if r != '-': try: ladder_object = Ladder.objects.get(season=season_object, division=int(division_no)) except: print 'No ladder matching: ' + season_pk + ' ' + division_no try: player_object = Player.objects.get(first_name=first_name, last_name=last_name) except: print 'No match for player: ' + first_name + last_name try: opp_object = Player.objects.get(first_name=opp_first_name, last_name=opp_last_name) except: print 'No match for opp: ' + opp_first_name + opp_last_name #print str(season_pk) + ' ' +division_no + ': ' + first_name + last_name + ' vs ' +opp_first_name + opp_last_name + ' ' + str(r) result_object = Result(ladder=ladder_object, player=player_object, opponent=opp_object, result=int(r), date_added=datetime.date(2013, 03, 29)) result_object.save() results.clear() score_pos.clear()
def handle(self, *args, **options): # make arg checks if options['file'] is False: raise CommandError('--file location is not set') file_location = options['file'] if os.access(file_location, os.R_OK) is False: raise CommandError('Directory (%s) is not writeable, change in code' % file_location) # make arg checks if options['season'] is False: raise CommandError('--season id option not set') book = open_workbook(file_location) season = Season.objects.get(pk=options['season']) sh1 = book.sheet_by_index(0) # sheet1, aways first sheet player_list = defaultdict(dict) # initialize defaultdict for our player list. current_div = {} # set the division counter to 0 # save all players then set up ladder for rownum in xrange(sh1.nrows): rows = sh1.row_values(rownum) #Find out division numbers if not rows[0] and rows[1] != 'NAME' and rows[1] != 'ROUND': for div in rows: if isinstance(div, float): current_div = ('%.2f' % div).rstrip('0').rstrip('.') try: ladder = Ladder.objects.get(season=season, division=current_div) except: ladder = Ladder(season=season, division=current_div, ladder_type="First to 9") ladder.save() #save players if rows[0] and rows[1] != 'NAME': position = rows[0] first_name = rows[1] last_name = rows[2] #populate our array for later use player_list[current_div][position] = {'first_name': first_name, 'last_name': last_name} try: player_object = Player.objects.get(first_name=first_name, last_name=last_name) except: print 'No match for player: ' + first_name + last_name try: player_object = Player(first_name=first_name, last_name=last_name) player_object.save() except: print 'Failed to create player: ' + first_name + last_name try: league_object = League.objects.get(ladder=ladder, player=player_object) except League.MultipleObjectsReturned: pass except: league_object = League.objects.create(ladder=ladder, player=player_object, sort_order=position * 10) print 'built good' current_div = {} # reset the division counter to 0 # save the results for rownum in xrange(sh1.nrows): try: rows = sh1.row_values(rownum) #Well lazy copy pasta for division info if not rows[0] and rows[1] != 'NAME' and rows[1] != 'ROUND': for div in rows: if isinstance(div, float): current_div = ('%.2f' % div).rstrip('0').rstrip('.') print current_div if rows[1] == 'NAME': i = 3 while rows[i] != 'Div': # flag to end scores normally 'Div' i += 1 count = i - 3 if rows[0]: try: position = rows[0] first_name = rows[1] last_name = rows[2] except: print 'index error for row: ' + rows[0] for c in xrange(count): if rows[c + 3] != '': score = rows[c + 3] if not isinstance(score, float): continue opp_last_name = player_list[current_div][c + 1]['last_name'] opp_first_name = player_list[current_div][c + 1]['first_name'] try: ladder_object = Ladder.objects.get(season=season, division=current_div) except: print 'No ladder matching: ' + ' ' + unicode(current_div) break try: player_object = Player.objects.get(first_name=first_name, last_name=last_name) except: print 'No match for player: ' + first_name + last_name try: opp_object = Player.objects.get(first_name=opp_first_name, last_name=opp_last_name) except: print 'No match for opp: ' + opp_first_name + opp_last_name try: result_object = Result.objects.get(ladder=ladder_object, player=player_object, opponent=opp_object) except Result.MultipleObjectsReturned: pass except: result_object = Result(ladder=ladder_object, player=player_object, opponent=opp_object, result=score, date_added=season.end_date) result_object.save() except Exception, e: print 'problem @ row: ' + rownum.__str__() + e.__str__() raise CommandError(e.__str__())
opp_last_name = player_list[current_div][c + 1]['last_name'] opp_first_name = player_list[current_div][c + 1]['first_name'] #print str(id) + ' vs ' + str(c) + ' score: ' + str(rows[c+3]) try: ladder_object = Ladder.objects.get(season=season, division=int(current_div)) except: print 'No ladder matching: ' + ' ' + str(current_div) break try: player_object = Player.objects.get(first_name=first_name, last_name=last_name) except: print 'No match for player: ' + first_name + last_name try: opp_object = Player.objects.get(first_name=opp_first_name, last_name=opp_last_name) except: print 'No match for opp: ' + opp_first_name + opp_last_name #print str(season_pk) + ' ' +division_no + ': ' + first_name + last_name + ' vs ' +opp_first_name + opp_last_name + ' ' + str(r) try: result_object = Result.objects.get(ladder=ladder_object, player=player_object, opponent=opp_object) except Result.MultipleObjectsReturned: pass except: result_object = Result(ladder=ladder_object, player=player_object, opponent=opp_object, result=score, date_added=season.end_date) result_object.save() except Exception, e: print 'problem @ row: ' + rownum.__str__() + e.message
for k, r in results.iteritems(): opp_last_name = player_list[division_no][str(k)]['sname'] opp_first_name = player_list[division_no][str(k)]['fname'] if r != '-': try: ladder_object = Ladder.objects.get( season=season_object, division=int(division_no)) except: print 'No ladder matching: ' + season_pk + ' ' + division_no try: player_object = Player.objects.get(first_name=first_name, last_name=last_name) except: print 'No match for player: ' + first_name + last_name try: opp_object = Player.objects.get(first_name=opp_first_name, last_name=opp_last_name) except: print 'No match for opp: ' + opp_first_name + opp_last_name #print str(season_pk) + ' ' +division_no + ': ' + first_name + last_name + ' vs ' +opp_first_name + opp_last_name + ' ' + str(r) result_object = Result(ladder=ladder_object, player=player_object, opponent=opp_object, result=int(r), date_added=datetime.date(2013, 03, 29)) result_object.save() results.clear() score_pos.clear()