def reserve(request): # check if the time is ok context = RequestContext(request) if request.method == 'POST': form = forms.ReserveForm(request.POST) if form.is_valid(): # save to database # print "user name: ", form.cleaned_data['username'] currdate = datetime.date.today() usr = form.cleaned_data['user'] r_id = form.cleaned_data['resource_id'] st = datetime.datetime.combine(currdate, form.cleaned_data['starttime']) dur = form.cleaned_data['duration'] # user not logged in if usr == '*': return redirect('login') # check if there is other reservations in this time # get the current time curr_time = datetime.datetime.now() if helper.Helper().isAvailable(r_id, st, dur, curr_time): reserve = models.Reservation(user=usr, resource=r_id, starttime=st, duration=dur, reservetime=curr_time) reserve.put() return redirect('home') else: q = db.GqlQuery( "SELECT * FROM Resource where __key__ = KEY('Resource', :1)", int(r_id)) resource = q.get() reserve_form = forms.ReserveForm( initial={ 'user': usr, 'resource_id': r_id, 'starttime': st.strftime("%I:%M %p"), 'duration': dur }) message = 'Sorry, this resource is reserved in this time slot.' return render_to_response( 'resource.html', { 'loggedin': True, 'resource': resource, 'reserve_form': reserve_form, 'message': message }, context) else: html = "<html><body>ID: %s.</body></html>" % form.errors return HttpResponse(html) return redirect('home')
def create_reservation(db: Session, reservation_data: schemas.Do_reservation): validate_customer(db=db, customer_id=reservation_data.customer_id) validate_table(db=db, table_id=reservation_data.table_id) if (reservation_data.slot not in [1, 2, 3, 4]): raise HTTPException(status_code=404, detail="Wrong slot. Please enter correct slot.") add_rrservation = models.Reservation(**reservation_data.dict()) db.add(add_rrservation) db.commit() db.refresh(add_rrservation) return {"Reservation done"}
def request_reservation(): desired_date = datetime.date.fromisoformat(flask.request.values['date']) desired_start = flask.request.values['start'] desired_end = flask.request.values['end'] machine = db.db.session.query(models.Machine).filter( models.Machine.id == flask.request.values['machine_id']).one() member = db.db.session.query(models.Member).filter( models.Member.id == flask.request.values['member_id']).one() reservations = db.db.session.query(models.Reservation).filter( models.Reservation.date == desired_date).filter( models.Reservation.machine_id == machine.id) if reservations is not None: for reservation in reservations: if (time.strptime(reservation.end, TIME_FORMAT) <= time.strptime( desired_start, TIME_FORMAT) or time.strptime(reservation.start, TIME_FORMAT) >= time.strptime(desired_end, TIME_FORMAT)): pass else: # maybe also return the calendar return flask.jsonify({ msg: "sorry, but {} has a booking in that time slot already". format(reservation.member.name), status_code: 409 }), 409 db.db.session.add( models.Reservation(date=desired_date, start=desired_start, end=desired_end, machine_id=machine.id, member_id=member.id)) db.db.session.commit() return flask.jsonify({ 'msg': 'congratulations! {} is booked on the {} for {} - {} on {}. Don\'t be late!' .format(member.name, machine.name, desired_start, desired_end, desired_date), 'status_code': 200 }), 200
def delete_reservation(db: Session, r_id: int): validate_reservation(db=db, reservation_id=r_id) reservation_remove = db.query( models.Reservation).filter(models.Reservation.id == r_id).first() db.delete(reservation_remove) waiting_exist = validate_waiting(db=db, waiting_data=reservation_remove) if waiting_exist is not None: add_reservation = models.Reservation() add_reservation.customer_id = waiting_exist.customer_id add_reservation.table_id = waiting_exist.table_id add_reservation.slot = waiting_exist.slot add_reservation.r_date = waiting_exist.r_date db.add(add_reservation) # Add into reservation table db.delete(waiting_exist) # Remove from waiting table db.commit() db.refresh(add_reservation) return {"Reservation Cancelled"} db.commit() return {"Reservation Cancelled"}
def seed(): """Seed the database with sample data.""" import models # the rooms room_features = ['Projector', 'TV', 'Webcam', 'Phone Line'] rooms = { '1560': ['Projector'], '1561': ['TV', 'Webcam'], '1562': ['Projector'], '1563': ['TV'], '1564': ['Projector'], '1565': ['TV', 'Webcam'], '1665': ['TV', 'Webcam'], '1663': ['TV'], '1662': ['Projector'], '1661': ['Projector'], '1660': ['Projector'] } feature_dict = {} for feature in room_features: f = models.RoomFeature(name=feature) get_db().add(f) feature_dict[feature] = f rooms_dict = {} for room_number in rooms: r = models.Room(number=room_number) for feature in rooms[room_number]: f = feature_dict[feature] r.features.append(f) rooms_dict[room_number] = r get_db().add(r) # the types of team teamtype_dict = {} teamtype_dict['single'] = models.TeamType( name='single', priority=4, advance_time=7 * 2 # 2 weeks ) get_db().add(teamtype_dict['single']) get_db().add( models.TeamType( name='other_team', priority=4, advance_time=7 * 2 # 2 weeks )) get_db().add( models.TeamType( name='class', priority=3, advance_time=7 * 2 # 2 weeks )) get_db().add( models.TeamType( name='colab_class', priority=2, advance_time=7 * 2 # 2 weeks )) get_db().add( models.TeamType( name='senior_project', priority=1, advance_time=7 * 2 # 2 weeks )) # the permissions perm_names = [ 'team.create', 'team.create.elevated', 'team.delete', 'team.delete.elevated', 'team.read', 'team.read.elevated', 'team.update', 'team.update.elevated', 'reservation.create', 'reservation.delete', 'reservation.delete.elevated', 'reservation.read', 'reservation.update', 'reservation.update.elevated', 'room.update.elevated', 'room.create.elevated', 'room.read', 'room.delete.elevated', 'feature.create', 'feature.delete', 'feature.update', 'feature.read', 'role.create', 'role.delete', 'role.update' ] perm_dict = {} for perm in perm_names: p = models.Permission(name=perm) get_db().add(p) perm_dict[perm] = p roles = { 'student': [ 'team.create', 'team.delete', 'team.read', 'team.update', 'reservation.create', 'reservation.delete', 'reservation.read', 'reservation.update', 'room.read', 'feature.read' ], 'labbie': [ 'team.create', 'team.delete', 'team.read', 'team.update', 'reservation.create', 'reservation.delete', 'reservation.read', 'reservation.update', 'room.read', 'feature.read', 'team.read.elevated' ], 'professor': [ 'team.create', 'team.delete', 'team.read', 'team.update', 'reservation.create', 'reservation.delete', 'reservation.read', 'reservation.update', 'room.read', 'feature.read', 'team.create.elevated', 'team.read.elevated' ], 'admin': [ 'team.create', 'team.create.elevated', 'team.delete', 'team.delete.elevated', 'team.read', 'team.read.elevated', 'team.update', 'team.update.elevated', 'reservation.create', 'reservation.delete', 'reservation.delete.elevated', 'reservation.read', 'reservation.update', 'reservation.update.elevated', 'room.update.elevated', 'room.create.elevated', 'room.read', 'room.delete.elevated', 'feature.create', 'feature.delete', 'feature.update', 'feature.read', 'role.create', 'role.delete', 'role.update' ] } users_dict = {} for role in roles: r = models.Role(name=role) for permission in roles[role]: p = perm_dict[permission] r.permissions.append(p) get_db().add(r) # seed a user TODO don't do this in production? u = models.User(name=role, email=role + "@example.com") u.roles.append(r) team = models.Team(u.name) team_type = teamtype_dict['single'] team.team_type = team_type team.members.append(u) u.teams.append(team) users_dict[role] = u get_db().add(team) get_db().add(u) reservation = models.Reservation( start=datetime.datetime.now() + datetime.timedelta(days=7), end=datetime.datetime.now() + datetime.timedelta(days=7, hours=1), team=users_dict['admin'].teams[0], room=rooms_dict['1660'], created_by=users_dict['admin']) get_db().add(reservation) get_db().commit()