def motolaptimes_as_model(parsed_content, user): ''' converts parsed motolaptimes content to model objects and persists them ''' if not('date' in parsed_content): parsed_content['date'] = '9/4/15' if not('time' in parsed_content): parsed_content['time'] = '12:00PM' if not('name' in parsed_content): parsed_content['name'] = 'Unnamed Race' date_str = "%s %s" % (parsed_content['date'], parsed_content['time']) print "date_str=%s" % date_str date = datetime.datetime.strptime(date_str, '%m/%d/%y %H:%M%p') num_laps = len(parsed_content['laps']) r = Race(user=user, name=parsed_content['name'], date_time=date, num_laps=num_laps) r.save() for i in range(num_laps): lapstr = parsed_content['laps'][i] lapstr = lapstr.replace('.', ':') laptime = util.interpret_time(lapstr) lap, created = Lap.objects.get_or_create(race=r, num=i+1, time=laptime) return r
def motolaptimes_as_model(parsed_content, user): ''' converts parsed motolaptimes content to model objects and persists them ''' if not ('date' in parsed_content): parsed_content['date'] = '9/4/15' if not ('time' in parsed_content): parsed_content['time'] = '12:00PM' if not ('name' in parsed_content): parsed_content['name'] = 'Unnamed Race' date_str = "%s %s" % (parsed_content['date'], parsed_content['time']) print "date_str=%s" % date_str date = datetime.datetime.strptime(date_str, '%m/%d/%y %H:%M%p') num_laps = len(parsed_content['laps']) r = Race(user=user, name=parsed_content['name'], date_time=date, num_laps=num_laps) r.save() for i in range(num_laps): lapstr = parsed_content['laps'][i] lapstr = lapstr.replace('.', ':') laptime = util.interpret_time(lapstr) lap, created = Lap.objects.get_or_create(race=r, num=i + 1, time=laptime) return r
def test_simple_interpret_time(self): num_seconds = util.interpret_time('1:30:500') self.assertTrue((num_seconds - Decimal(90.5)) < 0.0001)
def edit_race_laps(request, username, race_id): user = assert_user_logged_in(username, request) race = get_object_or_404(Race, pk=race_id) changes = RiderChange.objects.filter(race=race) if not (race.user == user): raise PermissionDenied laps = Lap.objects.filter(race__id=race_id) if race.num_laps == 0: # No laps to enter/edit, so just return to the race page return HttpResponseRedirect( reverse('laps:race', args=(username, race.id))) if request.method == 'POST': # TODO: include notification to say update was successful form = forms.EditLapsForm(request.POST, num_laps=race.num_laps, laps=laps, race=race) if form.is_valid(): if race.is_team: changes.delete() lap_dict = form.get_lap_dict() for lap_num in xrange( 1, form.num_laps + 1): # TODO: Maybe delete all laps and recreate? if not (lap_num in lap_dict) or not ( lap_dict[lap_num]): # no lap time given try: lap = Lap.objects.get(race=race, num=lap_num) lap.delete() except Lap.DoesNotExist: pass # Lap doesn't exist? No problem else: # lap time given for this lap number lap_time_s = util.interpret_time(lap_dict[lap_num]) try: lap = Lap.objects.get(race=race, num=lap_num) # Update exist lap: lap.time = lap_time_s except Lap.DoesNotExist: # Create a new lap: lap, created = Lap.objects.get_or_create( race=race, num=lap_num, time=lap_time_s) except Lap.MultipleObjectsReturned: # TODO: had an issue with race that had same lap number twice... # TODO: maybe ensure uniqueness here? Lap.objects.filter(race=race, num=lap_num).delete() lap, created = Lap.objects.get_or_create( race=race, num=lap_num, time=lap_time_s) lap.save() if race.is_team: fieldname = "rider_change_name_lap%d" % lap_num if fieldname in request.POST: rider_change_name = request.POST[fieldname] fieldname = "rider_change_user_lap%d" % lap_num if fieldname in request.POST: rider_change_user = request.POST[fieldname] if rider_change_name or rider_change_user: print "found a change [%s, %s]" % (rider_change_name, rider_change_user) change = RiderChange() change.race = race if rider_change_user: change.user = get_user_model().objects.get( username=rider_change_user) if rider_change_name: change.rider_name = rider_change_name change.num = lap_num change.save() return HttpResponseRedirect( reverse('laps:race', args=(username, race.id))) else: form = forms.EditLapsForm(num_laps=race.num_laps, laps=laps, race=race) return render( request, 'laps/edit_laps.html', { 'form': form, 'race': race, 'racer': user.username, 'users': get_user_model().objects.all() })