def test_clear_datastore(self): """Tests if the database is being cleared and considers non-empty lists of instances for all the models used by the application. """ self.assertNotEquals(Word.query().fetch(), []) self.assertNotEquals(Work.query().fetch(), []) self.assertNotEquals(Character.query().fetch(), []) self.assertNotEquals(list(FileMetadata.all().run()), []) self.testapp.get('/') self.assertEquals(Word.query().fetch(), []) self.assertEquals(Work.query().fetch(), []) self.assertEquals(Character.query().fetch(), []) self.assertEquals(list(FileMetadata.all().run()), [])
def get(self): """Clears the datastore.""" ndb.delete_multi(Word.query().fetch(keys_only=True)) ndb.delete_multi(Work.query().fetch(keys_only=True)) ndb.delete_multi(Character.query().fetch(keys_only=True)) ndb.delete_multi(Line.query().fetch(keys_only=True)) db.delete(FileMetadata.all(keys_only=True).run()) self.redirect('/admin')
def new_game(request): user = User.query(User.name == request.user_name).get() total_words = Word.query().count() rand_num = random.randint(1, total_words) word = Word.query(Word.word_id == rand_num).get() if not user: raise endpoints.NotFoundException( 'A User with that name does not exist!') try: game = Game.new_game(user.key, word.key) except AttributeError: raise endpoints.BadRequestException('Random word not found') # Use a task queue to update the average attempts remaining. # This operation is not needed to complete the creation of a new game # so it is performed out of sequence. taskqueue.add(url='/tasks/cache_average_attempts') return game
def make_move(request): game = get_by_urlsafe(request.urlsafe_game_key, Game) game.number_of_guess += 1 word = Word.query(Word.key == game.word).get() split_word = list(word.word) guess = request.guess game_history = History.query(History.game == game.key) letters_guessed_so_far = [] for history in game_history.iter(): letters_guessed_so_far.append(history.guess) if game.game_over is True: msg = 'Game already over!' elif guess == '' or guess.isdigit() or len(guess) > 1: msg = "Please enter a single alpha character only" elif guess in letters_guessed_so_far: msg = "You have already used that letter" elif guess not in split_word: msg = "letter isn't in word" game.guesses_remaining -= 1 save_history(game.key, game.number_of_guess, guess, False) else: msg = "Letter is in word!" save_history(game.key, game.number_of_guess, guess, True) # Added in a sleep because of latency writing to the datastore. # http://stackoverflow.com/questions/9137214/writing-then-reading-entity-does-not-fetch-entity-from-datastore time.sleep(0.1) count_of_success = History.query(History.status == True).filter(History.game == game.key).count() if len(word.word) == count_of_success: msg = "You've won! The word was {}".format(word.word) game.end_game(True) elif game.guesses_remaining == 0: msg = 'You have run out of guesses! The word was {}'.format(word.word) game.end_game() taskqueue.add(url='/tasks/cache_average_attempts') game.put() return game, msg
def get_words(self, request): """Return all words in the words to be found list""" return WordForms(words=[word.to_form() for word in Word.query()])