def get(self, current_user, r_id): """Method for get requests""" if r_id: try: ride = Ride(id=r_id) ride = ride.find_by_id(r_id) if ride: return jsonify(ride), 200 return jsonify({'msg': "Ride not found "}), 404 except Exception as e: response = {'message': str(e)} return make_response(jsonify(response)), 500 else: try: ride = Ride() rides = ride.fetch_all() if rides == []: return jsonify( {"msg": " There are no rides rides at the moment"}), 200 return jsonify(rides), 200 except Exception as e: response = {'message': str(e)} return make_response(jsonify(response)), 500
def test_follow_rides(self): # create four users u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') u3 = User(username='******', email='*****@*****.**') u4 = User(username='******', email='*****@*****.**') db.session.add_all([u1, u2, u3, u4]) # create four rides now = datetime.utcnow() r1 = Ride(start="Eldoret", destination="Kakamega", time="06/01/2018 10:00 AM", seats=2, cost=200, driver=u1, timestamp=now + timedelta(seconds=1)) r2 = Ride(start="Nakuru", destination="Naivasha", time="06/01/2018 11:00 AM", seats=4, cost=100, driver=u2, timestamp=now + timedelta(seconds=4)) r3 = Ride(start="Nairobi", destination="Thika", time="06/01/2018 2:00 PM", seats=3, cost=50, driver=u3, timestamp=now + timedelta(seconds=3)) r4 = Ride(start="Narok", destination="Nairobi", time="06/01/2018 4:00 PM", seats=2, cost=80, driver=u4, timestamp=now + timedelta(seconds=2)) db.session.add_all([r1, r2, r3, r4]) db.session.commit() # setup the followers u1.follow(u2) # john follows susan u1.follow(u4) # john follows david u2.follow(u3) # susan follows mary u3.follow(u4) # mary follows david db.session.commit() # check the followed posts of each user f1 = u1.followed_rides().all() f2 = u2.followed_rides().all() f3 = u3.followed_rides().all() f4 = u4.followed_rides().all() self.assertEqual(f1, [r2, r4, r1]) self.assertEqual(f2, [r2, r3]) self.assertEqual(f3, [r3, r4]) self.assertEqual(f4, [r4])
def dashboard(): form = RideForm() if form.validate_on_submit(): ride = Ride(start=form.start.data, destination=form.destination.data, time=form.time.data, seats=form.seats.data, cost=form.cost.data, driver=current_user) db.session.add(ride) db.session.commit() flash('Your ride is now live!') return redirect(url_for('main.dashboard')) page = request.args.get('page', 1, type=int) rides = current_user.followed_rides().paginate( page, current_app.config['RIDES_PER_PAGE'], False) next_url = url_for('main.dashboard', page=rides.next_num) \ if rides.has_next else None prev_url = url_for('main.dashboard', page=rides.prev_num) \ if rides.has_prev else None return render_template('dashboard.html', title='Home', form=form, rides=rides.items, next_url=next_url, prev_url=prev_url)
def post(self, current_user): """offers a new ride""" data = request.json origin = data['origin'] destination = data['destination'] date = data['date'] ride = Ride(origin=origin, destination=destination, date=date) try: all_rides = ride.fetch_all() for this_ride in all_rides: if this_ride['origin'] == ride.origin and this_ride[ 'destination'] == ride.destination and this_ride[ 'date'] == ride.date and this_ride[ 'driver'] == current_user[2]: response = { 'message': 'This ride already exists.', } return make_response(jsonify(response)), 202 driver = current_user[2] ride.insert(driver) response = { 'message': 'You offered a ride successfully.', } return make_response(jsonify(response)), 201 except Exception as e: response = {'message': str(e)} return make_response(jsonify(response)), 500
def create_ride(): form = CreateRideForm() cars = Car.query.filter_by(owner_id=current_user.id).all() form.cars.choices = [(car.id, car.brand) for car in cars] if form.validate_on_submit(): language = guess_language(form.about_ride.data) if language == 'UNKNOWN' or len(language) > 5: language = '' ride = Ride(driver=current_user.id, car=form.cars.data, from_location=form.from_location.data, to_location=form.to_location.data, ride_time=form.ride_time.data, free_seats=form.free_seats.data, about_ride=form.about_ride.data, language=language) db.session.add(ride) db.session.commit() flash(_('Ride was created')) return redirect(url_for('ride.available_rides')) return render_template('ride/create_ride.html', form=form)