def add_rating(): data = request.get_json() license_number = data['license_number'] phone = data['student_phone'] rating = int(data['rating']) driver = DriverModel.query.filter_by(license_number=license_number).first() student = StudentModel.query.filter_by(phone=phone).first() if (not verify_session_key(request, student.phone)): return jsonify({'status': 0, 'message': 'SessionFault'}) if (not driver): return jsonify({'status': 0, 'message': 'Invalid License Number'}) if (not student): return jsonify({ 'status': 0, 'message': 'Invalid Student Phone Number' }) if (not any([(J.student == student) for J in driver.journeys])): return jsonify({ 'status': 0, 'message': 'Cannot Rate as Student has not travelled with Driver' }) driver.add_rating(rating) return jsonify({'status': 200, 'message': 'OK'})
def allow_student(): data = request.get_json() sid = data['student_id'] license_number = data['license_number'] student = StudentModel.query.filter_by(student_id=sid).first() driver = DriverModel.query.filter_by(license_number=license_number).first() if (not verify_session_key(request, driver.phone)): printlog("session fault") return jsonify({'status': 0, 'message': 'SessionFault'}) if (not driver): printlog("LicenseFault") return jsonify({'status': 0, 'message': 'Invalid License Number'}) if (not student): printlog("Student IDFault") return jsonify({ 'status': 0, 'message': 'Invalid Student Phone Number' }) if (not student.is_paid): printlog("Student Not Paid") return jsonify({'status': 0, 'message': 'Student Has not Paid'}) # printlog(f"DriverLocation: {driver.location} ----> StudentLocation: {student.location}") if (not student.location == driver.location): printlog("Locations dont match") return jsonify({'status': 0, 'message': 'Locations do not match'}) journey = JourneyModel(driver=driver, student=student) db.session.add(journey) db.session.commit() printlog(f"{driver} Allowed {student}") return jsonify({'status': 200, 'message': 'OK'})
def mydrivers(phone): if (not verify_session_key(request, phone)): return jsonify({'status': 0, 'message': 'SessionFault'}) student = StudentModel.query.filter_by(phone=phone).first() if (not student): return jsonify({'status': 0, 'message': 'Invalid Student Phone'}) drivers = [] for J in JourneyModel.query.filter_by(student=student).all(): data = { 'timestamp': J.timestamp, 'driver': J.driver.get_json_representation() } drivers.append(data) return jsonify({'status': 200, 'message': 'OK', 'drivers': drivers})
def getdriver(phone): if (not verify_session_key(request, phone)): return jsonify({'status': 0, 'message': 'SessionFault'}) driver = DriverModel.query.filter_by(phone=phone).first() if (not driver): return jsonify({ 'status': 0, 'message': 'No Driver with that Phone Number' }) return jsonify({ 'status': 200, 'message': 'OK', 'driver': driver.get_json_representation() })
def checkverificationstatus(number): if (not verify_session_key(request, number)): return jsonify({'status': 0, 'message': 'SessionFault'}) driver = DriverModel.query.filter_by(phone=number).first() if (not driver): return jsonify({ 'status': 0, 'message': 'No Driver Found with that Phone Number' }) return jsonify({ 'status': 200, 'message': 'OK', 'isVerified': driver.is_verified })
def getstudent(phone): if (not verify_session_key(request, phone)): return jsonify({'status': 0, 'message': 'SessionFault'}) student = StudentModel.query.filter_by(phone=phone).first() if (not student): return jsonify({ 'status': 0, 'message': 'No Student With that Phone Number' }) # print(student.get_json_representation()) printlog(f"Got JSON Representation :: {student}") return jsonify({ 'status': 200, 'message': 'OK', 'student': student.get_json_representation() })
def update_profile_image(phone): if (not verify_session_key(request, phone)): return jsonify({'status': 0, 'message': 'SessionFault'}) student = StudentModel.query.filter_by(phone=phone).first() if (not student): return jsonify({ 'status': 0, 'message': 'No Student with that Phone Number' }) pictureData = request.files['picture'] pBytes = io.BytesIO(pictureData.read()) uploaded_img = upload_file_to_cloud(pBytes) if (uploaded_img['STATUS'] == 'OK'): student.picture = uploaded_img['URI'] else: return jsonify({ 'status': 0, 'message': 'Could not Upload image to Cloud (500)' }) db.session.commit() return jsonify({'status': 200, 'message': 'Updated Profile Image'})
def getbuses(phone_number): if (not verify_session_key(request, phone_number)): return jsonify({'status': 0, 'message': 'SessionFault'}) student = StudentModel.query.filter_by(phone=phone_number).first() if (student): s_loc = student.location[0] available_drivers = s_loc.drivers.all() #!Change Made Upon Ismail's Request #!Unverified Drivers aren't shown available_drivers = [d for d in available_drivers if d.is_verified] return jsonify({ 'status': 200, 'message': 'OK', 'drivers': [driver.get_json_representation() for driver in available_drivers] if (available_drivers) else [] }) return jsonify({'status': 0, 'message': 'Student Does not Exist'})
def checkpaymentstatus(number): if (not verify_session_key(request, number)): return jsonify({'status': 0, 'message': 'SessionFault'}) student = StudentModel.query.filter_by(phone=number).first() if (not student): return jsonify({ 'status': 0, 'message': 'No Student Found with that Phone Number' }) #Check if 6 months lapsed :: AutoCheck if (student.is_lapsed): return jsonify({ 'status': 1, 'message': 'Account Expired', 'isPaid': False }) #Chck if Student Hasnt paid :: No AutoCheck if (not student.is_paid): return jsonify({ 'status': 3, 'message': 'Payment Not Done', 'isPaid': False }) #Checking if Payment OverDue if ((datetime.datetime.utcnow() - student.utc_last_paid).days > 30): student.is_paid = False db.session.commit() return jsonify({ 'status': 0, 'message': '30 Days Elapsed. Please Pay to restore services', 'isPaid': False }) return jsonify({'status': 200, 'message': 'OK', 'isPaid': True})
def add_details(): data = request.get_json() phone = data['phone'] if (not verify_session_key(request, phone)): return jsonify({'status': 0, 'message': 'SessionFault'}) student = StudentModel.query.filter_by(phone=phone).first() if (not student): return jsonify({ 'status': 0, 'message': 'No Student with that Phone number' }) isFullTime = data['isFullTime'] if ( data.get('isFullTime') != None) else True semester = data['semester'] dob = data['dob'] #DD/MM/YYYY timing = data['timing'] or [] #[8:30AM, 3:30PM] if (timing == []): return jsonify({ 'status': 0, 'message': 'Timings(start, end) must be provided' }) S, E = rectify_timings(timing[0], timing[1]) T = TimingModel.query.filter_by(start=S, end=E).first() if (not T): T = TimingModel(start=S, end=E) db.session.add(T) db.session.commit() student.add_extras(dob=dob, timing=T, is_fulltime=isFullTime, semester=semester) return jsonify({'status': 200, 'message': 'OK'})
def edit_profile(): data = request.get_json() id = data['id'] #Identifier student = StudentModel.query.filter_by(id=id).first() if (not student): return jsonify({'status': 0, 'message': 'No Student With that ID'}) name = data.get('name') or student.name phone = data.get('phone_number') or student.phone address = data.get('home_address') or student.home_address timings = data.get('timings') or [] dob = data.get('dob') or student.get_json_representation()['dob'] is_fulltime = data.get('isFullTime') semester = data.get('semester') or student.semester if (not verify_session_key(request, student.phone)): return jsonify({'status': 0, 'message': 'SessionFault'}) location = student.location[0] university = student.university[0] location.students.remove(student) #Remove Student from Existing Location university.students.remove( student) #Remove Student from Existing University #Handle Location Changes if (data.get('location')): if (LocationModel.query.filter_by( location_name=data['location'].lower()).first()): location = LocationModel.query.filter_by( location_name=data['location'].lower()).first() else: loc = LocationModel(location_name=data['location']) db.session.add(loc) db.session.commit() location = loc #Handle University Changes if (data.get('university_name') and data.get('university_address')): if (UniversityModel.query.filter_by( name=data['university_name'].lower()).first()): university = UniversityModel.query.filter_by( name=data['university_name']).first() else: uni = UniversityModel(name=data['university_name'], address=data['university_address'].lower()) db.session.add(uni) db.session.commit() university = uni location.students.append(student) #Add Student to New Location university.students.append(student) if (phone != student.phone): #Number Changed -> Verify Number student.phone_verified = False #If Session Exists, change phone number S = SessionModel.query.filter_by(phone=student.phone).first() #Update the Session S.phone = phone db.session.commit() send_otp(phone) if (timings != []): timings = list(timings) #Remove All Students's Timings student.timings = [] db.session.commit() #Making new or getting old Timings S, E = rectify_timings(timings[0], timings[1]) T = TimingModel.query.filter_by(start=S, end=E).first() if (not T): T = TimingModel(start=S, end=E) db.session.add(T) db.session.commit() #Updating Timing T.students.append(student) db.session.commit() #Updating Date of Birth if (dob != student.get_json_representation()['dob']): student.dob = datetime.datetime(day=int(dob.split('/')[0]), month=int(dob.split('/')[1]), year=int(dob.split('/')[2])) student.name = name student.phone = phone student.home_address = address student.semester = semester student.is_fulltime = is_fulltime db.session.commit() return jsonify({'status': 200, 'message': 'Updated Data'})
def edit_profile(): data = request.get_json() id = data['id'] #Identifier driver = DriverModel.query.filter_by(id=id).first() if (not driver): return jsonify({'status': 0, 'message': 'Invalid ID'}) if (not verify_session_key(request, driver.phone)): return jsonify({'status': 0, 'message': 'SessionFault'}) name = data['name'] or driver.name phone = data['phone_number'] or driver.phone bus_number = data['bus_number'] or driver.bus_number experience = data['experience'] or driver.experience license_number = data['license_number'] or driver.license_number timings = data['timings'] or [] #[[8:30AM, 3:30PM], [7:20AM, 2:20PM]] location = driver.location[0] location.drivers.remove(driver) #Remove Existing Location if (data['location']): location = LocationModel.query.filter_by( location_name=data['location'].lower()).first() if (not location): loc = LocationModel(location_name=data['location'].lower()) db.session.add(loc) db.session.commit() location = loc #If such sensitive information changes, Driver must be reverified #!REMOVED UNDER SPECIFIC REQUEST BY ISMAIL # if(phone != driver.phone or data['bus_number'] != driver.bus_number or data['license_number'] != driver.license_number): # driver.is_verified = False if (phone != driver.phone): #Number Changed -> Verify Number driver.phone_verified = False S = SessionModel.query.filter_by(phone=driver.phone).first() if (S): S.phone = phone db.session.commit() send_otp(phone) if (timings and timings != []): timings = list(timings) # printlog("Recievedtimings", timings) #Remove All Driver's Timings driver.timings = [] db.session.commit() #Make new or collect old Timings TimingsList = [] for TList in timings: S, E = rectify_timings(TList[0], TList[1]) T = TimingModel.query.filter_by(start=S, end=E).first() if (not T): T = TimingModel(start=S, end=E) db.session.add(T) db.session.commit() TimingsList.append(T) # printlog("New Timings", TimingsList) #Add Timings to Driver [T.drivers.append(driver) for T in TimingsList] db.session.commit() driver.name = name driver.phone = phone driver.bus_number = bus_number driver.experience = experience driver.license_number = license_number location.drivers.append(driver) #Add Driver to New Location db.session.commit() return jsonify({'status': 200, 'message': 'Updated Data'})