def add_word(self, request): """Add word to list of words Args: The WordForm objects which include a 'word' Returns: StringMessage: confirming the 'word' has been added as an entity to the Word model Raises: endpoints.BadRequestException: if the word is not a single word or contains special characters and numbers. """ if Word.query(Word.word == request.word).get(): raise endpoints.ConflictException('That word is in the list!') else: word_list = [] temp = request.word.upper() for i in temp: if i == " " or i < 'A' or i > 'Z': raise endpoints.BadRequestException( 'Please Enter One Word!') else: word_list.append(i) w = Word(word=request.word, word_list=word_list) w.put() return StringMessage(message='Added %s to the list!' % request.word)
def test_insert_entity(self): Word(englishWord='Hi', imagePath='hello.jpg', languageName='Italian', translatedWord='Ciao', difficulty=1).put() self.assertEqual(1, len(Word.query().fetch(2)))
def get(self): word_index_list = WordList.query().order(WordList.order).fetch() word_list = Word.query().fetch() return render_template( 'admin/form.html', **{ 'word_index_list': word_index_list, 'word_list': word_list })
def get(self, key): key = ndb.Key(WordList, int(key)) word_list = Word.query().filter(Word.list == key).fetch() resp_data = [] for lst in word_list: resp_data.append(lst.to_json()) return Response(json.dumps(resp_data), mimetype='application/json')
def new_game(self, request): """Creates new game""" user = User.query(User.name == request.user_name).get() if not user: raise endpoints.NotFoundException( 'A User with that name does not exist!') word = Word.query(Word.word_to_guess == request.word).get() if not word: word = Word(word_to_guess=request.word) word.put() try: game = Game.new_game(user.key, word.key.urlsafe()) return game.to_form('Good luck playing Hangman!') except: raise
def post(self): if request.form.get('add_index', None): list_title = request.form['list_name'] order = (WordList.query().count() + 1) * 10 lst = WordList(order=order, title=str(list_title)) lst.put() elif request.form.get('add_word', None): key = ndb.Key(WordList, int(request.form['index_key'])) word_name = str(request.form['word_name']) words_count = Word.query().filter(Word.list == key).count() + 1 w = Word(list=key, word=word_name, order=words_count * 2) w.put() return self.get()
def test_filter_by_language(self): #Add two words, one French and one Italian Word(englishWord='Hi', imagePath='hello.jpg', languageName='Italian', translatedWord='Ciao', difficulty=1).put() Word(englishWord='Hi', imagePath='hello.jpg', languageName='French', translatedWord='Salut', difficulty=1).put() #Get french words query = Word.query(Word.languageName == 'French') results = query.fetch() #Check to see if only 1 result was returned self.assertEqual(1, len(results)) #Check if the word is French language self.assertEqual('French', results[0].languageName)
def __init__(self): # initialize word bank key = Word.query().get(keys_only=True) if key is None: # import word bank from file Word.import_words()