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 rideform(auth_dict=None): # Time to prepopulate the datetime field loc_dt = datetime.datetime.now(tz=eastern) st = loc_dt.strftime(fmt) form = RideForm() if form.validate_on_submit(): name = form.name.data address = form.address.data start_time = datetime.datetime(int(form.start_date_time.data.year), int(form.start_date_time.data.month), int(form.start_date_time.data.day), int(form.start_date_time.data.hour), int(form.start_date_time.data.minute)) end_time = datetime.datetime(int(form.end_date_time.data.year), int(form.end_date_time.data.month), int(form.end_date_time.data.day), int(form.end_date_time.data.hour), int(form.end_date_time.data.minute)) creator = auth_dict['uid'] ride = Ride(name, address, start_time, end_time, creator) db.session.add(ride) db.session.commit() infinity = Car('∞', 'Need a Ride', 0, 0, start_time, end_time, "", ride.id) db.session.add(infinity) db.session.commit() return redirect(url_for('index_auth')) return render_template('rideform.html', form=form, timestamp=st, auth_dict=auth_dict)
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_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 editrideform(rideid, auth_dict=None): username = auth_dict['uid'] ride = Ride.query.get(rideid) if username == ride.creator and ride is not None: form = RideForm() if form.validate_on_submit(): ride.name = form.name.data ride.address = form.address.data ride.start_time = datetime.datetime( int(form.start_date_time.data.year), int(form.start_date_time.data.month), int(form.start_date_time.data.day), int(form.start_date_time.data.hour), int(form.start_date_time.data.minute)) ride.end_time = datetime.datetime( int(form.end_date_time.data.year), int(form.end_date_time.data.month), int(form.end_date_time.data.day), int(form.end_date_time.data.hour), int(form.end_date_time.data.minute)) ride.creator = auth_dict['uid'] car = Car.query.filter(Car.ride_id == rideid).filter( Car.name == "Need a Ride").first() car.departure_time = datetime.datetime( int(form.start_date_time.data.year), int(form.start_date_time.data.month), int(form.start_date_time.data.day), int(form.start_date_time.data.hour), int(form.start_date_time.data.minute)) car.return_time = datetime.datetime( int(form.end_date_time.data.year), int(form.end_date_time.data.month), int(form.end_date_time.data.day), int(form.end_date_time.data.hour), int(form.end_date_time.data.minute)) db.session.commit() return redirect(url_for('index_auth')) return render_template('editrideform.html', form=form, ride=ride, auth_dict=auth_dict)
def rideform(auth_dict=None): form = RideForm() print(form.start_date.data) print(form.start_time.data) if form.validate_on_submit(): name = form.name.data address = form.address.data start_time = datetime.datetime(int(form.start_date.data['year']), int(form.start_date.data['month']), int(form.start_date.data['day']), int(form.start_time.data['hour']), int(form.start_time.data['minute'])) end_time = datetime.datetime(int(form.end_date.data['year']), int(form.end_date.data['month']), int(form.end_date.data['day']), int(form.end_time.data['hour']), int(form.end_time.data['minute'])) creator = auth_dict['uid'] ride = Ride(name, address, start_time, end_time, creator) db.session.add(ride) db.session.commit() return redirect(url_for('index')) return render_template('rideform.html', form=form, auth_dict=auth_dict)
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 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 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 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 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)