def test_url_for_nonexistent(self): """Tests that attempting to get the URL for an unknown model yields an error. """ with self.assertRaises(ValueError): url_for(self.Person)
def test_url_for(self): """Tests the global :func:`flask_restless.url_for` function.""" self.manager.create_api(self.Person, collection_name='people') self.manager.create_api(self.Article, collection_name='articles') with self.flaskapp.test_request_context(): url1 = url_for(self.Person) url2 = url_for(self.Person, resource_id=1) url3 = url_for(self.Person, resource_id=1, relation_name='articles') url4 = url_for(self.Person, resource_id=1, relation_name='articles', related_resource_id=2) assert url1.endswith('/api/people') assert url2.endswith('/api/people/1') assert url3.endswith('/api/people/1/articles') assert url4.endswith('/api/people/1/articles/2')
def collect(self): if g.user is not None and g.user.current_player is not None: player = g.user.current_player else: # FIXME throw proper error return None # Check if the player is in range if self.distance_max is not None: if self.distance_max < self.distance_to(player): return send_action("notice", self, "You are too far away!") # Check if the player already has the maximum amount of items of a class if self.owned_max is not None: if player.has_item(self.__class__) >= self.owned_max: return send_action("notice", self, "You have already collected enough of this!") # Check if the collection is allowed if self.collectible and self.isonmap and self.may_collect(player): # Change owner self.owner = player self.on_collected() DB.session.add(self) DB.session.commit() return redirect(url_for(self.__class__, resource_id=self.id)) else: return send_action("notice", self, "You cannot collect this!")
def test_wiki_page(self): response = self.client.get( url_for(WikiPage, instid=self.wiki_page01.id) ) self.assert_200(response) self.assertEqual(response.json['id'], self.wiki_page01.id) self.assertEqual(len(response.json['versions']), 2)
def player_join(self): """ Make the current player join this world. Called as an API method from a client. """ # Check whether a user is logged in if g.user is None: # FIXME proper error return None # Check whether the user has a player in this world player = Player.query.filter_by(user=g.user, world=self).scalar() if player is None: # Create a new player in this world player = self.game.module.Player() player.name = g.user.username player.world = self player.user = g.user DB.session.add(player) DB.session.commit() # Update current_player g.user.current_player = player DB.session.add(g.user) DB.session.commit() # Redirect to new player object return redirect(url_for(player.__class__, resource_id=player.id)) return redirect("/api/gameobject_player/%i" % player.id)
def test_wiki_page_version_change_status(self): response = self.client.patch( url_for(WikiPageVersion, instid=self.wiki_page_version01.id), headers=[('Content-Type', 'application/json')], data=json.dumps(dict(status=WikiPageVersion.STATUS_NOT_CURRENT)) ) self.assert_200(response) self.assertEqual( response.json['status'], WikiPageVersion.STATUS_NOT_CURRENT )
def handover(self, target_player): # Check if the handover is allowed if self.owner is not None and self.handoverable and self.may_handover(target_player) and target_player.may_accept_handover(self): # Change owner self.owner = target_player self.on_handedover() DB.session.add(self) DB.session.commit() return redirect(url_for(self.__class__, resource_id=self.id)) else: return send_action("notice", self, "You cannot hand this over.")
def test_wiki_page_add(self): response = self.client.post( url_for(WikiPage), headers=[('Content-Type', 'application/json')], data=json.dumps(dict(title='TITLE', text='TEXT')), ) self.assertStatus(response, 201) self.assertEqual(response.json['versions'][0]['title'], 'TITLE') self.assertEqual(response.json['versions'][0]['text'], 'TEXT') self.assertEqual( response.json['versions'][0]['status'], WikiPageVersion.STATUS_CURRENT )
def test_wiki_page_current(self): response = self.client.post( url_for(WikiPage), headers=[('Content-Type', 'application/json')], data=json.dumps(dict(title='TITLE', text='TEXT')), ) wiki_page_id = response.json['id'] response = self.client.get( url_for(WikiPageVersion, q=json.dumps({ "filters": [ {"name": "wiki_page_id", "op": "eq", "val": wiki_page_id}, { "name": "status", "op": "eq", "val": WikiPageVersion.STATUS_CURRENT } ] })) ) self.assert_200(response) self.assertEqual( response.json['objects'][0]['status'], WikiPageVersion.STATUS_CURRENT )
def update_position(self, latlon): if g.user is None: # FIXME proper error return None # Only the own position may be updated if g.user is not self.user: # FIXME proper error return None # Update position self.latitude, self.longitude = [float(x) for x in latlon.split(",")] # FIXME remove slow iteration for item in Item.query.filter_by(world=g.user.current_player.world).all(): if item.auto_collect_radius > 0 and item.distance_to_current_player <= item.auto_collect_radius: item.collect() DB.session.add(self) DB.session.commit() # Redirect to own object return redirect(url_for(self.__class__, resource_id=self.id))
def _get_own_player(): """ Return the GameObject Player of the user that is currently logged in. """ # Check if a user is logged in, in the first place if g.user is None: # Return an error causing the lcient to ask for login # FIXME more specific error return ("", 401) # Check if a player is associated to the user currently if g.user.current_player is None: # Check if the user has any player objects if g.user.players.count() > 0: # Set current player to the first available player of the current user g.user.current_player = g.user.players[0] else: # Create a new player in the first world found World.query.filter_by(enabled=True).first().player_join() # Redirect to the current player object return redirect( url_for(g.user.current_player.__class__, resource_id=g.user.current_player.id))
def test_wiki_page_version(self): response = self.client.get( url_for(WikiPageVersion, instid=self.wiki_page_version01.id) ) self.assert_200(response) self.assertEqual(response.json['id'], self.wiki_page_version01.id)
def test_wiki_page_list(self): response = self.client.get(url_for(WikiPage)) self.assert_200(response)