def search(request): context = RequestContext(request) # Get current user and student information current_user = request.user current_student = Student.objects.get(user=current_user) # Generate ride_form from POST data ride_form = RideForm(initial={ 'seats': current_student.seats, 'driver': current_user }, data=request.POST) if ride_form.is_valid(): # Construct a ride object from ride_form ride = ride_form.save(commit=False) start = ride.start dest = ride.dest time = ride.time identity = ride.id ride.driver = current_user else: print(ride_form.errors) rides = Ride.objects.filter(start=start, dest=dest) schedule_form = ScheduleForm(ride) return render_to_response( 'rides/search.html', { 'schedule_form': schedule_form, 'ride_form': ride_form, 'start': start, 'dest': dest, 'time': time, 'rides': rides, 'identity': identity }, context)
def index(request): context = RequestContext(request) current_user = request.user scheduled_ride = "" scheduled = "" current_student = Student.objects.get(user=current_user) if (request.method == 'POST'): # Attempt to grab information from the raw form information. ride_form = RideForm(initial={'seats': current_student.seats, 'driver': current_user}, data=request.POST) # If the form is valid... if ride_form.is_valid(): ride = ride_form.save() str_start = str(ride_form.cleaned_data['start']) str_dest = str(ride_form.cleaned_data['dest']) str_time = str(ride_form.cleaned_data['time']) scheduled = "Thanks for scheduling your ride!" scheduled_ride = "Start: " + str_start + ", Dest: " + str_dest + ", Time: " + str_time # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print(ride_form.errors) # Not a HTTP POST, so we render our form using a ModelForm instance # This will be blank, ready for user input. else: ride_form = RideForm(initial={'seats': current_student.seats, 'driver': current_user}) # Render the template depending on the context. return render_to_response( 'rides/index.html', {'ride_form': ride_form, 'scheduled_ride': scheduled_ride, 'scheduled': scheduled}, context)
def ride_edit(request, ride_id, template="concierge/ride_edit.html"): ride = get_object_or_404( Ride.objects.select_related('destination').select_related('start'), pk=ride_id) customer = get_object_or_404(Customer.objects.select_related( 'user').select_related('user__profile'), pk=ride.customer.id) cancel_form = CancelRideForm( initial={ 'ride_id': ride_id, 'next_url': reverse('customer_detail', args=[customer.id]) }) errors = {} if request.method == 'GET': form = RideForm(instance=ride, customer=customer) confirmation_form = ConfirmRideForm(initial={ 'ride': ride, 'confirmed_by': request.user, }, prefix='conf') else: form = RideForm(request.POST, instance=ride, customer=customer) confirmation_form = ConfirmRideForm(request.POST, prefix='conf') if form.is_valid(): ride = form.save(commit=False) if ride.start.timezone: # get timezone object for customer start_tz = pytz.timezone(ride.start.timezone) # convert default (pac) datetime to naive naived_start_date = ride.start_date.replace(tzinfo=None) # re-localize datetime to customer's timezone localized_start_date = start_tz.localize(naived_start_date) # set start_date to re-localized datetime ride.start_date = localized_start_date ride.save() if 'is_confirmed' in request.POST and confirmation_form.is_valid(): confirmation_form.save() messages.success(request, "Ride confirmed") messages.success(request, "Ride saved successfully") else: errors = form.errors d = { 'customer': customer, 'ride': ride, 'form': form, 'cancel_form': cancel_form, 'confirmation_form': confirmation_form, 'ride_page': True, 'errors': errors } return render(request, template, d)
def ride_edit(request, id): ride = get_object_or_404(Ride.objects.filter(user=request.user), id=id) if request.method == "POST": form = RideForm(request.POST, instance=ride) if form.is_valid(): ride = form.save(commit=False) ride.save() return redirect('ride_detail', id=ride.id) else: form = RideForm(instance=ride) return render(request, 'rides/ride_edit.html', {'form': form})
def ride_new(request): if request.method == "POST": form = RideForm(request.POST) if form.is_valid(): ride = form.save(commit=False) ride.user = request.user print("******") print(ride) print('*****') ride.save() return redirect(ride_detail, id=ride.id) else: form = RideForm() return render(request, 'rides/ride_edit.html', {'form': form})
def ride_add(request, event_id): context = get_event_details(request, event_id) if request.method == 'POST': form = RideForm(request.POST) if form.is_valid(): ride = form.save(commit=False) ride.event_id = event_id ride.event_name = context["name"] ride.save() ride.riders.add(get_object_or_404(Rider, user=request.user)) return HttpResponseRedirect(reverse('rides:event_details', args=(event_id,))) else: form = RideForm() context = {'form':form, 'geo': context['geo']} return render(request, 'rides/add.html', context)
def index(request): context = RequestContext(request) current_user = request.user scheduled_ride = "" scheduled = "" current_student = Student.objects.get(user=current_user) if (request.method == 'POST'): # Attempt to grab information from the raw form information. ride_form = RideForm(initial={ 'seats': current_student.seats, 'driver': current_user }, data=request.POST) # If the form is valid... if ride_form.is_valid(): ride = ride_form.save() str_start = str(ride_form.cleaned_data['start']) str_dest = str(ride_form.cleaned_data['dest']) str_time = str(ride_form.cleaned_data['time']) scheduled = "Thanks for scheduling your ride!" scheduled_ride = "Start: " + str_start + ", Dest: " + str_dest + ", Time: " + str_time # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print(ride_form.errors) # Not a HTTP POST, so we render our form using a ModelForm instance # This will be blank, ready for user input. else: ride_form = RideForm(initial={ 'seats': current_student.seats, 'driver': current_user }) # Render the template depending on the context. return render_to_response( 'rides/index.html', { 'ride_form': ride_form, 'scheduled_ride': scheduled_ride, 'scheduled': scheduled }, context)
def search(request): context = RequestContext(request) # Get current user and student information current_user = request.user current_student = Student.objects.get(user=current_user) # Generate ride_form from POST data ride_form = RideForm(initial={'seats': current_student.seats, 'driver': current_user}, data=request.POST) if ride_form.is_valid(): # Construct a ride object from ride_form ride = ride_form.save(commit=False) start = ride.start dest = ride.dest time = ride.time identity = ride.id ride.driver = current_user else: print(ride_form.errors) rides = Ride.objects.filter(start=start, dest=dest) schedule_form = ScheduleForm(ride) return render_to_response( 'rides/search.html', {'schedule_form': schedule_form, 'ride_form': ride_form, 'start': start, 'dest': dest, 'time': time, 'rides': rides, 'identity': identity}, context)
def postride(request): context = RequestContext(request) if request.method == 'POST': #To Load the Form data in necessary Form Objects sourceAddressForm = AddressForm(data=request.POST, prefix='source') destinationAddressForm = AddressForm(data=request.POST, prefix='destination') rideForm = RideForm(data=request.POST, prefix='ride') driverForm = DriverForm(data=request.POST, prefix='driver') if rideForm.is_valid() and sourceAddressForm.is_valid( ) and destinationAddressForm.is_valid(): #Crate model instances for the model objects ride = rideForm.save(commit=False) #Process the date and time startTime = rideForm.cleaned_data['rideStartTime'] if startTime == 'anytime' or startTime == 'early' or startTime == 'morning' or startTime == 'afternoon' or startTime == 'evening' or startTime == 'night': ride.ride_startDateTime = processDatetime( rideForm.cleaned_data['rideStartDate'], '12:00 AM') ride.ride_startPref = str(startTime) else: ride.ride_startDateTime = processDatetime( rideForm.cleaned_data['rideStartDate'], startTime) returnTime = rideForm.cleaned_data['rideReturnTime'] if returnTime == 'anytime' or returnTime == 'early' or returnTime == 'morning' or returnTime == 'afternoon' or returnTime == 'evening' or returnTime == 'night': ride.ride_returnDateTime = processDatetime( rideForm.cleaned_data['rideReturnDate'], '12:00 AM') ride.ride_returnPref = str(returnTime) else: ride.ride_returnDateTime = processDatetime( rideForm.cleaned_data['rideReturnDate'], returnTime) source, dummy = Address.objects.get_or_create( add_address=sourceAddressForm.cleaned_data['add_address']) destination, dummy = Address.objects.get_or_create( add_address=destinationAddressForm.cleaned_data['add_address']) ride.add_source = source ride.add_destination = destination ride.save() who = request.POST.get('ride-type', 'False') if who == 'True': if driverForm.is_valid(): driver = driverForm.save(commit=False) driver.ride_id = ride driver.user_id = request.user driver.save() else: driverForm.errors else: rider = Rider(ride_id=ride, user_id=request.user) rider.save() return HttpResponseRedirect('/rides/ridematches/' + str(ride.pk)) else: print rideForm.errors, sourceAddressForm.errors, destinationAddressForm.errors else: rideForm = RideForm(prefix='ride') sourceAddressForm = AddressForm(prefix='source') destinationAddressForm = AddressForm(prefix='destination') driverForm = DriverForm(prefix='driver') form_list = { 'rideForm': rideForm, 'sourceAddressForm': sourceAddressForm, 'destinationAddressForm': destinationAddressForm, 'driverForm': driverForm, } return render_to_response('rides/postride.html', form_list, context)
def postride(request): context = RequestContext(request) if request.method == "POST": # To Load the Form data in necessary Form Objects sourceAddressForm = AddressForm(data=request.POST, prefix="source") destinationAddressForm = AddressForm(data=request.POST, prefix="destination") rideForm = RideForm(data=request.POST, prefix="ride") driverForm = DriverForm(data=request.POST, prefix="driver") if rideForm.is_valid() and sourceAddressForm.is_valid() and destinationAddressForm.is_valid(): # Crate model instances for the model objects ride = rideForm.save(commit=False) # Process the date and time startTime = rideForm.cleaned_data["rideStartTime"] if ( startTime == "anytime" or startTime == "early" or startTime == "morning" or startTime == "afternoon" or startTime == "evening" or startTime == "night" ): ride.ride_startDateTime = processDatetime(rideForm.cleaned_data["rideStartDate"], "12:00 AM") ride.ride_startPref = str(startTime) else: ride.ride_startDateTime = processDatetime(rideForm.cleaned_data["rideStartDate"], startTime) returnTime = rideForm.cleaned_data["rideReturnTime"] if ( returnTime == "anytime" or returnTime == "early" or returnTime == "morning" or returnTime == "afternoon" or returnTime == "evening" or returnTime == "night" ): ride.ride_returnDateTime = processDatetime(rideForm.cleaned_data["rideReturnDate"], "12:00 AM") ride.ride_returnPref = str(returnTime) else: ride.ride_returnDateTime = processDatetime(rideForm.cleaned_data["rideReturnDate"], returnTime) source, dummy = Address.objects.get_or_create(add_address=sourceAddressForm.cleaned_data["add_address"]) destination, dummy = Address.objects.get_or_create( add_address=destinationAddressForm.cleaned_data["add_address"] ) ride.add_source = source ride.add_destination = destination ride.save() who = request.POST.get("ride-type", "False") if who == "True": if driverForm.is_valid(): driver = driverForm.save(commit=False) driver.ride_id = ride driver.user_id = request.user driver.save() else: driverForm.errors else: rider = Rider(ride_id=ride, user_id=request.user) rider.save() return HttpResponseRedirect("/rides/ridematches/" + str(ride.pk)) else: print rideForm.errors, sourceAddressForm.errors, destinationAddressForm.errors else: rideForm = RideForm(prefix="ride") sourceAddressForm = AddressForm(prefix="source") destinationAddressForm = AddressForm(prefix="destination") driverForm = DriverForm(prefix="driver") form_list = { "rideForm": rideForm, "sourceAddressForm": sourceAddressForm, "destinationAddressForm": destinationAddressForm, "driverForm": driverForm, } return render_to_response("rides/postride.html", form_list, context)