<commit_before>from flask import Blueprint, request
from flask_restful import Resource, Api, fields, marshal, abort
from flask.ext.login import login_required, current_user
from datetime import datetime, timedelta
from sqlalchemy import exc

from steerclear.utils.eta import time_between_locations
from steerclear import sms_client, dm_client, gis_client

from steerclear.utils.permissions import (
    student_permission, 
    admin_permission, 
    AccessRidePermission
)

from models import *
from forms import *

# set up api blueprint
api_bp = Blueprint('api', __name__, url_prefix='/api')
api = Api(api_bp)

# response format for Ride objects
ride_fields = {
    'id': fields.Integer(),
    'num_passengers': fields.Integer(),
    'start_latitude': fields.Float(),
    'start_longitude': fields.Float(),
    'end_latitude': fields.Float(),
    'end_longitude': fields.Float(),
    'pickup_time': fields.DateTime(dt_format='rfc822'),
    'travel_time': fields.Integer(),
    'dropoff_time': fields.DateTime(dt_format='rfc822'),
    'pickup_address': fields.String(),
    'dropoff_address': fields.String(),
    'on_campus': fields.Boolean(), 
}

"""
RideListAPI
-----------
HTTP commands for interfacing with a list of
ride objects. uri: /rides
"""
class RideListAPI(Resource):

    # Require that users be logged in in order to access the RideListAPI
    method_decorators = [login_required]

    """
    Return the list of Ride objects in the queue

    User must be an admin to access route
    """
    @admin_permission.require(http_exception=403)
    def get(self):
        rides = Ride.query.all()                            # query db for Rides
        rides = map(Ride.as_dict, rides)                    # convert all Rides to dictionaries
        return {'rides': marshal(rides, ride_fields)}, 200  # return response

    """
    Create a new Ride object and place it in the queue
    """
    def post(self):
        form = RideForm()                       # validate RideForm or 404
        if not form.validate_on_submit():
            abort(400)
        
        # get pickup and dropoff locations of Ride request
        pickup_loc = (form.start_latitude.data, form.start_longitude.data)
        dropoff_loc = (form.end_latitude.data, form.end_longitude.data)

        # check if the pickup_location for the ride request
        # is on campus or off campus
        on_campus = gis_client.is_on_campus(pickup_loc)

        # query distance matrix api and get eta time data and addresses
        result = query_distance_matrix_api(pickup_loc, dropoff_loc)
        if result is None:
            abort(400)

        # get pickup, travel, and dropoff times
        pickup_time, travel_time, dropoff_time = result[0]<commit_msg>added logic to filter ridelist by location<commit_after>from flask import Blueprint, request
from flask_restful import Resource, Api, fields, marshal, abort, reqparse
from flask.ext.login import login_required, current_user
from datetime import datetime, timedelta
from sqlalchemy import exc

from steerclear.utils.eta import time_between_locations
from steerclear import sms_client, dm_client, gis_client

from steerclear.utils.permissions import (
    student_permission, 
    admin_permission, 
    AccessRidePermission
)

from models import *
from forms import *

# set up api blueprint
api_bp = Blueprint('api', __name__, url_prefix='/api')
api = Api(api_bp)

# response format for Ride objects
ride_fields = {
    'id': fields.Integer(),
    'num_passengers': fields.Integer(),
    'start_latitude': fields.Float(),
    'start_longitude': fields.Float(),
    'end_latitude': fields.Float(),
    'end_longitude': fields.Float(),
    'pickup_time': fields.DateTime(dt_format='rfc822'),
    'travel_time': fields.Integer(),
    'dropoff_time': fields.DateTime(dt_format='rfc822'),
    'pickup_address': fields.String(),
    'dropoff_address': fields.String(),
    'on_campus': fields.Boolean(), 
}

"""
RideListAPI
-----------
HTTP commands for interfacing with a list of
ride objects. uri: /rides
"""
class RideListAPI(Resource):

    # Require that users be logged in in order to access the RideListAPI
    method_decorators = [login_required]

    """
    Return the list of Ride objects in the queue

    User must be an admin to access route
    """
    @admin_permission.require(http_exception=403)
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('location', type=str, location='args')
        args = parser.parse_args()

        if args.get('location', '') == 'on_campus':
            # return list of all ride requests that are on campus
            on_campus_rides = Ride.query.filter_by(on_campus=True).all()
            on_campus_rides = map(Ride.as_dict, on_campus_rides)
            return {'rides': marshal(on_campus_rides, ride_fields)}, 200
        elif args.get('location', '') == 'off_campus':
            # return list of all ride requests that are off campus
            off_campus_rides = Ride.query.filter_by(on_campus=False).all()
            off_campus_rides = map(Ride.as_dict, off_campus_rides)
            return {'rides': marshal(off_campus_rides, ride_fields)}, 200
        else:
            # return list of all ride requests
            rides = Ride.query.all()                            # query db for Rides
            rides = map(Ride.as_dict, rides)                    # convert all Rides to dictionaries
            return {'rides': marshal(rides, ride_fields)}, 200  # return response

    """
    Create a new Ride object and place it in the queue
    """
    def post(self):
        form = RideForm()                       # validate RideForm or 404
        if not form.validate_on_submit():
            abort(400)
        
        # get pickup and dropoff locations of Ride request
        pickup_loc = (form.start_latitude.data, form.start_longitude.data)
        dropoff_loc = (form.end_latitude.data, form.end_longitude.data)

        # check if the pickup_location for the ride request
        # is on campus or off campus
        on_campus = gis_client.is_on_campus(pickup_loc)

        # query distance matrix api and get eta time data and addresses
        result = query_distance_matrix_api(pickup_loc, dropoff_loc)
        if result is None:
            abort(400)

        # get pickup, travel, and dropoff times
        pickup_time, travel_time, dropoff_time = result[0]
{'fraction_non_alphanumeric': 0.09425717852684144, 'fraction_numerical': 0.007802746566791511, 'mean_word_length': 2.9635126777983922, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1038549', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1857, 'n_words': 548}