def create(): data = request.get_json() schema = ItinerarySchema(strict=True) data, errors = schema.load(data) if errors: return jsonify({'errors': errors}), 400 itinerary = Itinerary(owner=g.user['email'], drivers=[], passengers=[], **data) result = Itinerary.insert(itinerary, return_changes=True) itinerary = Itinerary(**result['changes'][0]['new_val']) g.user.insert_itinerary(itinerary) Driver.update(g.user) return jsonify({'id': itinerary.get_id()}), 201
def auth_token(): token = request.headers.get('Authorization') if not token: return payload = User.decode_token(token, app.config['SECRET_KEY']) g.user = User.get_by_email(payload['email']) if g.user.is_driver: g.user = Driver(**g.user) if g.user.is_passenger: g.user = Passenger(**g.user)
class ItineraryCreateViewTestCase(AppTestCase): def setUp(self): self.test_driver = Driver(email='*****@*****.**') Driver.insert(self.test_driver) secret_key = self.app.config['SECRET_KEY'] self.test_driver_token = self.test_driver.generate_token(secret_key) def test_create_itinerary(self): url = url_for('itinerary.create') response = self.client.post(url, data=json.dumps({ 'name': 'IT123', 'timestart': '06:00', 'endtime': '07:00', 'destination': { 'street': 'Street 1', 'neighborhood': 'Neigh 1', 'city': 'City 1', 'state': 'AB', 'latitude': -27.4578, 'longitude': -45.8796 }, 'weekdays': 'Seg Ter Qua Qui Sex' }), headers={ 'Content-Type': 'application/json', 'Authorization': self.test_driver_token }) self.assertEqual(response.status_code, 201) itinerary = Itinerary.find_one({ 'name': 'IT123', 'timestart': '06:00', 'endtime': '07:00', 'destination': { 'street': 'Street 1', 'neighborhood': 'Neigh 1', 'city': 'City 1', 'state': 'AB', 'latitude': -27.4578, 'longitude': -45.8796 }, 'weekdays': 'Seg Ter Qua Qui Sex', 'owner': '*****@*****.**', 'drivers': [] }) self.assertIsNotNone(itinerary)
def update_driver_location(itinerary_id): data = request.get_json() itinerary = Itinerary.get_by_key(itinerary_id) if not itinerary: return jsonify({'errors': ['Itinerary not found']}), 404 driver = Driver.get_by_email(g.user['email']) if not driver: return jsonify({'errors': ['Driver not found.']}), 404 itinerary.update_driver_location(data) Itinerary.update(itinerary) return jsonify({'id': itinerary_id}), 201
def test_create_driver(self): url = url_for('drivers.create') response = self.client.post(url, data=json.dumps({ 'name': 'driver1', 'email': '*****@*****.**', 'password': '******', 'phone': '12345678' }), headers={ 'Content-Type': 'application/json', }) self.assertEqual(response.status_code, 201) driver = Driver.find_one({ 'name': 'driver1', 'email': '*****@*****.**', 'phone': '12345678' }) self.assertIsNotNone(driver)
def add_partner(itinerary_id): data = request.get_json() print(data) itinerary = Itinerary.get_by_key(itinerary_id) if not itinerary: return jsonify({'errors': ['Itinerary not found']}), 404 '''schema = ItineraryAddPartnerSchema(strict=True) data, errors = schema.load(data) if errors: return jsonify({ 'errors': errors }), 409''' driver = Driver.get_by_email(data['name']) if not driver: return jsonify({'errors': ['Driver not found.']}), 404 itinerary.add_partner(driver) Itinerary.update(itinerary) return jsonify({'id': itinerary_id}), 201
def setUp(self): super(DriverAuthViewTestCase, self).setUp() self.user_driver = Driver(name='Test', email='*****@*****.**') self.user_driver.set_password('secret123') Driver.insert(self.user_driver)
def setUp(self): self.test_driver = Driver(email='*****@*****.**') Driver.insert(self.test_driver) secret_key = self.app.config['SECRET_KEY'] self.test_driver_token = self.test_driver.generate_token(secret_key)
def list_drivers(): drivers = Driver.get_all_drivers(Driver.USER_TYPE) return jsonify({"objects": list(drivers)}), 201