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 save_changes_in_trains(): from SRRMSv2.server import SQLSession trains = generate_trains() for tr in trains: session = SQLSession() new_train = Train(train_id=tr[0], train_name=tr[1], train_type=tr[2]) session.add(new_train) session.commit()
def save_changes_in_station(): from SRRMSv2.server import SQLSession d = generate_stations() for st in d.keys(): session = SQLSession() new_stn = Station(station_id=st, station_name=d[st]) session.add(new_stn) session.commit()
def save_changes_in_train_specs(): from SRRMSv2.server import SQLSession train_specs = generate_trains_specs() for tr in train_specs: session = SQLSession() new_train_spec = TrainSpec(train_id=tr[0], start_sid=tr[1], end_sid=tr[2]) session.add(new_train_spec) session.commit()
def save_changes_in_tain_status(): from SRRMSv2.server import SQLSession train_consistsof = generate_status() for tr in train_consistsof: session = SQLSession() new_consistsof = TrainStatus(wait_seat=int(tr[2]), train_id=tr[0], available_seat=int(tr[1])) session.add(new_consistsof) session.commit()
def save_changes_in_consists_of(): from SRRMSv2.server import SQLSession train_consistsof = generate_consists_of() for tr in train_consistsof: session = SQLSession() new_consistsof = ConsistsOf(train_id=tr[0], station_id=tr[1], stop_no=tr[2]) session.add(new_consistsof) session.commit()
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 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