def login_student(phone): if (request.method == 'POST'): data = request.get_json() otp = data['otp'] is_correct = verify_otp(phone, otp) if (is_correct): #If OTP is Correct sessionkey = generate_session_id() s = SessionModel.query.filter_by(phone=phone).first() if (s): s.sessionkey = sessionkey else: s = SessionModel(phone=phone, sessionkey=sessionkey) db.session.add(s) db.session.commit() return jsonify({ 'status': 200, 'message': 'OK', 'session_key': sessionkey }) return jsonify({'status': 0, 'message': 'Invalid OTP'}) #On Get request, send OTP to number student = StudentModel.query.filter_by(phone=phone).first() if (not student): return jsonify({'status': 0, 'message': 'No Student With that Number'}) send_otp(phone) return jsonify({'status': 200, 'message': 'OK'})
def driver_register(): data = request.get_json() # Get Data name = data['name'] phone = data['phone_number'] bus_number = data['bus_number'] location = data['location'] license_number = data['license_number'] experience = int(data['experience']) timings = list(data['timings']) #[[8:00AM, 3:40PM], [9:30AM, 5:00PM]] #If Location Exists get it. loc = LocationModel.query.filter_by(location_name=location.lower()).first() if (loc == None): loc = LocationModel(location_name=location.lower()) db.session.add(loc) db.session.commit() #---------------------------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) #------------------------------------------------------ driver = DriverModel(name=name, phone=phone, bus_number=bus_number, license_number=license_number, experience=experience, loc=loc, timings_list=TimingsList) db.session.add(driver) db.session.commit() #Send the OTP Immediately after Registration send_otp(phone) return jsonify({ 'status': 200, 'message': 'Created Driver Account', })
def register_number(): data = request.get_json() #Get Data name = data['name'] phone = data['phone_number'] location = data['location'].lower() uni_name = data['university_name'].lower() uni_addr = data['university_address'] home_addr = data['home_address'] std = StudentModel.query.filter_by(phone=phone).first() if (std): return jsonify({ 'status': 0, 'message': 'Phone Number has already been used' }) #Create location if doesnt exist loc = LocationModel.query.filter_by(location_name=location).first() if (loc == None): loc = LocationModel(location_name=location) db.session.add(loc) db.session.commit() #Create University if doesn't exist uni = UniversityModel.query.filter_by(name=uni_name).first() if (uni == None): uni = UniversityModel(name=uni_name, address=uni_addr) db.session.add(loc) db.session.commit() sid = generate_student_id(6) printlog(f"New Generated SID: {sid}") student = StudentModel( name=name, phone=phone, student_id=sid, home_address=home_addr, uni=uni, loc=loc, ) send_otp(phone) db.session.add(student) db.session.commit() return jsonify({'status': 200, 'message': 'Created'})
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 resend_otp(phone): printlog(f"Resending OTP to {phone}") send_otp(phone) return jsonify({'status': 200, 'message': 'OK'})
def resend_otp(phone): send_otp(phone) return jsonify({'status': 200, 'message': 'OK'})
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'})