def post(self): data = check_request_json(request) offer_id = data.get('offer_id') if offer_id is None: missing_required_field('offer_id') user_id = data.get('user_id') if user_id is None: missing_required_field('user_id') portions = data.get('portions') if portions is None: missing_required_field('portions') if ecv.testing: offer = ecv.session.query(Offer).filter_by(id=offer_id).first() if offer is None: abort(404, message='The offer does not exist.') user = ecv.session.query(User).filter_by(id=user_id).first() if user is None: abort(404, message='The user does not exist') reservation = Reservation(offer_id=offer_id, user_id=user_id, portions=portions, timestamp=datetime.datetime.utcnow()) ecv.session.add(reservation) ecv.session.commit() return reservation.to_dict(), 201 else: p = Publisher() result = p.reserve(user_id=user_id, offer_id=offer_id, portions=portions) return result, 201
def post(self): json_data = request.get_json() current_client = get_jwt_identity() client_id = json_data.get('client_id') workspace_id = json_data.get('workspace_id') if Reservation.get_by_workspace(workspace_id): return {'message': 'time already used'}, HTTPStatus.BAD_REQUEST # if Reservation.get_by_id(workspace_id): # return {'message': 'invalid time'}, HTTPStatus.BAD_REQUEST reservation = Reservation( client_id=client_id, workspace_id=workspace_id, ) reservation.save() data = { 'id': reservation.id, 'client_id': reservation.client_id, 'workspace_id': reservation.workspace_id, } return data, HTTPStatus.OK
def get(self): user = User.get_by_id(user_id=get_jwt_identity()) if user.is_admin: return ReservationSchema(many=True).dump(Reservation.show_all()), HTTPStatus.OK else: if Reservation.show_mine(user.username): return ReservationSchema(many=True).dump(Reservation.show_mine(user.username)), HTTPStatus.OK else: return {"message": 'You have no reservations'}, HTTPStatus.OK
def setUp(self): self.guest_1 = Guest("Simon Elsmie") self.guest_2 = Guest("Rick Sanchez") self.room_1 = Room(1) self.room_2 = Room(2) self.date_1 = '2021-04-06' self.date_2 = '2021-04-07' self.reservation_1 = Reservation(self.guest_1, self.room_2, self.date_1, self.date_2, "Arrival") self.reservation_2 = Reservation(self.guest_2, self.room_1, self.date_1, self.date_2, "Arrival")
def post(self): try: json_data = request.get_json() data = ReservationSchema().load(json_data) current_user = User.get_by_id(user_id=get_jwt_identity()) current_workspace = Workspace.get_by_number(data.get('workspace')) # check duration format if "minutes" in data.get('duration'): increase = timedelta(minutes=int(''.join(filter(str.isdigit, data.get('duration'))))) elif "hours" in data.get('duration'): increase = timedelta(hours=int(''.join(filter(str.isdigit, data.get('duration'))))) else: return {"message": 'Wrong format, correct example - [10 hours]'}, HTTPStatus.BAD_REQUEST # assign start and end time ending_time = data.get('start_time') + increase check_timeframes_result = check_timeframes(data.get('start_time'), ending_time, current_workspace.workspace_number) # check if it can fit all if data.get('attendees') >= current_workspace.available_space: return {"message": 'Not enough space'}, HTTPStatus.BAD_REQUEST # check if the workspace is available if current_workspace.turkuamk_only is True and current_user.is_turkuamk is False: return {"message": 'This workspace is only for TurkuAMK users'}, HTTPStatus.BAD_REQUEST if check_timeframes_result is not None: return {"message": check_timeframes_result}, HTTPStatus.BAD_REQUEST else: new_reservation = Reservation(reserved_by=current_user.username, end_time=ending_time, user_id=current_user.id, **data) new_timeframe = Timeframes(start_time=data.get('start_time'), end_time=ending_time, workspace=current_workspace.workspace_number, user_id=current_user.id ) new_reservation.save() new_timeframe.save() return ReservationSchema().dump(new_reservation), HTTPStatus.OK except Exception as err: return err.args[0], HTTPStatus.BAD_REQUEST
def get(self, reservation_id): reservation = Reservation.get_by_id(reservation_id=reservation_id) if reservation is None: return {'message': 'Reservation not found'}, HTTPStatus.NOT_FOUND return reservation.data(), HTTPStatus.OK
def get(self, space_id): reservation = Reservation.get_all_by_space_id(space_id=space_id) if reservation is None: return {'message': 'reservations not found'}, HTTPStatus.NOT_FOUND return reservation_list_schema.dump(reservation).data, HTTPStatus.OK
def patch(self, reservation_id): json_data = request.get_json() data, errors = reservation_schema.load(data=json_data, partial=('name', )) if errors: return { 'message': 'Validation errors', 'errors': errors }, HTTPStatus.BAD_REQUEST reservation = Reservation.get_by_id(reservation_id=reservation_id) if reservation is None: return {'message': 'reservation not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != reservation.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN reservation.time = data.get('time') or reservation.time reservation.save() return reservation_schema.dump(reservation).data, HTTPStatus.OK
def delete(self): data = request.args.get('jsdata') reservation = Reservation.get_by_id(data) reservation.delete() return render_template('deleted.html')
def get(self, room_name): reservations = Reservation.get_by_room(room=room_name) data = [] for reservation in reservations: data.append(reservation.data()) return {'data': data}, HTTPStatus.OK
def post(self): data = request.form data2 = json.dumps(data) data3 = json.loads(data2) user = data3['user'] data, errors = reservation_schema.load(data=data3) if errors: return {'message': "Validation errors", 'errors': errors}, HTTPStatus.BAD_REQUEST date = data3["datetime"] timeend = data3["endtime"] timestart = data3["starttime"] datetime = str(date) + "T" + timestart datetimeend = str(date) + "T" + timeend news = timestart.split(":")[0] newe = timeend.split(":")[0] if news < "16" or newe > "21": return {'message': "Time bust be between 4pm and 9pm."} else: resp = make_response(redirect(url_for('hello'))) reservation = Reservation(**data) reservation.datetime = datetime reservation.datetimeend = datetimeend reservation.reservor = user reservation.save() return resp
def get(self, date_string): date_time_obj = datetime.strptime(date_string, '%d%m%y') reservations = Reservation.get_by_date(date=date_time_obj) data = [] for reservation in reservations: data.append(reservation.data()) return {'data': data}, HTTPStatus.OK
def get(self): reservations = Reservation.get_by_is_listed() data = [] for reservation in reservations: data.append(reservation.data()) return {'data': data}, HTTPStatus.OK
def select(id): reservation = None sql = "SELECT * FROM reservations WHERE id = %s" values = [id] result = run_sql(sql, values)[0] if result is not None: guest = guest_repository.select(result['guest_id']) room = room_repository.select(result['room_id']) reservation = Reservation(guest, room, result['arrival_date'], result['departure_date'], result['status'], result['id']) return reservation
def select_all(): reservations = [] sql = "SELECT * FROM reservations ORDER BY status ASC, arrival_date" results = run_sql(sql) for row in results: guest = guest_repository.select(row['guest_id']) room = room_repository.select(row['room_id']) reservation = Reservation(guest, room, row['arrival_date'], row['departure_date'], row['status'], row['id']) reservations.append(reservation) return reservations
def get(self): reservations = Reservation.get_by_listed() data = [] for reservation in reservations: if not reservation.date < datetime.now(): data.append(reservation.data()) return {'data': data}, HTTPStatus.OK
def arrivals(): reservations = [] sql = "SELECT * FROM reservations WHERE arrival_date = %s AND status = %s ORDER BY arrival_date ASC" values = [datetime.date.today(), "Arrival"] results = run_sql(sql, values) for row in results: guest = guest_repository.select(row['guest_id']) room = room_repository.select(row['room_id']) reservation = Reservation(guest, room, row['arrival_date'], row['departure_date'], row['status'], row['id']) reservations.append(reservation) return reservations
def in_house(): reservations = [] sql = "SELECT * FROM reservations WHERE status = %s ORDER BY departure_date, room_id" values = ["Checked In"] results = run_sql(sql, values) for row in results: guest = guest_repository.select(row['guest_id']) room = room_repository.select(row['room_id']) reservation = Reservation(guest, room, row['arrival_date'], row['departure_date'], row['status'], row['id']) reservations.append(reservation) return reservations
def get_reservations(self, request): if not Auth.check(): raise endpoints.UnauthorizedException('Please log in.') reservations = Reservation.query() messages = [] for reservation in reservations: messages.append(reservation.toMessage()) return ReservationsMessage(reservations = messages)
def create_reservation(): guest_id = request.form['guest_id'] guest = guest_repository.select(guest_id) room = room_repository.select_default() arrival_date = request.form['arrival_date'] departure_date = request.form['departure_date'] status = request.form['status'] new_reservation = Reservation(guest, room, arrival_date, departure_date, status) reservation_repository.save(new_reservation) reservation_repository.arrival_status() return redirect('/reservations')
def select_from_today(date): reservations = [] sql = "SELECT * FROM reservations WHERE arrival_date >= %s ORDER BY arrival_date, status ASC" values = [date] results = run_sql(sql, values) for row in results: guest = guest_repository.select(row['guest_id']) room = room_repository.select(row['room_id']) reservation = Reservation(guest, room, row['arrival_date'], row['departure_date'], row['status'], row['id']) reservations.append(reservation) return reservations
def update_reservation(id): guest_id = request.form['guest_id'] guest = guest_repository.select(guest_id) room_id = request.form['room_id'] room = room_repository.select(room_id) arrival_date = request.form['arrival_date'] departure_date = request.form['departure_date'] status = request.form['status'] updated_reservation = Reservation(guest, room, arrival_date, departure_date, status, id) reservation_repository.update(updated_reservation) reservation_repository.arrival_status() return redirect('/reservations')
def post(self): json_data = request.get_json() current_user = get_jwt_identity() data, errors = reservation_schema.load(data=json_data) existing_reservations = Reservation.query.filter_by( time=json_data['time'], space_id=json_data['space_id']) if existing_reservations.count(): return { 'message': "A reservation already exists for given time and space" }, HTTPStatus.BAD_REQUEST if errors: return { 'message': "Validation errors", 'errors': errors }, HTTPStatus.BAD_REQUEST reservation = Reservation(**data) reservation.user_id = current_user reservation.save() return reservation_schema.dump(reservation).data, HTTPStatus.CREATED
def get(self, reservation_id): reservation = Reservation.get_by_id(reservation_id=reservation_id) if reservation is None: return {'message': 'reservation not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if reservation.user_id != current_user or current_user is None: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN return reservation_schema.dump(reservation).data, HTTPStatus.OK
def reservation(db, offer, user_client): r = Reservation(user_id=user_client.id, offer_id=offer.id, portions=3, timestamp=datetime.datetime.utcnow()) db.session.add(r) db.session.commit() yield r if inspect(r).persistent: db.session.delete(r) db.session.commit()
def delete(self, reservation_id): reservation = Reservation.get_by_id(reservation_id=reservation_id) if reservation is None: return {'message': 'Reservation not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != reservation.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN reservation.delete() return {}, HTTPStatus.NO_CONTENT
def put(self): try: user = User.get_by_id(user_id=get_jwt_identity()) json_data = request.get_json() data = ReservationSchema().load(json_data) reservation = Reservation.find_by_workspace(data.get('workspace')) current_workspace = Workspace.get_by_number(data.get('workspace')) previous_timeframe = Timeframes.get_for_update(current_workspace.workspace_number, user.id) if "minutes" in json_data['duration']: increase = timedelta(minutes=int(''.join(filter(str.isdigit, data.get('duration'))))) elif "hours" in json_data['duration']: increase = timedelta(hours=int(''.join(filter(str.isdigit, data.get('duration'))))) else: return {"message": 'Wrong format'}, HTTPStatus.BAD_REQUEST ending_time = data.get('start_time') + increase check_timeframes_result = check_timeframes(data.get('start_time'), ending_time, current_workspace.workspace_number) if check_timeframes_result is not None: return {"message": check_timeframes_result}, HTTPStatus.BAD_REQUEST elif reservation.user_id != user.id: return {"message": "No such reservation"}, HTTPStatus.BAD_REQUEST elif reservation.workspace != data.get('workspace'): return {"message": "No reservations in {}".format(data.get('workspace'))} else: reservation.start_time = data.get('start_time') reservation.end_time = ending_time reservation.duration = increase reservation.reserved_by = user.username reservation.workspace = current_workspace.workspace_number reservation.attendees = data.get('attendees') new_timeframe = Timeframes(start_time=data.get('start_time'), end_time=ending_time, workspace=current_workspace.workspace_number, user_id=user.id ) reservation.save() new_timeframe.save() if previous_timeframe is not None: previous_timeframe.delete() return ReservationSchema().dump(reservation), HTTPStatus.OK except Exception as err: return err.args[0], HTTPStatus.BAD_REQUEST
def add_reservation(self, reservation): user_id = reservation["user_id"] offer_id = reservation["offer_id"] portions = reservation["portions"] session = self.DBSession user=session.query(User).filter(User.id==user_id).first() offer=session.query(Offer).filter(Offer.id==offer_id).first() if not user: return "User does not exist" if not offer: return "Offer does not exist" try: self.sync.create_obj(Reservation(user=user, offer=offer, portions=portions)) print (" > "+user.username+" reserved "+str(portions)+" portions from " +offer.host.username+"'s home made "+offer.info) return "SUCCESS" except IntegrityError: print (" > Failed to reserve for user "+user.username) return "FAILURE"
def post(self): def is_valid_date(year, month, day): day_count_for_month = [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0): day_count_for_month[2] = 29 return 1 <= month <= 12 and 1 <= day <= day_count_for_month[month] json_data = request.get_json() current_user = get_jwt_identity() if not Room.get_by_name(json_data['room']): return {'message': 'Room does not exist'}, HTTPStatus.BAD_REQUEST date_time_str = json_data['date'] date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y') if not 15 < json_data['time'] < 21: return { 'message': 'Only hours between 16-20 can be booked' }, HTTPStatus.BAD_REQUEST if date_time_obj < datetime.now(): return { 'message': 'Date already passed' }, HTTPStatus.BAD_REQUEST, print(date_time_obj) if not is_valid_date(date_time_obj.year, date_time_obj.month, date_time_obj.day): return {'not a real date'}, HTTPStatus.BAD_REQUEST if date_time_obj.weekday() > 5: return { 'message': 'Room cant be reserved for the weekend' }, HTTPStatus.BAD_REQUEST if Reservation.get_by_date_time_room(date_time_obj, json_data['time'], json_data['room']): return {'message': 'Already booked'}, HTTPStatus.BAD_REQUEST reservation = Reservation(name=json_data['name'], user_id=current_user, room=json_data['room'], description=json_data['description'], date=date_time_obj, time=json_data['time']) reservation.save() return reservation.data(), HTTPStatus.CREATED
def put(self, reservation_id): json_data = request.get_json() reservation = Reservation.get_by_id(reservation_id) if reservation is None: return {'message': 'reservation not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != reservation.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN reservation.id = json_data['id'] reservation.time = json_data['time'] reservation.save() return reservation.data(), HTTPStatus.OK
def delete(self): try: user = User.get_by_id(user_id=get_jwt_identity()) json_data = request.get_json() reservation = Reservation.find_by_workspace(json_data['workspace']) current_workspace = Workspace.get_by_number(json_data['workspace']) timeframe = Timeframes.get_for_this_workspace(current_workspace.workspace_number, user.id) if reservation: reservation.delete() else: return {"message": "Already deleted or it doesn't exist"}, HTTPStatus.NOT_FOUND if timeframe is not None: timeframe.delete() return {}, HTTPStatus.OK except Exception as err: return str(err.args[0]) + " missing", HTTPStatus.BAD_REQUEST