class LeaderboardView(LoginRequiredMixin, generic.TemplateView): template_name = 'Leaderboard.html' login_url = '/start' # Post Request Values: # code: game code for Leaderboard to be displayed def get(self, request, code, *args, **kwargs): # temp game self.game_creator = GameCreatorMiddleware(request.user.username) self.game_player = GamePlayerMiddleware(request.user.username) if not self.game_creator.is_authorized_to_access_game( code) and not self.game_player.is_authorized_to_access_game( code): return handler(request, 404) self.game = _GameMiddleware(code) return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'leaderboards': self.game_creator.get_leaderboard(code) })
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_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_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_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_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")
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")
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)
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)
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'])