def post(self, id_user, id_estate): data = Room.parser_add.parse_args() try: estate = EstateModel.find_by_id(id_estate) # We handle the case of an owner can only modify the characteristics of his property # without having access to the edition of the other owners if id_user != estate.id_user: raise PermissionError room = RoomModel(id_estate, **data) room.save_to_db() return room.json(), 201 except NoResultFound: return { 'message': 'Please make sure the estate id is correct' }, 400 except PermissionError: return {'message': 'Access denied'}, 403 except: return { 'message': 'An error occurred while inserting the room' }, 500
def post(self): data = AddHouse.parser.parse_args() house_type = TypeModel.find_by_name(data['house_type']) if not house_type: return {'message':"invalide house type"}, 400 city = CityModel.find_by_name(data['city']) if not city: return {'message':"invalide city name"}, 400 house = HouseModel(data['name'],data['description'],house_type.id,city.id) try: house.save_to_db() except: return {"message": "An error occurred creating the store."}, 500 # add the new house attached rooms if data['rooms'] is not None: rooms =[ast.literal_eval(rooms) for rooms in data['rooms']] for x in rooms: room = RoomModel(x['characteristics'],house.id) room.save_to_db() return house.json(), 201
def test_crud(self): with self.app_context(): room = RoomModel(_id=1, company='Test Company', location='Test Location', name='Test Name', description='Some Test Description', rateofescape='10/10', image='test.jpg', duration='60 minutes', playedon='01/01/2018') self.assertIsNone(RoomModel.find_by_id(1)) room.save_to_db() self.assertIsNotNone( RoomModel.find_by_name_company('Test Name', 'Test Company')) self.assertIsNotNone(RoomModel.find_by_id(1)) self.assertIsNotNone(RoomModel.get_all_rooms()) room.delete_from_db() self.assertIsNone(RoomModel.find_by_id(1)) self.assertEqual(RoomModel.get_all_rooms(), {'rooms': []})
def test_create_duplicate_room(self): room = RoomModel(_id=1, company='Test Company', location='Test Location', name='Test Name', description='Some Test Description', rateofescape='10/10', image='test.jpg', duration='60 minutes', playedon='01/01/2018') data = { 'id': 1, 'company': 'Test Company', 'location': 'Test Location', 'name': 'Test Name', 'description': 'Some Test Description', 'rateofescape': '10/10', 'image': 'test.jpg', 'duration': '60 minutes', 'playedon': '01/01/2018' } with self.app() as client: with self.app_context(): room.save_to_db() resp = client.post('/room/create', data=data) self.assertEqual(resp.status_code, 400) self.assertDictEqual( { 'message': "'Test Name' by 'Test Company' already exists" }, json.loads(resp.data))
def test_room_list(self): with self.app() as client: with self.app_context(): room = RoomModel(_id=1, company='Test Company', location='Test Location', name='Test Name', description='Some Test Description', rateofescape='10/10', image='test.jpg', duration='60 minutes', playedon='01/01/2018') data = { 'id': 1, 'company': 'Test Company', 'location': 'Test Location', 'name': 'Test Name', 'description': 'Some Test Description', 'rateofescape': '10/10', 'image': 'test.jpg', 'duration': '60 minutes', 'playedon': '01/01/2018' } room.save_to_db() resp = client.get('/rooms') self.assertDictEqual({'rooms': [data]}, json.loads(resp.data))
def post(self): data = RoomCreate.parser.parse_args() room = RoomModel.find_by_name_company(data['name'], data['company']) if room: return { 'message': "'{}' by '{}' already exists".format(data['name'], data['company']) }, 400 room = RoomModel(None, **data) room.save_to_db() return {'message': 'Room Created'}, 201
def test_get_room(self): with self.app() as client: with self.app_context(): room = RoomModel(_id=1, company='Test Company', location='Test Location', name='Test Name', description='Some Test Description', rateofescape='10/10', image='test.jpg', duration='60 minutes', playedon='01/01/2018') room.save_to_db() resp = client.get('/room/1', headers={'Authorization': self.access_token}) self.assertEqual(resp.status_code, 200)
def test_delete_room(self): with self.app() as client: with self.app_context(): room = RoomModel(_id=1, company='Test Company', location='Test Location', name='Test Name', description='Some Test Description', rateofescape='10/10', image='test.jpg', duration='60 minutes', playedon='01/01/2018') room.save_to_db() resp = client.delete( '/room/1', data={ 'name': 'Test Name', 'company': 'Test Company' }, headers={'Authorization': self.access_token}) self.assertEqual(resp.status_code, 200) self.assertDictEqual({'message': 'Room was deleted'}, json.loads(resp.data))
def TraverseOnce(self, token, move): opposite_dirs = { "n": "s", "e": "w", "s": "n", "w": "e" } time.sleep(1) # Get the room the player is currently in: player_status_response = requests.get( 'https://lambda-treasure-hunt.herokuapp.com/api/adv/init/', headers={'authorization': token}).json() if len(player_status_response['errors']) > 0: return player_status_response, 400 time.sleep(1) # if the direction the player wants to move is in their current rooms direction list (if its a possible direction to move) if move in player_status_response["exits"]: # See if we have the players current room in our database. found_room = RoomModel.find_by_id( player_status_response['room_id']) # If we don't have the room in our database, we haven't seen it before. So we add it to our database. if not found_room: # room_coordinates turns "(32,45)" -> ["32", "45"] room_coordinates = re.findall( r"\d+", player_status_response['coordinates']) avail_exits = { "n": None if "n" not in player_status_response["exits"] else "?", "w": None if "w" not in player_status_response["exits"] else "?", "e": None if "e" not in player_status_response["exits"] else "?", "s": None if "s" not in player_status_response["exits"] else "?", } new_room_data = { "id": player_status_response['room_id'], "title": player_status_response['title'], "description": player_status_response['description'], "x": int(room_coordinates[0]), "y": int(room_coordinates[1]), "terrain": player_status_response['terrain'], "elevation": player_status_response['elevation'] } new_room_data.update(avail_exits) found_room = RoomModel(**new_room_data) # Now travel and save the next room result in the previous room player_travel_request = requests.post('https://lambda-treasure-hunt.herokuapp.com/api/adv/move/', json={ "direction": move}, headers={'authorization': token}).json() try: new_room_id = player_travel_request['room_id'] traveled_into_room = RoomModel.find_by_id(new_room_id) if not traveled_into_room: # Create room record for the room we just traveled into if not found new_room_coordinates = re.findall( r"\d+", player_travel_request['coordinates']) avail_exits = { "n": None if "n" not in player_travel_request["exits"] else "?", "w": None if "w" not in player_travel_request["exits"] else "?", "e": None if "e" not in player_travel_request["exits"] else "?", "s": None if "s" not in player_travel_request["exits"] else "?", } traveled_into_room_data = { "id": new_room_id, "title": player_travel_request['title'], "description": player_travel_request['description'], "x": int(new_room_coordinates[0]), "y": int(new_room_coordinates[1]), "terrain": player_travel_request['terrain'], "elevation": player_travel_request['elevation'] } traveled_into_room_data.update(avail_exits) traveled_into_room = RoomModel( **traveled_into_room_data) setattr(traveled_into_room, opposite_dirs[move], str( found_room.json()['id'])) setattr(found_room, move, new_room_id) found_room.save_to_db() return player_travel_request, 200 except KeyError: return player_travel_request, 400 # Room is found else: # Check if we have the next room's id that the player is traveling to. If we do, travel there and return response to user. if move in found_room.json()["exits"] and found_room.json()["exits"][move] is not None and found_room.json()["exits"][move] is not "?": next_room = found_room.json()["exits"][move] player_travel_request = requests.post('https://lambda-treasure-hunt.herokuapp.com/api/adv/move/', json={ "direction": move, "next_room_id": str(next_room)}, headers={'authorization': token}).json() return player_travel_request, 200 # If we don't, travel that direction without wise explorer bonus. Then save that direction result in our found_room for next time. else: player_travel_request = requests.post('https://lambda-treasure-hunt.herokuapp.com/api/adv/move/', json={ "direction": move}, headers={'authorization': token}).json() try: # Create new room we just traveled into, and save the direction to the previous room. new_found_room = RoomModel.find_by_id( player_travel_request["room_id"]) new_room_id = player_travel_request['room_id'] if not new_found_room: room_coordinates = re.findall( r"\d+", player_travel_request['coordinates']) avail_exits = { "n": None if "n" not in player_travel_request["exits"] else "?", "w": None if "w" not in player_travel_request["exits"] else "?", "e": None if "e" not in player_travel_request["exits"] else "?", "s": None if "s" not in player_travel_request["exits"] else "?", } new_room_data = { "id": new_room_id, "title": player_travel_request['title'], "description": player_travel_request['description'], "x": int(room_coordinates[0]), "y": int(room_coordinates[1]), "terrain": player_travel_request['terrain'], "elevation": player_travel_request['elevation'] } new_room_data.update(avail_exits) new_found_room = RoomModel(**new_room_data) setattr(new_found_room, opposite_dirs[move], str( player_status_response["room_id"])) setattr(found_room, move, new_room_id) new_found_room.save_to_db() found_room.save_to_db() return player_travel_request, 200 except KeyError: return player_travel_request, 400 else: return {'error': True, 'message': 'There is no exit in that direction.'}, 400