Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #3
0
def create_race(request, username):
    user = assert_user_logged_in(username, request)
    if request.method == 'POST':
        form = forms.EditRaceForm(request.POST, user=user)
        if form.has_changed():
            if form.is_valid():
                machine = Machine.objects.get(
                    name=form.cleaned_data['machine_name'], user=user)
                config = machine.empty_configuration()

                race = Race()
                race.name = form.cleaned_data['name']
                race.date_time = form.cleaned_data['date_time']
                race.track = Track.objects.get(
                    name=form.cleaned_data['track_name'])
                race.num_laps = form.cleaned_data['num_laps']
                race.organization = form.cleaned_data['organization']
                race.machine_config = config
                race.user = user
                race.is_team = form.cleaned_data['is_team']
                # If user hits back & returns to new race form,
                # help them out by not creating a race twice or
                # failing on uniqueness constraint:
                race, created = Race.objects.get_or_create(
                    name=race.name,
                    date_time=race.date_time,
                    track=race.track,
                    num_laps=race.num_laps,
                    organization=race.organization,
                    machine_config=race.machine_config,
                    user=race.user)
                return HttpResponseRedirect(
                    reverse('laps:edit_race_laps', args=(username, race.id)))
    else:
        form = forms.EditRaceForm(user=user)
    return render(request, 'laps/new_race.html', {
        'form': form,
        'racer': user.username
    })