Example #1
0
    def get(self):
        if not WhiteTokenModel.is_jti_whitelisted(get_raw_jwt()["jti"]):
            return {'message': 'Not logged in'}, 205
        try:
            print("Starting", flush=True)
            current_user = get_jwt_identity()
            userid = request.args.get('userid')
            tripid = request.args.get('tripid')
            print("Got data", flush=True)

            # If a tripid is provided, it will return just that trip
            if (tripid):
                print("tripid was provided", flush=True)
                trip = Trip.find_by_tid(int(tripid))
                print("Found trip", flush=True)
                # Making sure that the user asking for the trip has access to it, either because the user owns it, or is friends with the owner
                isFriends = Friends.find_by_uid_and_fid(
                    current_user, trip.user_id)
                if (trip.user_id != current_user and isFriends == None):
                    print("No access", flush=True)
                    return {
                        "message": "You do not have access to that trip"
                    }, 203
                elif (isFriends.friend_status == "accepted"
                      or trip.user_id == current_user):
                    print("Returning", flush=True)
                    return {
                        "message":
                        "The trip with id {} was found".format(tripid),
                        "trips": [trip.trip_json],
                        "tid": trip.trip_id,
                        "username": User.find_by_uid(trip.user_id).user_name
                    }, 200

            if (not userid):
                userId = current_user
            else:
                userId = userid

            if (current_user == userId
                    or Friends.find_by_uid_and_fid(current_user, userId)):
                all_trips = Trip.find_all_public_trips(userId)
                return {
                    "message":
                    "The trips of user {} was found".format(
                        User.find_by_uid(userId).user_name),
                    "trips":
                    json.dumps(all_trips)
                }, 200
            else:
                return {
                    "message":
                    "You are not friends with the requested user, therefore you cannot get their trips"
                }, 203

        except Exception as error:
            return {
                "message": "Something went wrong on the server",
                "error": str(error)
            }, 500
Example #2
0
    def post(self):
        if not WhiteTokenModel.is_jti_whitelisted(get_raw_jwt()["jti"]):
            return {'message': 'Not logged in'}, 205

        data = post_trip_parser.parse_args()
        print("Test print", flush=True)
        try:
            existing_trip = Trip.does_trip_exist(data["trip"])
            if (existing_trip["exists"]):
                return {"message": "The trip already exist"}, 200

            current_user = get_jwt_identity()
            if (not data["public"]):
                public = True
            else:
                public = bool(data["public"])

            if not data["trip"]:
                return {'message': 'You need to provide a trip'}
            else:
                #TODO: Improve this \/
                tid = random.randint(10000000, 99999999)
                while Trip.find_by_tid(tid):
                    if tid >= 99999999:
                        tid = 10000000
                    else:
                        tid += 1

                new_trip = Trip(trip_id=tid,
                                user_id=current_user,
                                trip_json=data["trip"],
                                is_public=public)
                new_trip.save_to_db()
                return {
                    "message": "The trips was uploaded successfully",
                    "tripid": tid
                }, 201
        except Exception as err:
            return {"message": str(err)}, 500