diff --git a/.env b/.env index 87db316..2da71c5 100755 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ SECRET_KEY = 'key' DEBUG = 'True' -DB_URI = 'cockroachdb://root@127.0.0.1:/movr' +DB_URI = 'cockroachdb://root@127.0.0.1:26257/movr' API_KEY = 'API_key' diff --git a/README.md b/README.md index effbb71..689ea80 100644 --- a/README.md +++ b/README.md @@ -74,23 +74,14 @@ The steps below walk you through how to start up an insecure, virtual nine-node Once the database finishes initializing, you should see a SQL shell prompt: ~~~ - root@127.0.0.1:/movr> + root@127.0.0.1:26257/movr> ~~~ Keep this terminal window open. Closing it will shut down the virtual cluster. - - Also take note of the value for ``, as you'll be using that for the next step. 1. Configure environment variables - - Because we use `` in a few places, let's save the port from the previous step - into a shell environment variable named `MOVR_PORT`: - - ~~~ - export MOVR_PORT= - ~~~ - The MovR application also uses Google Maps Static API, so we'll also store your + The MovR application uses Google Maps Static API, so we'll also store your Google API Key into an environment variable as well: ~~~ @@ -168,7 +159,7 @@ For local deployment and development, use [`pipenv`](https://pypi.org/project/pi This lets the application read values from an environement variable, rather than us needing to hard-code values directly into the source code. In [Database and Environment Setup](#database-and-environment-setup) section, you ran `./init.sh` which - set the `DB_URI` and `API_KEY` keys in your `.env` file: + set the `DB_URI` and `API_KEY` variables in your `.env` file: - `DB_URI` is the [SQL connection string](https://en.wikipedia.org/wiki/Connection_string) needed for SQLAlchemy to connect to CockroachDB. Note that SQLAlchemy requires the connection string protocol to be specific to the CockroachDB dialect. - `API_KEY` should be your Google Static Maps API Key. diff --git a/dbinit.sql b/dbinit.sql index 0554cc6..455733e 100644 --- a/dbinit.sql +++ b/dbinit.sql @@ -1,140 +1,123 @@ -SET sql_safe_updates = false; +SET sql_safe_updates = FALSE; +DROP DATABASE IF EXISTS movr CASCADE; -DROP DATABASE IF EXISTS movr CASCADE; - - -CREATE DATABASE movr; - +CREATE DATABASE movr PRIMARY REGION "gcp-us-east1" REGIONS "gcp-europe-west1", +"gcp-us-west1"; USE movr; - CREATE TABLE IF NOT EXISTS users ( - id UUID NOT NULL DEFAULT gen_random_uuid(), - city STRING NOT NULL, - first_name STRING NULL, - last_name STRING NULL, - email STRING NULL, - username STRING NULL, - password_hash STRING NULL, - is_owner BOOL NULL, - CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - CONSTRAINT check_city CHECK (city IN ('amsterdam','boston','los angeles','new york','paris','rome','san francisco','seattle','washington dc')), - UNIQUE INDEX users_username_key (username ASC), - FAMILY "primary" (id, city, first_name, last_name, email, username, password_hash, is_owner) -) PARTITION BY LIST (city) ( - PARTITION us_west VALUES IN (('seattle'), ('san francisco'), ('los angeles')), - PARTITION us_east VALUES IN (('new york'), ('boston'), ('washington dc')), - PARTITION europe_west VALUES IN (('amsterdam'), ('paris'), ('rome')) -); - -ALTER PARTITION europe_west OF INDEX movr.public.users@primary CONFIGURE ZONE USING - constraints = '[+region=gcp-europe-west1]'; -ALTER PARTITION us_east OF INDEX movr.public.users@primary CONFIGURE ZONE USING - constraints = '[+region=gcp-us-east1]'; -ALTER PARTITION us_west OF INDEX movr.public.users@primary CONFIGURE ZONE USING - constraints = '[+region=gcp-us-west1]' -; - + id uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid (), + city STRING NOT NULL, + first_name STRING NULL, + last_name STRING NULL, + email STRING NULL, + username STRING NULL, + password_hash STRING NULL, + is_owner bool NULL, + region crdb_internal_region AS ( + CASE WHEN city = 'amsterdam' THEN + 'gcp-europe-west1' + WHEN city = 'paris' THEN + 'gcp-europe-west1' + WHEN city = 'rome' THEN + 'gcp-europe-west1' + WHEN city = 'new york' THEN + 'gcp-us-east1' + WHEN city = 'boston' THEN + 'gcp-us-east1' + WHEN city = 'washington dc' THEN + 'gcp-us-east1' + WHEN city = 'san francisco' THEN + 'gcp-us-west1' + WHEN city = 'seattle' THEN + 'gcp-us-west1' + WHEN city = 'los angeles' THEN + 'gcp-us-west1' + ELSE + 'gcp-us-east1' + END) STORED, + UNIQUE INDEX users_username_key (username ASC)) LOCALITY REGIONAL BY + ROW; CREATE TABLE IF NOT EXISTS vehicles ( - id UUID NOT NULL DEFAULT gen_random_uuid(), - city STRING NOT NULL, - type STRING NULL, - owner_id UUID NULL, - date_added DATE NULL, - status STRING NULL, - last_location STRING NULL, - color STRING NULL, - brand STRING NULL, - CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - CONSTRAINT check_city CHECK (city IN ('amsterdam','boston','los angeles','new york','paris','rome','san francisco','seattle','washington dc')), - CONSTRAINT fk_city_ref_users FOREIGN KEY (city, owner_id) REFERENCES users(city, id), - INDEX vehicles_auto_index_fk_city_ref_users (city ASC, owner_id ASC, status ASC) PARTITION BY LIST (city) ( - PARTITION us_west VALUES IN (('seattle'), ('san francisco'), ('los angeles')), - PARTITION us_east VALUES IN (('new york'), ('boston'), ('washington dc')), - PARTITION europe_west VALUES IN (('amsterdam'), ('paris'), ('rome')) - ), - FAMILY "primary" (id, city, type, owner_id, date_added, status, last_location, color, brand) -) PARTITION BY LIST (city) ( - PARTITION us_west VALUES IN (('seattle'), ('san francisco'), ('los angeles')), - PARTITION us_east VALUES IN (('new york'), ('boston'), ('washington dc')), - PARTITION europe_west VALUES IN (('amsterdam'), ('paris'), ('rome')) -); - -ALTER PARTITION europe_west OF INDEX movr.public.vehicles@primary CONFIGURE ZONE USING - constraints = '[+region=gcp-europe-west1]'; -ALTER PARTITION us_east OF INDEX movr.public.vehicles@primary CONFIGURE ZONE USING - constraints = '[+region=gcp-us-east1]'; -ALTER PARTITION us_west OF INDEX movr.public.vehicles@primary CONFIGURE ZONE USING - constraints = '[+region=gcp-us-west1]'; -ALTER PARTITION europe_west OF INDEX movr.public.vehicles@vehicles_auto_index_fk_city_ref_users CONFIGURE ZONE USING - constraints = '[+region=gcp-europe-west1]'; -ALTER PARTITION us_east OF INDEX movr.public.vehicles@vehicles_auto_index_fk_city_ref_users CONFIGURE ZONE USING - constraints = '[+region=gcp-us-east1]'; -ALTER PARTITION us_west OF INDEX movr.public.vehicles@vehicles_auto_index_fk_city_ref_users CONFIGURE ZONE USING - constraints = '[+region=gcp-us-west1]' -; - + id uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid (), + city STRING NOT NULL, + type STRING NULL, + owner_id uuid NULL, + date_added date NULL, + status STRING NULL, + last_location STRING NULL, + color STRING NULL, + brand STRING NULL, + region crdb_internal_region AS ( + CASE WHEN city = 'amsterdam' THEN + 'gcp-europe-west1' + WHEN city = 'paris' THEN + 'gcp-europe-west1' + WHEN city = 'rome' THEN + 'gcp-europe-west1' + WHEN city = 'new york' THEN + 'gcp-us-east1' + WHEN city = 'boston' THEN + 'gcp-us-east1' + WHEN city = 'washington dc' THEN + 'gcp-us-east1' + WHEN city = 'san francisco' THEN + 'gcp-us-west1' + WHEN city = 'seattle' THEN + 'gcp-us-west1' + WHEN city = 'los angeles' THEN + 'gcp-us-west1' + ELSE + 'gcp-us-east1' + END) STORED, + CONSTRAINT fk_ref_users FOREIGN KEY (owner_id) REFERENCES users (id)) LOCALITY REGIONAL BY + ROW; CREATE TABLE rides ( - id UUID NOT NULL DEFAULT gen_random_uuid(), - city STRING NOT NULL, - vehicle_id UUID NULL, - rider_id UUID NULL, - rider_city STRING NOT NULL, - start_location STRING NULL, - end_location STRING NULL, - start_time TIMESTAMPTZ NULL, - end_time TIMESTAMPTZ NULL, - length INTERVAL NULL, - CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - CONSTRAINT check_city CHECK (city IN ('amsterdam','boston','los angeles','new york','paris','rome','san francisco','seattle','washington dc')), - CONSTRAINT fk_city_ref_users FOREIGN KEY (rider_city, rider_id) REFERENCES users(city, id), - CONSTRAINT fk_vehicle_city_ref_vehicles FOREIGN KEY (city, vehicle_id) REFERENCES vehicles(city, id), - INDEX rides_auto_index_fk_city_ref_users (rider_city ASC, rider_id ASC) PARTITION BY LIST (rider_city) ( - PARTITION us_west VALUES IN (('seattle'), ('san francisco'), ('los angeles')), - PARTITION us_east VALUES IN (('new york'), ('boston'), ('washington dc')), - PARTITION europe_west VALUES IN (('amsterdam'), ('paris'), ('rome')) - ), - INDEX rides_auto_index_fk_vehicle_city_ref_vehicles (city ASC, vehicle_id ASC) PARTITION BY LIST (city) ( - PARTITION us_west VALUES IN (('seattle'), ('san francisco'), ('los angeles')), - PARTITION us_east VALUES IN (('new york'), ('boston'), ('washington dc')), - PARTITION europe_west VALUES IN (('amsterdam'), ('paris'), ('rome')) - ), - FAMILY "primary" (id, city, rider_id, rider_city, vehicle_id, start_location, end_location, start_time, end_time, length) -) PARTITION BY LIST (city) ( - PARTITION us_west VALUES IN (('seattle'), ('san francisco'), ('los angeles')), - PARTITION us_east VALUES IN (('new york'), ('boston'), ('washington dc')), - PARTITION europe_west VALUES IN (('amsterdam'), ('paris'), ('rome')) -); -ALTER PARTITION europe_west OF INDEX movr.public.rides@primary CONFIGURE ZONE USING - constraints = '[+region=gcp-europe-west1]'; -ALTER PARTITION us_east OF INDEX movr.public.rides@primary CONFIGURE ZONE USING - constraints = '[+region=gcp-us-east1]'; -ALTER PARTITION us_west OF INDEX movr.public.rides@primary CONFIGURE ZONE USING - constraints = '[+region=gcp-us-west1]'; -ALTER PARTITION europe_west OF INDEX movr.public.rides@rides_auto_index_fk_city_ref_users CONFIGURE ZONE USING - constraints = '[+region=gcp-europe-west1]'; -ALTER PARTITION us_east OF INDEX movr.public.rides@rides_auto_index_fk_city_ref_users CONFIGURE ZONE USING - constraints = '[+region=gcp-us-east1]'; -ALTER PARTITION us_west OF INDEX movr.public.rides@rides_auto_index_fk_city_ref_users CONFIGURE ZONE USING - constraints = '[+region=gcp-us-west1]'; -ALTER PARTITION europe_west OF INDEX movr.public.rides@rides_auto_index_fk_vehicle_city_ref_vehicles CONFIGURE ZONE USING - constraints = '[+region=gcp-europe-west1]'; -ALTER PARTITION us_east OF INDEX movr.public.rides@rides_auto_index_fk_vehicle_city_ref_vehicles CONFIGURE ZONE USING - constraints = '[+region=gcp-us-east1]'; -ALTER PARTITION us_west OF INDEX movr.public.rides@rides_auto_index_fk_vehicle_city_ref_vehicles CONFIGURE ZONE USING - constraints = '[+region=gcp-us-west1]' -; - - -INSERT INTO users (id, city, first_name, last_name, email, username) VALUES - ('2804df7c-d8fd-4b1c-9799-b1d44452554b', 'new york', 'Carl', 'Kimball', 'carl@cockroachlabs.com', 'carl'); - -INSERT INTO vehicles (id, city, type, owner_id, date_added, status, last_location, color, brand) VALUES - ('142b7c9e-6227-4dbb-b188-b1dac57d5521', 'new york', 'scooter', '2804df7c-d8fd-4b1c-9799-b1d44452554b', current_date(),'available', 'Time Square', 'Blue', 'Razor'); + id uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid (), + city STRING NOT NULL, + vehicle_id uuid NULL, + rider_id uuid NULL, + start_location STRING NULL, + end_location STRING NULL, + start_time timestamptz NULL, + end_time timestamptz NULL, + length interval NULL, + region crdb_internal_region AS ( + CASE WHEN city = 'amsterdam' THEN + 'gcp-europe-west1' + WHEN city = 'paris' THEN + 'gcp-europe-west1' + WHEN city = 'rome' THEN + 'gcp-europe-west1' + WHEN city = 'new york' THEN + 'gcp-us-east1' + WHEN city = 'boston' THEN + 'gcp-us-east1' + WHEN city = 'washington dc' THEN + 'gcp-us-east1' + WHEN city = 'san francisco' THEN + 'gcp-us-west1' + WHEN city = 'seattle' THEN + 'gcp-us-west1' + WHEN city = 'los angeles' THEN + 'gcp-us-west1' + ELSE + 'gcp-us-east1' + END) STORED, + CONSTRAINT fk_city_ref_users FOREIGN KEY (rider_id) REFERENCES users (id), + CONSTRAINT fk_vehicle_ref_vehicles FOREIGN KEY (vehicle_id) REFERENCES vehicles (id)) LOCALITY REGIONAL BY + ROW; + +INSERT INTO users (id, city, first_name, last_name, email, username) + VALUES ('2804df7c-d8fd-4b1c-9799-b1d44452554b', 'new york', 'Carl', 'Kimball', 'carl@cockroachlabs.com', 'carl'); + +INSERT INTO vehicles (id, city, type, owner_id, date_added, status, last_location, color, brand) + VALUES ('142b7c9e-6227-4dbb-b188-b1dac57d5521', 'new york', 'scooter', '2804df7c-d8fd-4b1c-9799-b1d44452554b', current_date(), 'available', 'Time Square', 'Blue', 'Razor'); + +INSERT INTO rides (city, rider_id, vehicle_id, start_location, end_location, start_time, end_time, length) + VALUES ('new york', '2804df7c-d8fd-4b1c-9799-b1d44452554b', '142b7c9e-6227-4dbb-b188-b1dac57d5521', 'Cockroach Labs, 23rd Street', 'Time Square', '2020-01-16 21:20:48.224453+00:00', '2020-01-16 21:20:52.045813+00:00', '00:00:03.82136'); -INSERT INTO rides(city, rider_id, rider_city, vehicle_id, start_location, end_location, start_time, end_time, length) VALUES - ('new york', '2804df7c-d8fd-4b1c-9799-b1d44452554b', 'new york', '142b7c9e-6227-4dbb-b188-b1dac57d5521', 'Cockroach Labs, 23rd Street', 'Time Square', '2020-01-16 21:20:48.224453+00:00', '2020-01-16 21:20:52.045813+00:00', '00:00:03.82136'); diff --git a/init.sh b/init.sh index 695d991..561297f 100755 --- a/init.sh +++ b/init.sh @@ -1,16 +1,11 @@ #!/bin/bash # Loads dbinit.sql into your running CockroachDB cluster -cockroach sql --insecure --url="postgres://root@127.0.0.1:$MOVR_PORT" < dbinit.sql +cockroach sql --insecure --url="postgres://root@127.0.0.1:26257" < dbinit.sql # Resets .env file, clearing out any variables that were previously set git checkout -- .env -# replace with $MOVR_PORT in `.env` for the following env variable: -# DB_URI = 'cockroachdb://root@127.0.0.1:/movr' -# where DB_URI is the SQL connection string needed for SQLAlchemy to connect to CockroachDB. -sed "s//$MOVR_PORT/" .env > temp - # replace API_key with $MOVR_MAPS_API in `.env` for the following env variable: # API_KEY = 'API_key' # where API_KEY is Google Maps Static API Key needed to generate maps on the Vehicles page. diff --git a/movr/models.py b/movr/models.py index bd090df..46ed123 100755 --- a/movr/models.py +++ b/movr/models.py @@ -62,7 +62,7 @@ class Vehicle(Base): last_location = Column(String) color = Column(String) brand = Column(String) - PrimaryKeyConstraint(city, id) + PrimaryKeyConstraint(id) def __repr__(self): return "".format( @@ -83,14 +83,13 @@ class Ride(Base): id = Column(UUID) city = Column(String, ForeignKey('vehicles.city')) rider_id = Column(UUID, ForeignKey('users.id')) - rider_city = Column(String, ForeignKey('users.city')) vehicle_id = Column(UUID, ForeignKey('vehicles.id')) start_location = Column(String) end_location = Column(String) start_time = Column(DateTime) end_time = Column(DateTime) length = Column(Interval) - PrimaryKeyConstraint(city, id) + PrimaryKeyConstraint(id) def __repr__(self): return "".format( diff --git a/movr/movr.py b/movr/movr.py index b066b73..ccc960f 100644 --- a/movr/movr.py +++ b/movr/movr.py @@ -11,6 +11,7 @@ class MovR: """ Wraps the database connection. The class methods wrap database transactions. """ + def __init__(self, conn_string): """ Establish a connection to the database, creating Engine and Sessionmaker objects. @@ -21,32 +22,30 @@ def __init__(self, conn_string): self.engine = create_engine(conn_string, convert_unicode=True) self.sessionmaker = sessionmaker(bind=self.engine) - def start_ride(self, city, rider_id, rider_city, vehicle_id): + def start_ride(self, city, rider_id, vehicle_id): """ Wraps a `run_transaction` call that starts a ride. Arguments: city {String} -- The ride's city. rider_id {UUID} -- The user's unique ID. - rider_city {String} -- The user's city. vehicle_id {UUID} -- The vehicle's unique ID. """ return run_transaction( self.sessionmaker, lambda session: start_ride_txn( - session, city, rider_id, rider_city, vehicle_id)) + session, city, rider_id, vehicle_id)) - def end_ride(self, city, ride_id, location): + def end_ride(self, ride_id, location): """ Wraps a `run_transaction` call that ends a ride. Arguments: - city {String} -- The ride's city. ride_id {UUID} -- The ride's unique ID. location {String} -- The vehicle's last location. """ return run_transaction( self.sessionmaker, - lambda session: end_ride_txn(session, city, ride_id, location)) + lambda session: end_ride_txn(session, ride_id, location)) def add_user(self, city, first_name, last_name, email, username, password): """ @@ -65,39 +64,29 @@ def add_user(self, city, first_name, last_name, email, username, password): lambda session: add_user_txn(session, city, first_name, last_name, email, username, password)) - def remove_user(self, city, user_id): + def remove_user(self, user_id): """ Wraps a `run_transaction` call that "removes" a user. No rows are deleted by this function. Arguments: - city {String} -- The user's city. id {UUID} -- The user's unique ID. """ return run_transaction( self.sessionmaker, - lambda session: remove_user_txn(session, city, user_id)) + lambda session: remove_user_txn(session, user_id)) - def remove_vehicle(self, city, vehicle_id): + def remove_vehicle(self, vehicle_id): """ Wraps a `run_transaction` call that "removes" a vehicle. No rows are deleted by this function. Arguments: - city {String} -- The vehicle's city. id {UUID} -- The vehicle's unique ID. """ return run_transaction( self.sessionmaker, - lambda session: remove_vehicle_txn(session, city, vehicle_id)) - - def add_vehicle(self, - city, - owner_id, - last_location, - type, - color, - brand, - status, - is_owner=False): + lambda session: remove_vehicle_txn(session, vehicle_id)) + + def add_vehicle(self, city, owner_id, last_location, type, color, brand, status, is_owner=False): """ Wraps a `run_transaction` call that adds a vehicle. diff --git a/movr/transactions.py b/movr/transactions.py index 66b056a..a3dacd3 100644 --- a/movr/transactions.py +++ b/movr/transactions.py @@ -3,7 +3,7 @@ import uuid -def start_ride_txn(session, city, rider_id, rider_city, vehicle_id): +def start_ride_txn(session, city, rider_id, vehicle_id): """ Insert a new row into the rides table and update a row of the vehicles table. @@ -14,33 +14,32 @@ def start_ride_txn(session, city, rider_id, rider_city, vehicle_id): rider_city {String} -- The city in which the rider is registered. vehicle_id {UUID} -- The vehicle's unique ID. """ - v = session.query(Vehicle).filter(Vehicle.city == city, - Vehicle.id == vehicle_id).first() - r = Ride(city=city, - id=str(uuid.uuid4()), - rider_id=rider_id, - rider_city=rider_city, - vehicle_id=vehicle_id, - start_location=v.last_location, - start_time=datetime.datetime.now(datetime.timezone.utc)) + v = session.query(Vehicle).filter(Vehicle.id == vehicle_id).first() + r = Ride( + city=city, + id=str( + uuid.uuid4()), + rider_id=rider_id, + vehicle_id=vehicle_id, + start_location=v.last_location, + start_time=datetime.datetime.now( + datetime.timezone.utc)) + session.add(r) v.status = "unavailable" -def end_ride_txn(session, city, ride_id, location): +def end_ride_txn(session, ride_id, location): """ Update a row of the rides table, and update a row of the vehicles table. Arguments: session {.Session} -- The active session for the database connection. - city {String} -- The ride's city. ride_id {UUID} -- The ride's unique ID. location {String} -- The vehicle's last location. """ - r = session.query(Ride).filter(Ride.city == city, - Ride.id == ride_id).first() - v = session.query(Vehicle).filter(Vehicle.city == city, - Vehicle.id == r.vehicle_id).first() + r = session.query(Ride).filter(Ride.id == ride_id).first() + v = session.query(Vehicle).filter(Vehicle.id == r.vehicle_id).first() r.end_location = location r.end_time = datetime.datetime.now(datetime.timezone.utc) r.length = r.end_time - r.start_time @@ -48,15 +47,16 @@ def end_ride_txn(session, city, ride_id, location): v.status = "available" -def add_user_txn(session, - city, - first_name, - last_name, - email, - username, - password, - password_hash=None, - is_owner=False): +def add_user_txn( + session, + city, + first_name, + last_name, + email, + username, + password, + password_hash=None, + is_owner=False): """ Insert a new row into the users table. The password passed to the function is not stored. Instead, the `set_password` method hashes the string and stores the hash. @@ -84,21 +84,28 @@ def add_user_txn(session, session.add(u) -def remove_user_txn(session, city, id): +def remove_user_txn(session, id): """ Update a row in the users table. This function does not delete a row. Arguments: session {.Session} -- The active session for the database connection. - city {String} -- The user's city. id {UUID} -- The user's unique ID. """ - u = session.query(User).filter(User.city == city, User.id == id).first() + u = session.query(User).filter(User.id == id).first() u.username = None -def add_vehicle_txn(session, city, owner_id, last_location, type, color, brand, - status, is_owner): +def add_vehicle_txn( + session, + city, + owner_id, + last_location, + type, + color, + brand, + status, + is_owner): """ Insert a row into the vehicles table, and update a row in the users table. @@ -114,40 +121,37 @@ def add_vehicle_txn(session, city, owner_id, last_location, type, color, brand, is_owner {bool} -- The owner status of the user, before the vehicle is added. """ vehicle_type = type - v = Vehicle(id=str(uuid.uuid4()), - type=vehicle_type, - city=city, - owner_id=owner_id, - last_location=last_location, - color=color, - brand=brand, - status=status) + v = Vehicle( + id=str( + uuid.uuid4()), + type=vehicle_type, + city=city, + owner_id=owner_id, + last_location=last_location, + color=color, + brand=brand, + status=status) session.add(v) if not is_owner: - u = session.query(User).filter(User.city == city, - User.id == v.owner_id).first() + u = session.query(User).filter(User.id == v.owner_id).first() u.is_owner = True -def remove_vehicle_txn(session, city, id): +def remove_vehicle_txn(session, id): """ Update a row of the vehicles table. This function does not delete a row. Arguments: session {.Session} -- The active session for the database connection. - city {String} -- The vehicle's city. id {UUID} -- The vehicle's unique ID. """ - v = session.query(Vehicle).filter(Vehicle.city == city, - Vehicle.id == id).first() + v = session.query(Vehicle).filter(Vehicle.id == id).first() v.status = 'removed' owner = v.owner_id vehicles = session.query(Vehicle).filter( - Vehicle.city == city, Vehicle.owner_id == owner, - Vehicle.status != 'removed').all() + Vehicle.owner_id == owner, Vehicle.status != 'removed').all() if not vehicles: - u = session.query(User).filter(User.city == city, - User.id == owner).first() + u = session.query(User).filter(User.id == owner).first() u.is_owner = False @@ -162,18 +166,15 @@ def get_users_txn(session, city): Returns: List -- A list of dictionaries containing user information. """ - users = session.query(User).filter(User.city == city, - User.username is not None).all() - return list( - map( - lambda user: { - 'city': user.city, - 'id': user.id, - 'name': user.username, - 'first_name': user.first_name, - 'last_name': user.last_name, - 'is_owner': user.is_owner - }, users)) + users = session.query(User).filter( + User.city == city, User.username is not None).all() + return list(map(lambda user: {'city': user.city, + 'id': user.id, + 'name': user.username, + 'first_name': user.first_name, + 'last_name': user.last_name, + 'is_owner': user.is_owner}, + users)) def get_user_txn(session, username=None, user_id=None): @@ -223,8 +224,8 @@ def get_vehicles_txn(session, city): 'status': vehicle.status, 'date_added': vehicle.date_added, 'color': vehicle.color, - 'brand': vehicle.brand - }, vehicles)) + 'brand': vehicle.brand}, + vehicles)) def get_rides_txn(session, rider_id): @@ -238,16 +239,13 @@ def get_rides_txn(session, rider_id): Returns: List -- A list of dictionaries containing ride information. """ - rides = session.query(Ride).filter(Ride.rider_id == rider_id).order_by( - Ride.start_time).all() - return list( - map( - lambda ride: { - 'city': ride.city, - 'id': ride.id, - 'vehicle_id': ride.vehicle_id, - 'start_time': ride.start_time, - 'end_time': ride.end_time, - 'rider_id': ride.rider_id, - 'length': ride.length - }, rides)) + rides = session.query(Ride).filter( + Ride.rider_id == rider_id).order_by(Ride.start_time).all() + return list(map(lambda ride: {'city': ride.city, + 'id': ride.id, + 'vehicle_id': ride.vehicle_id, + 'start_time': ride.start_time, + 'end_time': ride.end_time, + 'rider_id': ride.rider_id, + 'length': ride.length}, + rides)) diff --git a/server.py b/server.py index 46aa615..fc8f74f 100755 --- a/server.py +++ b/server.py @@ -190,7 +190,7 @@ def user(user_id): @login_required @app.route('/users/remove/', methods=['POST']) def remove_user(user_id): - movr.remove_user(city=current_user.city, user_id=user_id) + movr.remove_user(user_id=user_id) logout_user() session['riding'] = None flash('You have successfully deleted your account.') @@ -243,7 +243,7 @@ def add_vehicle(): @login_required @app.route('/vehicles/remove/', methods=['POST']) def remove_vehicle(vehicle_id): - movr.remove_vehicle(city=current_user.city, vehicle_id=vehicle_id) + movr.remove_vehicle(vehicle_id=vehicle_id) flash('You have successfully removed a vehicle.') return redirect('{0}{1}{2}'.format( url_for('users', _external=True, _scheme=protocol), '/', @@ -287,7 +287,6 @@ def start_ride(vehicle_id): pass movr.start_ride(city=session['city'], rider_id=current_user.id, - rider_city=current_user.city, vehicle_id=vehicle_id) session['riding'] = True flash('Ride started.') @@ -303,8 +302,7 @@ def start_ride(vehicle_id): def end_ride(ride_id): try: form = EndRideForm() - movr.end_ride(city=session['city'], - ride_id=ride_id, + movr.end_ride(ride_id=ride_id, location=form.location.data) session['riding'] = False flash('Ride ended.')