def userprofile_show( request, user_id ): ''' Shows all RideRequests and RideOffers for a particular user ''' profile = get_mongo_or_404(UserProfile, pk=user_id) my_offers = RideOffer.objects.filter( driver=profile, completed=False ) my_requests = RideRequest.objects.filter( passenger=profile ) rides_requested, rides_offered, ride_requests_completed, ride_offers_completed = [], [], [], [] now = datetime.now() for req in my_requests: if req.date < now: if req.ride_offer: ride_requests_completed.append( req ) else: rides_requested.append( req ) for o in my_offers: if o.date < now: if not len(o.passengers) == 0: ride_offers_completed.append( o ) else: rides_offered.append( o ) # Show the user their home page if they are the logged-in user if request.session.get('profile') == profile: return render_to_response( "user_home.html", locals(), context_instance=RequestContext(request) ) # Put other context variables for a user's home page here... return render_to_response( "user_detail.html", locals(), context_instance=RequestContext(request) )
def process_offer_update(request, offer_id): ''' Render and process the offer update form ''' ride_offer = get_mongo_or_404( RideOffer, pk=ObjectId(offer_id) ) # Confirm correct user if not request.session.get('profile') == RideOffer.objects.get(pk=ObjectId(offer_id)).driver: raise PermissionDenied if request.method =='POST': form = OfferOptionsForm(request.POST) # Form validates if form.is_valid(): data = form.cleaned_data ride_offer = RideOffer.objects.get(pk=ObjectId(data['offer_id'])) # Parse out the form and update RideOffer if data['message']: ride_offer.message = data['message'] ride_offer.save() # render the form return render_to_response('offer_options.html', locals(), context_instance=RequestContext(request)) if ride_offer.message: message = ride_offer.message form = OfferOptionsForm(initial={'offer_id':offer_id, 'message':message}) else: form = OfferOptionsForm(initial={'offer_id':offer_id}) return render_to_response('offer_options.html', locals(), context_instance=RequestContext(request))
def process_request_update(request, request_id): ''' Render and process the request update form ''' ride_request = get_mongo_or_404( RideRequest, pk=ObjectId(request_id) ) # confirm correct user if not request.session.get('profile') == RideRequest.objects.get(pk=ObjectId(request_id)).passenger: raise PermissionDenied if request.method == 'POST': form = RequestOptionsForm(request.POST) # Form validates if form.is_valid(): data = form.cleaned_data # Parse out the form and update RideRequest if data['message']: ride_request.message = data['message'] ride_request.save() return render_to_response('request_options.html', locals(), context_instance=RequestContext(request)) if RideRequest.objects.get(pk=ObjectId(request_id)).message: message = RideRequest.objects.get(pk=ObjectId(request_id)) form = RequestOptionsForm(initial={'request_id':request_id, 'message':message}) else: form = RequestOptionsForm(initial={'request_id':request_id}) return render_to_response('request_options.html', locals(), context_instance=RequestContext(request))
def offer_show( request, offer_id ): ''' Renders a page displaying more information about a particular RideOffer ''' ride_offer = get_mongo_or_404( RideOffer, pk=ObjectId(offer_id) ) # This information is used in the template to determine if the user has already # requested a ride from this RideOffer user_profile = request.session.get("profile") if not user_profile in ride_offer.askers+ride_offer.passengers and user_profile != ride_offer.driver: # Find RideOffers the logged-in user has made that would work well with this request if user_profile: searchParams = { 'polygon': ride_offer.polygon, 'date': ride_offer.date, 'fuzziness': ride_offer.fuzziness, 'other_filters': {'passenger':user_profile} } requests = _request_search( **searchParams ) import sys sys.stderr.write("request search in offer_show returned the following: %s"%str(requests)) requests = [(str(req.id),str(req)) for req in requests] form = AskForRideForm(initial={'offer_id':offer_id}, request_choices=requests) else: form = AskForRideForm(initial={'offer_id':offer_id}) return render_to_response( 'ride_offer.html', locals(), context_instance=RequestContext(request) )
def request_show( request, request_id ): ''' Renders a page displaying more information about a particular RideRequest ''' ride_request = get_mongo_or_404( RideRequest, pk=ObjectId(request_id) ) # This information is used in the template to determine if the user has already # offered a ride to this RideRequest user_profile = request.session.get("profile") def is_driver(): if not ride_request.ride_offer: return False return user_profile == ride_request.ride_offer.driver if not user_profile in ride_request.askers and user_profile != ride_request.passenger and not is_driver(): # Find RideOffers the logged-in user has made that would work well with this request if user_profile: # TODO: Make this neater? searchParams = {} searchParams['start_lat'],searchParams['start_lng'] = ride_request.start.position searchParams['end_lat'],searchParams['end_lng'] = ride_request.end.position searchParams['date'] = ride_request.date searchParams['fuzziness'] = ride_request.fuzziness import sys sys.stderr.write("my other offers: %s\n"%str([offer for offer in user_profile.offers])) searchParams['other_filters'] = { 'id__in' : tuple([offer.id for offer in user_profile.offers]) } offers = _offer_search( **searchParams ) offers = [(str(offer.id),str(offer)) for offer in offers] form = OfferRideForm(initial={'request_id':request_id}, offer_choices=offers) else: form = OfferRideForm(initial={'request_id':request_id}) return render_to_response( 'ride_request.html', locals(), context_instance=RequestContext(request) )
def driver_feedback( request, offer_id ): offer = get_mongo_or_404( RideOffer, pk=ObjectId(offer_id) ) # Submitting feedback if request.method == 'POST': profile = request.session.get("profile") def fail( msg ): ''' What to do when we get an error ''' messages.add_message( request, messages.ERROR, msg ) return HttpResponseRedirect( reverse('user_landing') ) # Must be the driver of this RideOffer if profile != offer.driver: return fail( "Cannot leave feedback on that trip." ) # Make sure no feedback has already been left for this trip if offer.completed: return fail( "You have already left feedback for that trip." ) form = DriverFeedbackForm( offer, request.POST ) if form.is_valid(): data = form.cleaned_data passengers = { p:data[p] for p in data if p.startswith("passenger_") } group_message = data['group_fb'] # Make sure these passengers were those on the trip form_ids = sorted([p.split('_')[1] for p in passengers.keys()]) actual_ids = sorted([str(p.id) for p in offer.passengers]) if form_ids != actual_ids: return fail( "Cannot leave feedback on that trip because the feedback left for one or more passengers was invalid." ) # Increment trust rating for all passengers, and send emails for name, val in passengers.iteritems(): passenger = UserProfile.objects.get( pk=ObjectId(name.split("_")[1]) ) trust = Trust(message=val, truster=profile, offer=offer) if len(passenger.trust) == 0: passenger.trust = [trust] else: passenger.trust.append(trust) passenger.save() if len(group_message) > 0 or len(val) > 0: if len(group_message) > 0 and len(val)>0: email_body = "%s\r\n\r\nAdditionally, your driver says:\r\n\r\n%s"%( group_message, val ) elif len(group_message) > 0: email_body = group_message elif len(val) > 0: email_body = val send_email( email_to=passenger.user.username, email_subject="Correspondence on your trip %s"%str(offer), email_body=email_body ) # Mark this trip as having been reviewed already offer.completed = True offer.save() messages.add_message( request, messages.SUCCESS, "Your correspondence has been recorded." ) return HttpResponseRedirect( reverse('user_landing') ) form = DriverFeedbackForm( offer, initial={'offer_id':str(offer.id)} ) return render_to_response("driver_feedback.html", locals(), context_instance=RequestContext(request) )