Beispiel #1
0
    def post(self):
        """
        Create new rating for a stop. Uses the request parser specified above
        :return: created rating
        """
        try:
            args = parser.parse_args()
            rating = args['rating']
            s_id = args['entity_id']
            creator = args['created_by']

            old_rating = StopRating.query.filter_by(
                stop_id=s_id, created_by=creator).first()
            if old_rating:
                old_rating.rating = rating
                db.session.commit()
                return old_rating.serialize(), 201

            rating = StopRating(rating, s_id, creator)
            if rating is None:
                return create_error(500, "Cannot create stop rating"), 500
            db.session.add(rating)
            db.session.commit()
            return rating.serialize(), 201
        except Exception as e:
            return create_error(500,
                                "cannot create rating for stop",
                                extra=e.__str__()), 500
Beispiel #2
0
    def get(self, region):
        """
        Get all the stops in a specified region
        :param region: region
        :return: list of stops that are in the specified region
        :exception: exceptions are thrown if region could not be converted to Region enum value
        """
        region = try_convert(region)
        if type(region) is int:
            try:
                region = Region(region)
                if region is None:
                    raise ValueError("Cannot convert int to Region")
            except Exception as e:
                return create_error(
                    500,
                    "Region {} does not exist. For possible values make http request to /regions/values"
                    .format(region),
                    extra=e.__str__()), 500
        else:
            try:
                region = Region[region.upper()]
                if region is None:
                    raise ValueError("Cannot convert string to Region")
            except Exception as e:
                return create_error(
                    500,
                    "Region {} does not exist. For possible values, make http request to /regions/values"
                    .format(region),
                    extra=e.__str__()), 500

        stops = Stop.query.filter_by(region=region).all()
        return [s.serialize() for s in stops], 200
Beispiel #3
0
 def post(self):
     try:
         args = login_parser.parse_args()
         password = args["password"]
         username = args["username"]
         logged_user = User.query.filter_by(name=username).first()
         if logged_user is None:
             return create_error(
                 403, "User with username '{}' does not exist".format(
                     username)), 403
         if logged_user.password != password:
             return create_error(403, "Password is incorrect"), 403
         return {'status': 'success', 'user': logged_user.serialize()}, 200
     except Exception as e:
         return create_error(500, "Cannot sign in", extra=e.__str__()), 500
Beispiel #4
0
 def get(self, s_id):
     """
     Return the stop object with a specified id.
     :param s_id: ID of the stop that needs to be returned
     :return: stop with specified ID
     :exception: exception if ID does not exist or if s_id could not be converted to an integer
     """
     try:
         s_id = int(s_id)
     except Exception as e:
         return create_error(
             500,
             "Cannot convert id '{}' to integer".format(s_id),
             extra=e.__str__()), 500
     stop = Stop.query.filter_by(id=s_id).first()
     if stop is None:
         return create_error(
             404, "Stop with id {} does not exist".format(s_id)), 404
     return stop.serialize(), 200
Beispiel #5
0
    def get(self, v_id):
        """
        Get the vehicle with the specified id
        :param v_id: id of the vehicle to search for
        :return: vehicle object with specified id
        """
        try:
            v_id = int(v_id)
        except Exception as e:
            return create_error(
                500,
                "Cannot convert id '{}' to integer".format(v_id),
                extra=e.__str__()), 500

        vehicle = Vehicle.query.filter_by(id=v_id).first()
        if vehicle is None:
            return create_error(
                404, "Vehicle with id {} does not exist".format(v_id)), 404
        return vehicle.serialize(), 200
Beispiel #6
0
 def get(self, u_id):
     try:
         u_id = int(u_id)
     except Exception as e:
         return create_error(
             500, "Cannot convert id '{}' to integer".format(u_id))
     user = User.query.filter_by(u_id=u_id).first()
     if user is None:
         return None, 404
     return user.serialize(), 200
Beispiel #7
0
 def get(self, c_id):
     """
     Gets all ratings created by a particular creator
     :param c_id: ID of the creator
     :return: list of ratings of the creator
     """
     c_id = try_convert(c_id)
     if type(c_id) is not int:
         return create_error(
             500, "Cannot convert id '{}' to integer".format(c_id)), 500
     ratings = StopRating.query.filter_by(created_by=c_id).all()
     return [r.serialize() for r in ratings]
Beispiel #8
0
 def get(self, s_id):
     """
     Get the ratings of the stop with a given id
     :param s_id: id of the stop
     :return: list of ratings
     """
     s_id = try_convert(s_id)
     if type(s_id) is not int:
         return create_error(
             500, "Cannot convert id '{}' to integer".format(s_id)), 500
     ratings = StopRating.query.filter_by(stop_id=s_id).all()
     return [r.serialize() for r in ratings], 200
Beispiel #9
0
 def post(self):
     try:
         args = register_parser.parse_args()
         password = args["password"]
         username = args["username"]
         email = args["email"]
         user = User(username, password, email)
         db.session.add(user)
         db.session.commit()
         return user.serialize(), 201
     except Exception as e:
         return create_error(500, "Cannot register", extra=e.__str__()), 500
Beispiel #10
0
    def delete(self, v_id):
        """
        Delete a vehicle with the specified id
        :param v_id: id of the vehicle
        :return: "success" if vehicle is deleted
        """
        try:
            v_id = int(v_id)
        except Exception as e:
            return create_error(
                500,
                "Cannot convert id '{}' to integer".format(v_id),
                extra=e.__str__()), 500

        vehicle = Vehicle.query.filter_by(id=v_id).first()

        if vehicle is None:
            return create_error(
                404, "vehicle with id '{}' not found".format(v_id)), 404

        db.session.delete(vehicle)
        db.session.commit()
        return "success", 200
Beispiel #11
0
 def get(self, s_id):
     """
     Gets the average rating for a stop
     :param s_id: id of the stop
     :return: "No ratings yet" if there are no ratings, else it returns the average of the ratings
     """
     s_id = try_convert(s_id)
     if type(s_id) is not int:
         return create_error(
             500, "Cannot convert id '{}' to integer".format(s_id)), 500
     average = db.session.query(func.avg(
         StopRating.rating)).filter_by(stop_id=s_id).scalar()
     if not average:
         return "No ratings yet"
     return float(average), 200
Beispiel #12
0
    def get(self, c_id):
        """
        Gets the vehicles based on the creator ID
        :param c_id: Id of the creator
        :return: list of vehicles created by the given creator
        """
        try:
            c_id = int(c_id)
        except Exception as e:
            return create_error(
                500,
                "Cannot convert id '{}' to integer".format(c_id),
                extra=e.__str__()), 500

        vehicles = Vehicle.query.filter_by(created_by=c_id).all()
        return [v.serialize() for v in vehicles]
Beispiel #13
0
 def post(self):
     """
     Add a new vehicle to the database
     :return: the created vehicle
     """
     try:
         args = parser.parse_args()
         vehicle = Vehicle(args.get('id'), args.get('number'),
                           args.get('description'), args.get('type'),
                           args.get('created_by'), args.get('name'))
         db.session.add(vehicle)
         db.session.commit()
         return vehicle.serialize(), 201
     except Exception as e:
         return create_error(
             500,
             "Unable to create vehicle with specified arguments",
             extra=e.__str__()), 500