def get_trains(): data = request.json #try: train_ids = find_trains(data) '''with open("some.txt", "w") as f: f.write(str(train_ids))''' if len(train_ids) != 0: from SRRMSv2.server import SQLSession session = SQLSession() trains = {} #try: for t in train_ids: train = session.query(Train).filter_by(train_id=t).first() train_ = session.query(TrainSpec).filter_by(train_id=t).first() trains[str(t)] = { "train_name": train.train_name, "train_type": train.train_type, "train_source_stn": train_.start_sid, "train_end_stn": train_.end_sid } response_object = {"status": "success", "data": trains} return jsonify(response_object), 200 else: response_object = { "status": "fail", "message": "no trains in this route" } return jsonify(response_object), 401
def find_trains(data): from SRRMSv2.server import SQLSession sess = SQLSession() start_match = sess.query(ConsistsOf).filter_by(station_id = data['source_id']).all() dest_match = sess.query(ConsistsOf).filter_by(station_id = data['dest_id']).all() ''' with open("some.txt", "w") as f: f.write(str(start_match)) f.write(str(dest_match)) ''' start_match = [i.__dict__ for i in start_match] dest_match = [i.__dict__ for i in dest_match] start_match = pd.DataFrame(start_match) dest_match = pd.DataFrame(dest_match) start_match.to_csv('start_match.csv') dest_match.to_csv('dest_match.csv') start_match.rename(columns={'stop_no':'source_no'}, inplace=True) dest_match.rename(columns={'stop_no':'dest_no'}, inplace=True) final = pd.merge(start_match , dest_match , how ='inner',on=['train_id']) #final = final.apply(pd.to_numeric) #final[['dest_no', 'source_no']] = pd.to_numeric(final[['dest_no', 'source_no']]) #return valid_tid(final) final = final[['source_no','dest_no','train_id']] final.to_csv("final.csv") l = valid_tid(final) return l
def save_new_user(data): from SRRMSv2.server import SQLSession session = SQLSession() user = session.query(User).filter_by(email=data['email']).first() if not user: new_user = User(public_id=str(uuid.uuid4()), email=data['email'], username=data['username'], password=data['password'], registered_on=datetime.datetime.utcnow()) try: session.add(new_user) session.commit() except: response_object = { 'status': 'fail', 'message': 'Problem occured in db', } return jsonify(response_object), 401 finally: response_object = { 'status': 'Ok', 'message': 'User Created Successful', } return jsonify(response_object), 200 else: response_object = { 'status': 'fail', 'message': 'User already exists. Please Log in.', } return jsonify(response_object), 409
def get_seat(data): from SRRMSv2.server import SQLSession session = SQLSession() train = session.query(TrainStatus).filter_by(train_id=data['train_id']).first() print(train.available_seat) if not train: response_object = { "status": "fail", "message": "train doesn't exist" } return jsonify(response_object), 400 else: if train.available_seat == 0: response_object = { "status": "Success", "seat_available": "Not Available", "seat_wait": train.wait_seat } return jsonify(response_object), 201 else: response_object = { "status": "Success", "seat_available": train.available_seat, } return jsonify(response_object), 200
def get_all_users(): from SRRMSv2.server import SQLSession session = SQLSession() users_ = session.query(User).all() users_ = [{"name": i.username, "email": i.email} for i in users_] if len(users_) == 0: response_object = {'status': 'success', 'message': 'No user yer!'} else: response_object = {'status': 'success', 'users': users_} return jsonify(response_object), 200
def is_valid_user(data): from SRRMSv2.server import SQLSession session = SQLSession() user = session.query(User).filter_by(email=data['email']).first() if not user: return 400 else: if data['key'] == pub_key: return 200 else: return 400
def get_all_pnr(): from SRRMSv2.server import SQLSession session = SQLSession() data = request.json passanger = session.query(Passenger).all() passangers = [{"name":i.p_name, "pnr":i.pnr, "seat_no":i.seat_no, "status":i.reserve_status} for i in passanger] response_object = { "status": 'success', "data": passangers } return jsonify(response_object), 200
def validate_user(data): from SRRMSv2.server import SQLSession session = SQLSession() user = session.query(User).filter_by(email=data['email']).first() if not user: responce_object = { 'Status': 'fail', 'message': 'no such user exist', } return jsonify(responce_object), 400 else: if user.check_password(data.get('password')): responce_object = {'status': 'success', 'key': pub_key} return jsonify(responce_object), 200 else: responce_object = { 'status': 'fail', 'message': 'enter valid password/email' } return jsonify(responce_object), 400
def get_pnr_status(): from SRRMSv2.server import SQLSession session = SQLSession() data = request.json passanger = session.query(Passenger).filter_by(pnr=data['pnr']).first() if not passanger: response_object = { "status": "fail", "message": "Not a valid pnr" } return jsonify(response_object), 202 else: if passanger.reserved_status == True: response_object = { "status": "success", "seat number": passanger.seat_number } return jsonify(response_object), 200 else: response_object = { "status": "success", "waiting number": passanger.seat_number } return jsonify(response_object), 200
def cancelTicket(data): from SRRMSv2.server import SQLSession sess = SQLSession() passenger = sess.query(Passenger).filter_by(pnr=data["pnr"]).first() if not passenger: response_object = {"status": "fail", "message": "PNR number not valid"} return jsonify(response_object), 400 else: # True : passenger is in confirmed status if passenger.reserve_status == True: try: book1 = sess.query(book1).filter_by(pnr=passenger.pnr).first() sess.query(Passenger).filter( passenger.pnr == data['pnr']).delete( synchronize_session=False) sess.query(book1).filter(book1.pnr == data['pnr']).delete( synchronize_session=False) sess.query(book2).filter(book2.pnr == data['pnr']).delete( synchronize_session=False) train_status = sess.query(TrainStatus).filter_by( train_id == book1.train_id).first() if train_status.wait_seat > 0: passenger2 = sess.query(Passenger).filter_by( reserve_status == False).first() sess.query(Passenger).filter(pnr=passenger2.pnr).update( {"reserve_status": True}, synchronize_session='evaluate') sess.commit() else: sess.query(TrainStatus).filter( train_id=book1.train_id).update( {"available_seat": TrainStatus.available_seat + 1}, synchronize_session='evaluate') sess.commit() except: ro = { 'status': 'fail', 'message': 'could not cancel your ticket and add new passenger from waiting list' } return jsonify(ro), 402 #sess.query(update(train_status,values={train_status.available_seat:train_status.available_seat+1})) # passenger was in waiting list , before cancelling ticket else: try: sess.query(Passenger).filter( passenger.pnr == data['pnr']).delete( synchronize_session=False) sess.query(book1).filter(book1.pnr == data['pnr']).delete( synchronize_session=False) sess.query(book2).filter(book2.pnr == data['pnr']).delete( synchronize_session=False) except: ro = { 'status': 'fail', 'message': 'could not cancel your ticket' } return jsonify(ro), 401
def get_all_consistsof(): from SRRMSv2.server import SQLSession session = SQLSession() train_consistsof = session.query(ConsistsOf).all() for t in train_consistsof: print(t.station_id, t.train_id, t.stop_no)
def get_all_trains(): from SRRMSv2.server import SQLSession session = SQLSession() trains = session.query(Train).all() for t in trains: print(t.train_id, t.train_name, t.train_type)
def get_all_stns(): from SRRMSv2.server import SQLSession session = SQLSession() stns_ = session.query(Station).all() for s in stns_: print(s.station_id, s.station_name)
def get_all_train_status_(): from SRRMSv2.server import SQLSession session = SQLSession() train_consistsof = session.query(TrainStatus).all() for t in train_consistsof: print(t.train_id, t.wait_seat, t.available_seat)
def book_ticket(data): from SRRMSv2.server import SQLSession sess = SQLSession() station_source = sess.query(Station).filter_by( station_name=data['source']).first() station_dest = sess.query(Station).filter_by( station_name=data['dest']).first() av = sess.query(TrainStatus).filter_by(train_id=data['train_id']).first() #if train can accomodate more passenger and is within capacity if av.available_seat > 0: pnr_for_pass = generate_PNR(data['username']) new_pass = Passenger( pnr=pnr_for_pass, #generate_pnr(pnr_for_pass), p_name=data['pname'], reserve_status=True, age=data['age'], gender=data['gender'], seat_no=av.available_seat - 1, start_sid=station_source.station_id, end_sid=station_dest.station_id) new_book1 = Book1( #obtained from session: userId of the user booking the ticket for some passenger user_id=data['username'], pnr=pnr_for_pass, train_id=data['train_id']) new_book2 = Book2(pnr=pnr_for_pass, date=data['date']) sess.query(TrainStatus).update( {"available_seat": TrainStatus.available_seat - 1}, synchronize_session='evaluate') sess.add(new_pass) sess.add(new_book1) sess.add(new_book2) sess.commit() responce_object = { 'Status': 'sucess', 'message': 'conformed', 'seat number': av.available_seat - 1, 'pnr': pnr_for_pass } return jsonify(responce_object), 200 else: pnr_for_pass = generate_PNR(data['username']) new_pass = Passenger( pnr=pnr_for_pass, #generate_pnr(pnr_for_pass), p_name=data['pname'], reserve_status=False, age=data['age'], gender=data['gender'], seat_no=-1, start_sid=station_source.station_id, end_sid=station_dest.station_id) new_book1 = Book1( #obtained from session: userId of the user booking the ticket for some passenger user_id=data['username'], pnr=pnr_for_pass, train_id=data['train_id']) new_book2 = Book2(pnr=pnr_for_pass, date=data['date']) #sess.query(TrainStatus).update({"available_seat":TrainStatus.available_seat+1},synchronize_session='evaluate') sess.query(TrainStatus).update( {"wait_seat": TrainStatus.wait_seat + 1}, synchronize_session='evaluate') sess.add(new_pass) sess.add(new_book1) sess.add(new_book2) sess.commit() responce_object = { 'Status': 'sucess', 'message': 'Waiting', 'pnr': pnr_for_pass } return jsonify(responce_object), 202
def get_uid(name): from SRRMSv2.server import SQLSession sess = SQLSession() u = sess.query(User).filter_by(username=name) return str(u.username)
def get_all_train_specs(): from SRRMSv2.server import SQLSession session = SQLSession() train_specs = session.query(TrainSpec).all() for t in train_specs: print(t.train_id, t.start_sid, t.end_sid)