def post(self, request, code, *args, **kwargs): self.game = _GameMiddleware(code) self.creator = GameCreatorMiddleware(request.user) self.maps = MapsMiddleware() if not self.creator.is_authorized_to_access_game(code): return handler(request, 404) if 'location_order' in request.POST.keys(): location = request.POST['location_order'].title().strip() return self._add_location(request, code, location) # Search for the location and return latitude and longitude to display to the user for confirmation elif 'locationSearch' in request.POST.keys(): location = request.POST['locationSearch'].title() try: latitude, longitude = self.maps.get_coordinate( request.POST['locationSearch']) latitude = float(latitude) longitude = float(longitude) except: latitude = -33.865143 longitude = 151.209900 location = location + " * Not Found - Please Try Again *" return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'lat_long': [latitude, longitude], 'location_name': location, 'code': code })
def get(self, request, game_code, location_code, *args, **kwargs): self.locations = GameCreatorMiddleware(request.user.username) self.game = _GameMiddleware(game_code) self.maps = MapsMiddleware() this_location = self.locations.get_location_by_code(location_code) this_location_copy = self.locations.get_location_by_code(location_code) for x in this_location: location_name = str(x) if not self.locations.is_authorized_to_access_game(game_code): return handler(request, 404) latitude, longitude = self.maps.get_coordinate(location_name) latitude = float(latitude) longitude = float(longitude) return render(request, self.template_name, context={ 'locations_code': this_location, 'game_details': self.game.get_code_and_name(), 'game_player_name': self.locations.get_name(), 'game_player_username': self.locations.get_username(), 'lat_long': [latitude, longitude], 'game_code': self.game.game.code, 'location_code': this_location_copy.first() })
def get(self, request, code, *args, **kwargs): # temp game self.game_creator = GameCreatorMiddleware(request.user.username) game = self.game_creator.get_game(code) if not game: return handler(request, 404) if not self.game_creator.is_authorized_to_access_game(code): return handler(request, 404) if game.game.live: self.template_name = 'game-create-live.html' elif game.game.archived: self.template_name = 'game-create-archived.html' self.maps = MapsMiddleware() return render( request, self.template_name, context={ 'locations_code': self.game_creator.get_ordered_locations_of_game(code), 'game_details': self.game_creator.get_code_and_name(code), 'code': code, 'lat_long': self.maps.get_list_of_long_lat(code) })
def get(self, request, code, *args, **kwargs): self.game = _GameMiddleware(code) if not self.game: return handler(request, 404) self.player = GamePlayerMiddleware(request.user.username) if not self.player.is_authorized_to_access_game(code): return handler(request, 404) self.maps = MapsMiddleware() lat_long = [] visited = self.player.locations_visited(code) for location in visited: if location[1] != "???": temp = [] latitude, longitude = self.maps.get_coordinate(location[1]) temp.append(float(latitude)) temp.append(float(longitude)) temp.append(location[1]) lat_long.append(temp) return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'visited': self.player.locations_visited(code), 'lat_long': lat_long })
def test_can_delete_location_of_unpublished_game_of_yours(self): maps = MapsMiddleware() creator_middleware = GameCreatorMiddleware(self.david.username) self.assertTrue( creator_middleware.is_authorized_to_access_game("13T2-JFRN")) # delete location # we know that if this delete_location returned true, the location order also got refactored correctly # due to the implementation of this function self.assertTrue( maps.delete_location("13T2-JFRN", "24Q9-72EG"), "ERROR! Cannot delete location of an " "unpublished game of yours")
def post(self, request, *args, **kwargs): self.game = _GameMiddleware(request.POST['game_code']) if not self.game: return handler(request, 404) self.player = GamePlayerMiddleware(request.user.username) if not self.player.is_authorized_to_access_game( request.POST['game_code']): return handler(request, 404) error = "" if len(request.POST['location_code']) == 9: result = self.player.visit_location(request.POST['location_code'], request.POST['game_code']) if not result: error = "Invalid Location Code" else: error = "Invalid Location Code" self.maps = MapsMiddleware() lat_long = [] visited = self.player.locations_visited(request.POST['game_code']) for location in visited: if location[1] != "???": temp = [] latitude, longitude = self.maps.get_coordinate(location[1]) temp.append(float(latitude)) temp.append(float(longitude)) temp.append(location[1]) lat_long.append(temp) return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'visited': self.player.locations_visited( request.POST['game_code']), 'lat_long': lat_long, 'error': error })
def _update_title_post_request(self, request, *args, **kwargs): self.maps = MapsMiddleware() self.game_creator = GameCreatorMiddleware(request.user.username) self.game = _GameMiddleware(kwargs['code']) self.game.change_name(request.POST['title']) return render(request, self.template_name, context={ 'locations_code': self.game_creator.get_ordered_locations_of_game( kwargs['code']), 'game_details': self.game_creator.get_code_and_name(kwargs['code']), 'code': kwargs['code'], 'lat_long': self.maps.get_list_of_long_lat(kwargs['code']) })
def post(self, request, game_code, location_code, *args, **kwargs): self.locations = GameCreatorMiddleware(request.user.username) self.game = _GameMiddleware(game_code) self.maps = MapsMiddleware() if not self.locations.is_authorized_to_access_game( request.POST['game_code']): return handler(request, 404) if 'delete_location_code' in request.POST.keys( ) and self.locations.can_change_game(request.POST['game_code']): return self._delete_location( request, game_code, request.POST['delete_location_code'], self.locations.get_location_by_code(location_code)) if 'code' in request.POST.keys(): return self._change_clue(request, request.POST['game_code'], request.POST['code'], *args, **kwargs) return handler(request, 404)
def _update_location_order_post_request(self, request, *args, **kwargs): self.maps = MapsMiddleware() self.game_creator = GameCreatorMiddleware(request.user.username) codes_order_list = request.POST['location_order'].split(',') self.game_creator.update_location_order(codes_order_list, kwargs['code']) return render(request, self.template_name, context={ 'locations_code': self.game_creator.get_ordered_locations_of_game( kwargs['code']), 'game_details': self.game_creator.get_code_and_name(kwargs['code']), 'code': kwargs['code'], 'lat_long': self.maps.get_list_of_long_lat(kwargs['code']) })
class LocationAdd(LoginRequiredMixin, generic.TemplateView): template_name = 'addlocation.html' login_url = '/start' creator = None def get(self, request, code, *args, **kwargs): self.game = _GameMiddleware(code) self.creator = GameCreatorMiddleware(request.user) if not self.creator.is_authorized_to_access_game(code): return handler(request, 404) return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'lat_long': [-33.865143, 151.209900], 'location_name': "" }) # Handling of the various post requests to the add location page # Paramaters: # code: the game code of game to be modified # location_order: the location to be added to the game # location_search: the location to be searched for def post(self, request, code, *args, **kwargs): self.game = _GameMiddleware(code) self.creator = GameCreatorMiddleware(request.user) self.maps = MapsMiddleware() if not self.creator.is_authorized_to_access_game(code): return handler(request, 404) if 'location_order' in request.POST.keys(): location = request.POST['location_order'].title().strip() return self._add_location(request, code, location) # Search for the location and return latitude and longitude to display to the user for confirmation elif 'locationSearch' in request.POST.keys(): location = request.POST['locationSearch'].title() try: latitude, longitude = self.maps.get_coordinate( request.POST['locationSearch']) latitude = float(latitude) longitude = float(longitude) except: latitude = -33.865143 longitude = 151.209900 location = location + " * Not Found - Please Try Again *" return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'lat_long': [latitude, longitude], 'location_name': location, 'code': code }) # Add location to a game # Paramaters: # code: the game code of game to be modified # location: the new name of the location to be added to the game def _add_location(self, request, code, location): created = self.maps.create_game_location(code, location) return HttpResponseRedirect('/game/create/' + code + "/" + created.code)
def _delete_location(self, request, game_code, location_code, *args, **kwargs): self.maps = MapsMiddleware() self.maps.delete_location(game_code, location_code) return HttpResponseRedirect('/game/create/' + game_code)
class GamePlayingListView(LoginRequiredMixin, generic.TemplateView): template_name = 'play-games.html' login_url = '/start' player = None def get(self, request, code, *args, **kwargs): self.game = _GameMiddleware(code) if not self.game: return handler(request, 404) self.player = GamePlayerMiddleware(request.user.username) if not self.player.is_authorized_to_access_game(code): return handler(request, 404) self.maps = MapsMiddleware() lat_long = [] visited = self.player.locations_visited(code) for location in visited: if location[1] != "???": temp = [] latitude, longitude = self.maps.get_coordinate(location[1]) temp.append(float(latitude)) temp.append(float(longitude)) temp.append(location[1]) lat_long.append(temp) return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'visited': self.player.locations_visited(code), 'lat_long': lat_long }) # Handling of when a game player inputs a location code within a game # Post Request Values: # game_code: game code retrieved from a hidden input in the form # location_code: location code inputed by a user def post(self, request, *args, **kwargs): self.game = _GameMiddleware(request.POST['game_code']) if not self.game: return handler(request, 404) self.player = GamePlayerMiddleware(request.user.username) if not self.player.is_authorized_to_access_game( request.POST['game_code']): return handler(request, 404) error = "" if len(request.POST['location_code']) == 9: result = self.player.visit_location(request.POST['location_code'], request.POST['game_code']) if not result: error = "Invalid Location Code" else: error = "Invalid Location Code" self.maps = MapsMiddleware() lat_long = [] visited = self.player.locations_visited(request.POST['game_code']) for location in visited: if location[1] != "???": temp = [] latitude, longitude = self.maps.get_coordinate(location[1]) temp.append(float(latitude)) temp.append(float(longitude)) temp.append(location[1]) lat_long.append(temp) return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'visited': self.player.locations_visited( request.POST['game_code']), 'lat_long': lat_long, 'error': error })
class GameCreationListView(LoginRequiredMixin, generic.TemplateView): template_name = 'game-create.html' login_url = '/start' form = GameRenameForm # Depending on the game status various versions of this page will be displayed # If the game is NOT PUBLISHED then game settings can be still modifed and locations can be added # If the game is LIVE then the game can only be stopped # If the game is ARCHIVED then no changes can be made to the game including deletion def get(self, request, code, *args, **kwargs): # temp game self.game_creator = GameCreatorMiddleware(request.user.username) game = self.game_creator.get_game(code) if not game: return handler(request, 404) if not self.game_creator.is_authorized_to_access_game(code): return handler(request, 404) if game.game.live: self.template_name = 'game-create-live.html' elif game.game.archived: self.template_name = 'game-create-archived.html' self.maps = MapsMiddleware() return render( request, self.template_name, context={ 'locations_code': self.game_creator.get_ordered_locations_of_game(code), 'game_details': self.game_creator.get_code_and_name(code), 'code': code, 'lat_long': self.maps.get_list_of_long_lat(code) }) # Handling of the various POST requests that can be made on the game create page def post(self, request, *args, **kwargs): self.game_creator = GameCreatorMiddleware(request.user.username) if not self.game_creator.is_authorized_to_access_game(kwargs['code']): return handler(request, 404) if not self.game_creator.can_change_game( kwargs['code']) and 'game_stop' not in request.POST.keys(): return handler(request, 404) if 'title' in request.POST.keys() and 'code' in kwargs.keys(): return self._update_title_post_request(request, **kwargs) elif 'location_order' in request.POST.keys(): return self._update_location_order_post_request(request, **kwargs) elif 'game_delete' in request.POST.keys(): return self._delete_game(request, *args, **kwargs) elif 'game_start' in request.POST.keys(): return self._start_game(request, *args, **kwargs) elif 'game_stop' in request.POST.keys( ) and request.POST['game_stop'] != '': return self._stop_game(request, *args, **kwargs) # Modify the title/name of the game # Paramaters: # code: the game code of game to be modified # title: the new name of the game def _update_title_post_request(self, request, *args, **kwargs): self.maps = MapsMiddleware() self.game_creator = GameCreatorMiddleware(request.user.username) self.game = _GameMiddleware(kwargs['code']) self.game.change_name(request.POST['title']) return render(request, self.template_name, context={ 'locations_code': self.game_creator.get_ordered_locations_of_game( kwargs['code']), 'game_details': self.game_creator.get_code_and_name(kwargs['code']), 'code': kwargs['code'], 'lat_long': self.maps.get_list_of_long_lat(kwargs['code']) }) # Update the order of locations in a game # Paramaters: # location_order: list of location codes in the new order # code: the game code of game to be modified def _update_location_order_post_request(self, request, *args, **kwargs): self.maps = MapsMiddleware() self.game_creator = GameCreatorMiddleware(request.user.username) codes_order_list = request.POST['location_order'].split(',') self.game_creator.update_location_order(codes_order_list, kwargs['code']) return render(request, self.template_name, context={ 'locations_code': self.game_creator.get_ordered_locations_of_game( kwargs['code']), 'game_details': self.game_creator.get_code_and_name(kwargs['code']), 'code': kwargs['code'], 'lat_long': self.maps.get_list_of_long_lat(kwargs['code']) }) # Delete a game that has been created # Paramaters: # game_delete: the game code of game to be deleted def _delete_game(self, request, *args, **kwargs): self.game_creator.delete_game(request.POST['game_delete']) return HttpResponseRedirect('/') # Start the game so that game players can play it # Paramaters: # game_start: the game code of game to be started def _start_game(self, request, *args, **kwargs): self.game_creator.start_game(request.POST['game_start']) return HttpResponseRedirect('/game/create/' + request.POST['game_start']) # Stop a game that is currently being played and as such archiving it # Paramaters: # game_stop: the game code of game to be stopped def _stop_game(self, request, *args, **kwargs): self.game_creator.stop_game(request.POST['game_stop']) return HttpResponseRedirect('/game/create/' + request.POST['game_stop'])
class DeleteLocationTest(DatabaseRequiredTests): def setUp(self): super(DeleteLocationTest, self).setUp() self.maps = MapsMiddleware() def test_can_delete_location_of_unpublished_game_of_yours(self): maps = MapsMiddleware() creator_middleware = GameCreatorMiddleware(self.david.username) self.assertTrue( creator_middleware.is_authorized_to_access_game("13T2-JFRN")) # delete location # we know that if this delete_location returned true, the location order also got refactored correctly # due to the implementation of this function self.assertTrue( maps.delete_location("13T2-JFRN", "24Q9-72EG"), "ERROR! Cannot delete location of an " "unpublished game of yours") def test_cannot_delete_location_of_live_game_of_yours(self): creator_middleware = GameCreatorMiddleware(self.david.username) self.assertTrue( creator_middleware.is_authorized_to_access_game("9XMQ-FXYJ")) # delete location we know that if can_change_game returns False, the post request wont succeed and the # location cannot be deleted self.assertFalse(creator_middleware.can_change_game("9XMQ-FXYJ"), "ERROR! User can change game even if it's live") def test_cannot_delete_location_of_archived_game_of_yours(self): creator_middleware = GameCreatorMiddleware(self.david.username) self.assertTrue( creator_middleware.is_authorized_to_access_game("9XMQ-FXYJ")) # archive the game creator_middleware.stop_game("9XMQ-FXYJ") # delete location we know that if can_change_game returns False, the post request wont succeed and the # location cannot be deleted self.assertFalse(creator_middleware.can_change_game("9XMQ-FXYJ"), "ERROR! User can change game even if it's archived") def test_cannot_delete_location_of_game_of_another_creator(self): creator_middleware = GameCreatorMiddleware(self.david.username) # must not be able to access the game # the code won't go past this point so we know that he cannot delete the game of another creator # regardless of whether it is live or not self.assertFalse( creator_middleware.is_authorized_to_access_game("WS30-8FA3"), "ERROR! Can delete location of " "a game created by another " "creator") def test_cannot_delete_invalid_location_of_game_of_yours(self): creator_middleware = GameCreatorMiddleware(self.david.username) # must not be able to access the game # the code won't go past this point so we know that he cannot delete the game of another creator # whether it is live or not self.assertFalse( creator_middleware.is_authorized_to_access_game("WS30-8FA3")) # delete invalid location self.assertFalse( self.maps.delete_location("9XMQ-FXYJ", "INVALID CODE"), "ERROR! Can delete invalid location " "of a game of yours") def test_cannot_delete_invalid_location_of_game_of_yours_two(self): creator_middleware = GameCreatorMiddleware(self.david.username) # must not be able to access the game # the code won't go past this point so we know that he cannot delete the game of another creator # whether it is live or not self.assertFalse( creator_middleware.is_authorized_to_access_game("WS30-8FA3")) # delete location of another game in this game self.assertFalse( self.maps.delete_location("9XMQ-FXYJ", "684V-7K25"), "ERROR! Can delete a location of " "another game which is not in your game")
class LocationListView(LoginRequiredMixin, generic.TemplateView): template_name = 'locations.html' login_url = '/start' locations = None player = None creator = None form = ChangeClueForm def get(self, request, game_code, location_code, *args, **kwargs): self.locations = GameCreatorMiddleware(request.user.username) self.game = _GameMiddleware(game_code) self.maps = MapsMiddleware() this_location = self.locations.get_location_by_code(location_code) this_location_copy = self.locations.get_location_by_code(location_code) for x in this_location: location_name = str(x) if not self.locations.is_authorized_to_access_game(game_code): return handler(request, 404) latitude, longitude = self.maps.get_coordinate(location_name) latitude = float(latitude) longitude = float(longitude) return render(request, self.template_name, context={ 'locations_code': this_location, 'game_details': self.game.get_code_and_name(), 'game_player_name': self.locations.get_name(), 'game_player_username': self.locations.get_username(), 'lat_long': [latitude, longitude], 'game_code': self.game.game.code, 'location_code': this_location_copy.first() }) # Handling of the various post request that can be made on the locations page def post(self, request, game_code, location_code, *args, **kwargs): self.locations = GameCreatorMiddleware(request.user.username) self.game = _GameMiddleware(game_code) self.maps = MapsMiddleware() if not self.locations.is_authorized_to_access_game( request.POST['game_code']): return handler(request, 404) if 'delete_location_code' in request.POST.keys( ) and self.locations.can_change_game(request.POST['game_code']): return self._delete_location( request, game_code, request.POST['delete_location_code'], self.locations.get_location_by_code(location_code)) if 'code' in request.POST.keys(): return self._change_clue(request, request.POST['game_code'], request.POST['code'], *args, **kwargs) return handler(request, 404) # Delete a location from a game # Paramaters: # game_code: the game code of game to be modified # location_code: the location code of the location to be deleted def _delete_location(self, request, game_code, location_code, *args, **kwargs): self.maps = MapsMiddleware() self.maps.delete_location(game_code, location_code) return HttpResponseRedirect('/game/create/' + game_code) # Modify the clue of a location # Paramaters: # game_code: the game code of game to be modified # location_code: the location code of the location to be modified # clues: the new clue for the location def _change_clue(self, request, game_code, location_code, *args, **kwargs): self.creator = GameCreatorMiddleware(None) self.creator.user = request.user form = self.form(instance=self.creator.get_location_of_game( game_code=game_code, location_code=location_code), data=request.POST) if form.is_valid(): location = form.save(commit=False) location.clues = request.POST['clues'] location.save() return HttpResponseRedirect('/game/create/' + game_code) return handler(request, 404)
def setUp(self): super(DeleteLocationTest, self).setUp() self.maps = MapsMiddleware()
class AddLocationTest(DatabaseRequiredTests): def setUp(self): super(AddLocationTest, self).setUp() self.maps = MapsMiddleware() def test_location_saves_correctly_in_database(self): # Jacinda Arden - Australia game_code = "664P-RLCG" self.maps.create_game_location(game_code=game_code, area_name="Rotorua", city='Rotorua', country='New Zealand') game = Game.objects.get(code=game_code) location_exists = Location.objects.filter(game=game, name="Rotorua").exists() self.assertTrue(location_exists, "Location failed to save") def test_location_should_not_save_in_database(self): game_code = "AAAA-AAAA" result = None try: result = self.maps.create_game_location(game_code=game_code, area_name="Rotorua", city='Rotorua', country='New Zealand') except: # should be an exception in finding the game self.assertTrue(True) return self.assertFalse( result is not None, "ERROR! The game locations must not be saved for an incorrect game" ) def test_location_order_is_correct(self): # Jacinda Arden - Australia game_code = "664P-RLCG" new_location = self.maps.create_game_location(game_code=game_code, area_name="Rotorua", city='Rotorua', country='New Zealand') game = Game.objects.get(code=game_code) len_locations = len(Location.objects.filter(game=game)) self.assertEquals(len_locations, new_location.order, "Location order is not correct") def test_should_not_add_location_to_game_not_owned_by_user(self): game_creator = GameCreatorMiddleware(self.david.username) # Calvin's game calvins_game_code = "4Y1H-M4NX" self.assertFalse( game_creator.is_authorized_to_access_game(calvins_game_code), "ERROR! Can add locations to a " "game not owned by a user!") def test_cannot_add_location_to_an_archived_game(self): game_code = "9XMQ-FXYJ" game_creator_middleware = GameCreatorMiddleware(self.david.username) game_creator_middleware.stop_game(game_code) # if can_change_game(code) is false, no changes are allowed to the game, even adding location is not allowed self.assertFalse( game_creator_middleware.can_change_game(game_code), "ERROR! Can add location to a game even " "after it is archived") def test_cannot_add_location_to_a_live_game(self): # David's game live_game_code = "9XMQ-FXYJ" game_creator_middleware = GameCreatorMiddleware(self.david.username) game_creator_middleware.stop_game(live_game_code) # if can_change_game(code) is false, no changes are allowed to the game, even adding locations are not allowed self.assertFalse( game_creator_middleware.can_change_game(live_game_code), "ERROR! Can add location to a game even " "after it is live") def test_can_add_location_to_published_game_of_yours(self): unpublished_game_code = "13T2-JFRN" game_creator_middleware = GameCreatorMiddleware(self.david.username) # if can_change_game(code) is True, changes are allowed to the game, including adding locations self.assertTrue( game_creator_middleware.can_change_game(unpublished_game_code), "ERROR! Cannot add location to a game even " "if its not published yet")