def desk_confirm(nfc_id, token): try: user = Users.select().where(Users.token == token).get() except Users.DoesNotExist: return {'status': 'Wrong user token', 'code': 'E012'} date = datetime.datetime.now().strftime("%Y%m%d") desk = Desks.select().where(Desks.nfc_id == nfc_id).get() # check if reservation for user exists try: reserve = (Reservations.select() .where(Reservations.id_desk == desk) .where(Reservations.timestamp == date) .get()) if reserve.id_user == user: (Reservations.update(status='TAKEN') .where(Reservations.id_desk == desk) .where(Reservations.timestamp == date) .where(Reservations.id_user == user) .execute()) else: return {'status': 'Cannot take', 'id_desk': desk.id_desk, 'code': 'E011'} except Reservations.DoesNotExist: Reservations.create(id_user=user, id_desk=desk, timestamp=date, status='TAKEN') return {'status': 'Taken', 'id_desk': desk.id_desk}
def get_floor_data(building_name, number, date, token): try: user = Users.select().where(Users.token == token).get() except Users.DoesNotExist: return {'status': 'Wrong user token', 'code': 'E012'} if date == '': need_date = datetime.datetime.now().strftime("%Y%m%d") else: need_date = date try: building = (Buildings.select().where( Buildings.name == building_name).get()) except Buildings.DoesNotExist: return {'status': 'No Building found', 'code': 'E005'} try: floor = (Floors.select().where( Floors.id_building == building.id_building).where( Floors.number == number).get()) except Floors.DoesNotExist: return {'status': 'No Floor found', 'code': 'E005'} try: desk = (Desks.select().where( Desks.id_building == building.id_building).where( Desks.number == number)) desk_list = [{ 'id_desk': desk_data.id_desk, 'status': 'FREE' } for desk_data in desk] except Floors.DoesNotExist: desk_list = [] # get reservation data for index, desk in enumerate(desk_list): reservation_query = (Reservations.select().where( Reservations.id_desk == desk['id_desk']).where( Reservations.timestamp == need_date)) if reservation_query.exists(): reserv = reservation_query.get() desk_list[index]['status'] = reserv.status if reserv.id_user == user: if reserv.status == 'RESERVED': desk_list[index]['status'] = 'MY_RESERVED' elif reserv.status == 'TAKEN': desk_list[index]['status'] = 'MY_TAKEN' row_str_list = floor.shape.split(';') column_str_list = row_str_list[0].split(',') return_shape = floor.shape.replace(';', ',') return { 'shape': return_shape, 'desks_id': desk_list, 'width': len(column_str_list), 'height': len(row_str_list) }
def create_user(username, password, token=None): user_query = Users.select().where(Users.username == username) if user_query.exists(): print('TEST') return {'status': 'Username: "******" exists'.format(username)} else: pass_hash = bcrypt.hash(password) Users.insert(username=username, pass_hash=pass_hash, token=token).execute() return {'Username: "******" created'.format(username)}
def validate_user(username, password): try: user = Users.select().where(Users.username == username).get() except Users.DoesNotExist: return {'verify': False} if bcrypt.verify(password, user.pass_hash): token = uuid.uuid4() Users.update(token=token).where(Users.id_user == user).execute() return {'status': True, 'token': str(token)} return {'verify': False}
def get_desk_data(id_desk, date, token): if token: try: user = Users.select().where(Users.token == token).get() except Users.DoesNotExist: return {'status': 'Wrong user token', 'code': 'E012'} if date == '': need_date = datetime.datetime.now().strftime("%Y%m%d") else: need_date = date try: desk = (Desks.select(Desks, Floors) .join(Floors, on=((Desks.number == Floors.number) & (Desks.id_building == Floors.id_building))) .where(Desks.id_desk == id_desk).get()) except Desks.DoesNotExist: return {'status': 'No Desk found', 'code': 'E005'} desk_cursor = database.execute_sql("""SELECT id_desk, d.number, b.name FROM desks d JOIN floors f ON d.number = f.number AND d.id_building = f.id_building JOIN buildings b ON f.id_building = b.id_building WHERE d.id_desk = %s LIMIT %s OFFSET %s""", [id_desk, 1, 0]) desk = desk_cursor.fetchall()[0] try: reservation = (Reservations.select() .where(Reservations.id_desk == desk[0]) .where(Reservations.timestamp == need_date) .get()) if token: if reservation.id_user == user: reservation_dict = {'status': "MY_{}".format(reservation.status), 'username': reservation.id_user.username} else: reservation_dict = {'status': reservation.status, 'username': reservation.id_user.username} else: reservation_dict = {'status': reservation.status, 'username': reservation.id_user.username} except Reservations.DoesNotExist: reservation_dict = {'status': 'FREE', 'username': ''} return {'building_name': desk[2], 'floor_name': desk[1], 'id_desk': desk[0], 'date': need_date, 'status': reservation_dict['status'], 'username': reservation_dict['username']}
def find_me(token): try: user = Users.select().where(Users.token == token).get() except Users.DoesNotExist: return {'status': 'Wrong user token', 'code': 'E012'} date = datetime.datetime.now().strftime("%Y%m%d") try: reservation = (Reservations.select().where( Reservations.id_user == user).where( Reservations.timestamp == date).where( (Reservations.status == 'TAKEN') | (Reservations.status == 'RESERVED')).get()) building_name = reservation.id_desk.number.id_building.name number = reservation.id_desk.number.number return {'building_name': building_name, 'number': number} except Reservations.DoesNotExist: return {'status': 'No current desk reserved or took', 'code': 'E011'}
def desk_reserve(id_desk, date, token): try: user = Users.select().where(Users.token == token).get() except Users.DoesNotExist: return {'status': 'Wrong user token', 'code': 'E012'} # check if reservation exists reserve = (Reservations.select() .where(Reservations.id_desk == id_desk) .where(Reservations.timestamp == date) .where(Reservations.id_user != user)) if reserve.exists(): return {'status': 'Cannot reserve', 'id_desk': id_desk, 'code': 'E010'} Reservations.get_or_create(id_user=user, id_desk=id_desk, timestamp=date, status='RESERVED') return {'status': 'Reserved', 'id_desk': id_desk}
def delete_reservation(token, date): if date == '': need_date = datetime.datetime.now().strftime("%Y%m%d") else: need_date = date try: user = Users.select().where(Users.token == token).get() except Users.DoesNotExist: return {'status': 'Wrong user token', 'code': 'E012'} reservation_query = (Reservations.select() .where(Reservations.id_user == user) .where((Reservations.status == 'TAKEN') | (Reservations.status == 'RESERVED')) .where(Reservations.timestamp == need_date)) if reservation_query.exists(): for reservation in reservation_query: Reservations.delete().where(Reservations.id_reservation == reservation).execute() return {'status': 'Deleted reservation', 'date': date, 'user': user.username} else: return {'status': 'No reservation', 'date': date, 'user': user.username, 'code': 'W001'}