def create_rideoffer_reuests(self, rideoffer_id): """check whether ride offers exist.""" parser = reqparse.RequestParser() parser.add_argument('passengername', type=str, required=True) parser.add_argument('token', location='headers') args = parser.parse_args() passenger_name = args['passengername'] if not args['token']: return make_response(jsonify({"message": "Token is missing"}), 401) """ implementing token decoding""" decoded = decode_token(args['token']) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) check_ride_offer_cur = con.cursor() check_ride_offer_cur.execute( "select id from rides where id='"+rideoffer_id+"'") while True: row = check_ride_offer_cur.fetchone() if row == None: print(row) return make_response(jsonify({"message": "sorry please , ride offer not found"}), 404) # break else: cur_select_ride_offers = con.cursor() """checking whether the ride request for the user already exists.""" cur_select_ride_offers.execute( "select ride_offer_id from requests where passengername = '"+passenger_name+"' and ride_offer_id='"+rideoffer_id+"'") while True: row = cur_select_ride_offers.fetchone() if row == None: break if str(row[0]).strip() == str(rideoffer_id).strip(): print('db name : ' + str(row[0]) + ' url name : ' + str(rideoffer_id).strip()) return make_response(jsonify({"message": 'Sorry,you have already made a ride request.'}), 409) cur = con.cursor() formated_time_date = datetime.datetime.now() formated_time_date.strftime('%H-%M-%Y-%m-%d') cur.execute("INSERT INTO requests (passengername,time,ride_offer_id,status) VALUES('"+passenger_name+"','" + str(formated_time_date)+"','"+rideoffer_id+"','0')") con.commit() return make_response(jsonify({ 'message': 'Ride request created successfully.'}, ), 201)
def accept_or_reject_ridrequest(self, rideId, requestId): parser = reqparse.RequestParser() parser.add_argument('status', type=str, required=True) parser.add_argument('token', location='headers') args = parser.parse_args() if not args['token']: return make_response(jsonify({"message": "Token is missing"}), 401) """ implementing token decoding""" decoded = decode_token(args['token']) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) status = args['status'] cur = con.cursor() cur.execute( "select id from requests where ride_offer_id= '"+rideId+"' and id ='"+requestId+"' ") for row in cur.fetchall(): if row is not None: if int(status) == 0: cur = con.cursor() cur.execute( "update requests SET status ='"+status + "' where ride_offer_id = '"+rideId+"' " ) return make_response( jsonify({"message": "ride request rejected"}) , 200 ) else: cur = con.cursor() cur.execute( "update requests SET status ='"+status+"' where ride_offer_id = '"+rideId+"' ") return make_response( jsonify({"message": "ride request accepted"}) , 200 ) return make_response( jsonify({"message": "ride offer is not found please "}) , 404 ) return make_response( jsonify({"message": "no ride offer or ride request found "}) , 404 )
def get_ride_offers(self): parser = reqparse.RequestParser() parser.add_argument('token', location='headers') args = parser.parse_args() if not args['token']: return make_response(jsonify({"message": "Token is missing"}), 401) """ implementing token decoding""" decoded = decode_token(args['token']) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) cur = con.cursor() cur.execute( """select id, name,details,driver, price from rides order by id desc""") columns = ('id', 'name', 'details', 'driver', 'price' ) results = [] for row in cur.fetchall(): if row is None: return make_response(jsonify({"message": "No ride offers found."}), 404) results.append(dict(zip(columns, row))) print(str(results)) return make_response(jsonify({"ride_offers": str(results)}), 200)
def get_rideoffer_requests(self, id): parser = reqparse.RequestParser() parser.add_argument('token', location='headers') args = parser.parse_args() if not args['token']: return make_response(jsonify({"message": "Token is missing"}), 401) """ implementing token decoding""" decoded = decode_token(args['token']) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) cur = con.cursor() cur.execute( "select id, passengername,time from requests where ride_offer_id= '"+id+"'") columns = ('id', 'passengername', 'time' ) results = [] for row in cur.fetchall(): if row is not None: results.append(dict(zip(columns, row))) print(str(results)) return make_response(jsonify({"ride_offers": str(results).replace('[', '').replace(']', '')}), 200) return make_response(jsonify({"message": "No ride requests found."}), 404)
def get_single_ride(self, ride_id): """Getting data from the URL body """ parser = reqparse.RequestParser() parser.add_argument('token', location='headers') args = parser.parse_args() if not args['token']: return make_response(jsonify({"message": "Token is missing"}), 401) """ implementing token decoding""" decoded = decode_token(args['token']) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) cur = con.cursor() cur.execute( "select id , name , details , driver, \ price from rides where id='"+ride_id+"' ") columns = ('id', 'name', 'details', 'driver', 'price') results = [] for row in cur.fetchall(): results.append(dict(zip(columns, row))) if row is not None: return make_response(jsonify({ "ride_offer": str(results).replace("[", "").replace("]", "") }), 200) return make_response(jsonify({"message": "sorry please , ride offer not found,try searching again"}), 404)
def login(): """Getting data from the URL body """ parser = reqparse.RequestParser() parser.add_argument('username', type=str, required=True) parser.add_argument('password', type=str, required=True) args = parser.parse_args() username = args['username'] password = args['password'] cur = con.cursor() cur.callproc('login_auth', ( username, password, )) print(generate_password_hash(password)) row = cur.fetchone() try: while row is not None: status_value = str(row).strip().replace('(', '').replace( ')', '').replace(',', '') if int(status_value) == 0: return make_response(jsonify({"message": "wrong credentials"}), 401) else: cur_get_username = con.cursor() cur_get_username.execute( "select username from all_users where username ='******'") while True: row = cur_get_username.fetchone() print('--{}--'.format(row[0])) access_token = "{}".format(generate_token(username)) return make_response( jsonify({ "token": access_token, "message": "User logged in successfully" }), 200) break except TypeError as t: print(t) return make_response(jsonify({"message": "wrong credentials"}), 401)
def insert_ride_offers(self): """Getting data from the URL body """ parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=True) parser.add_argument('details', type=str, required=True) parser.add_argument('price', type=str, required=True) parser.add_argument('token', location='headers') parser.add_argument('driver_name',type=str,required=True) args = parser.parse_args() """ check the token value if its available""" if not args['token']: return make_response(jsonify({"message": "Token is missing"}), 401) """ implementing token decoding""" decoded = decode_token(args['token']) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) offer_name = args['name'] offer_details = args['details'] price = args['price'] driver_name = args['driver_name'] """creating a ride offer cursor to check for already existing ride offer names.""" cur_select_ride_offers = con.cursor() cur_select_ride_offers.execute( "select name from rides where driver = '"+driver_name+"' and name='"+offer_name+"'") while True: row = cur_select_ride_offers.fetchone() if row == None: break if str(row[0]).strip() == str(offer_name).strip(): print('db name : ' + str(row[0]) + ' url name : ' + str(offer_name).strip()) return make_response(jsonify({"message": 'Sorry,this ride offer is already available.'}), 400) cur = con.cursor() cur.execute("INSERT INTO rides (name,details,price,driver) VALUES('" + offer_name + "','"+offer_details+"','"+price+"','"+driver_name+"')") con.commit() return make_response(jsonify({'message': 'Ride offer created successfully.'},), 201)
def create_user(): """Getting data from the URL body """ parser = reqparse.RequestParser() parser.add_argument('username', type=str, required=True) parser.add_argument('email', type=str, required=True) parser.add_argument('password', type=str, required=True) parser.add_argument('isdriver', type=int, required=True) args = parser.parse_args() username = args['username'] email = args['email'] password = args['password'] isDriver = args['isdriver'] if username.strip() == "" or len(username.strip()) < 2: return make_response( jsonify( {"message": "invalid username, Enter correct username please"}), 400) if re.compile('[!@#$%^&*:;?><.0-9]').match(username): return make_response( jsonify({ "message": "Invalid characters not allowed, numbers and symbols are not allowed" }), 400) if not re.match(r"([\w\.-]+)@([\w\.-]+)(\.[\w\.]+$)", email): return make_response(jsonify({"message": "Enter valid email"}), 400) if password.strip() == "" or password.strip() == " " or password.strip( ) == " ": return make_response( jsonify({"message": "Password Empty, Enter a valid password"}), 400) if len(username) < 2: return make_response( jsonify({"message": "username is too short, < 2"}), 400) if len(password) < 5: return make_response( jsonify({"message": "Password is too short, < 5"}), 400) """creating a sign up cursor to check for already existing users.""" cur = con.cursor() cur.callproc('check_if_user_exixts', (username, )) row = cur.fetchone() while row is not None: status_value = str(row).strip().replace('(', '').replace(')', '').replace( ',', '') if int(status_value) == 0: print('go a head and insert data ') cur = con.cursor() cur.callproc('create_users', ( username, email, password, isDriver, )) con.commit() return make_response( jsonify({'message': 'user created successfully.'}, ), 201) else: print('this username already exists.') return make_response( jsonify( {"message": 'Sorry,this username is already available.'}), 409)