def edit_machine(request, username, machine_id): user = assert_user_logged_in(username, request) machine = get_object_or_404(Machine, pk=machine_id) if not (machine.user == user): raise PermissionDenied if request.method == 'POST': form = laps.forms.EditMachineForm(request.POST) if form.has_changed(): if form.is_valid(): machine.name = form.cleaned_data['name'] machine.make = form.cleaned_data['make'] machine.model = form.cleaned_data['model'] machine.year = form.cleaned_data['year'] machine.save() return HttpResponseRedirect( reverse('laps:machine', args=(username, machine.id))) else: initial_form_values = machine.__dict__ form = laps.forms.EditMachineForm(initial_form_values) return render(request, 'laps/edit_machine.html', { 'form': form, 'machine': machine, 'racer': username })
def import_race_from_lap_text(request, username): user = assert_user_logged_in(username, request) if request.method == 'POST': form = forms.ImportLapsFromTextForm(request.POST) if form.has_changed(): if form.is_valid(): laptext = form.cleaned_data['url'] parsed_content = lapimport.extract_from_text(laptext) race = lapimport.motolaptimes_as_model(parsed_content, user) return HttpResponseRedirect( reverse('laps:race', args=(user.username, race.id))) else: return HttpResponse(status=405)
def add_config_attr_to_race(request, username, race_id): user = assert_user_logged_in(username, request) race = get_object_or_404(Race, pk=race_id) if not (race.user == user): raise PermissionDenied if request.method == 'POST': form = forms.AddConfigurationAttributeToRaceForm(request.POST) if form.has_changed(): if form.is_valid(): key = form.cleaned_data['key'] value = form.cleaned_data['value'] attr, created = ConfigurationAttribute.objects.get_or_create( key=key, value=value) attr.machine_configurations.add(race.machine_config) attr.save() return HttpResponseRedirect(reverse('laps:race', args=(username, race.id)))
def create_machine(request, username): user = assert_user_logged_in(username, request) if request.method == 'POST': form = laps.forms.EditMachineForm(request.POST) if form.has_changed(): if form.is_valid(): machine = Machine() machine.name = form.cleaned_data['name'] machine.make = form.cleaned_data['make'] machine.model = form.cleaned_data['model'] machine.year = form.cleaned_data['year'] machine.user = user machine.save() return HttpResponseRedirect(reverse('laps:machines', args=(username,))) else: form = laps.forms.EditMachineForm() return render(request, 'laps/new_machine.html', { 'form':form, 'racer': user.username })
def edit_machine(request, username, machine_id): user = assert_user_logged_in(username, request) machine = get_object_or_404(Machine, pk=machine_id) if not(machine.user == user): raise PermissionDenied if request.method == 'POST': form = laps.forms.EditMachineForm(request.POST) if form.has_changed(): if form.is_valid(): machine.name = form.cleaned_data['name'] machine.make = form.cleaned_data['make'] machine.model = form.cleaned_data['model'] machine.year = form.cleaned_data['year'] machine.save() return HttpResponseRedirect(reverse('laps:machine', args=(username, machine.id))) else: initial_form_values = machine.__dict__ form = laps.forms.EditMachineForm(initial_form_values) return render(request, 'laps/edit_machine.html', { 'form':form, 'machine':machine, 'racer': username })
def import_race(request, username): user = assert_user_logged_in(username, request) if request.method == 'POST': form = forms.ImportMotolaptimesForm(request.POST) if form.has_changed(): if form.is_valid(): motolaptimes_url = form.cleaned_data['url'] parsed_content = lapimport.extract_from_motolaptimes( motolaptimes_url) race = lapimport.motolaptimes_as_model(parsed_content, user) return HttpResponseRedirect( reverse('laps:race', args=(user.username, race.id))) else: form = forms.ImportMotolaptimesForm() text_form = forms.ImportLapsFromTextForm() return render(request, 'laps/import_race.html', { 'form': form, 'text_form': text_form, 'racer': user.username })
def edit_race(request, username, race_id): user = assert_user_logged_in(username, request) race = get_object_or_404(Race, pk=race_id) if not (race.user == user): raise PermissionDenied if request.method == 'POST': form = forms.EditRaceForm(request.POST, user=user) if form.has_changed(): if form.is_valid(): 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.is_team = form.cleaned_data['is_team'] if (race.machine_config is None) or not (race.machine_config.machine.name == form.cleaned_data['machine_name']): # The machine was changed machine = Machine.objects.get( name=form.cleaned_data['machine_name'], user=user) race.machine_config = machine.empty_configuration() race.save() return HttpResponseRedirect( reverse('laps:edit_race_laps', args=(username, race.id))) else: initial_form_values = race.__dict__ if race.machine_config: initial_form_values[ 'machine_name'] = race.machine_config.machine.name if race.track: initial_form_values['track_name'] = race.track.name initial_form_values['num_laps'] = race.num_laps form = forms.EditRaceForm(initial_form_values, user=user) return render(request, 'laps/edit_race.html', { 'form': form, 'race': race, 'racer': username })
def create_machine(request, username): user = assert_user_logged_in(username, request) if request.method == 'POST': form = laps.forms.EditMachineForm(request.POST) if form.has_changed(): if form.is_valid(): machine = Machine() machine.name = form.cleaned_data['name'] machine.make = form.cleaned_data['make'] machine.model = form.cleaned_data['model'] machine.year = form.cleaned_data['year'] machine.user = user machine.save() return HttpResponseRedirect( reverse('laps:machines', args=(username, ))) else: form = laps.forms.EditMachineForm() return render(request, 'laps/new_machine.html', { 'form': form, 'racer': user.username })
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 })
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() })