def register(cls, chat_id, first_name, last_name, username, is_bot=False, save=True): """ register a new user on the db :param chat_id: user's chat id :param first_name: user's first name :param last_name: user's last name :param username: user's username :param is_bot: whether the user is a telegram bot :param save: if True, will save to db :return: User """ user = cls() user.chat_id = chat_id user.first_name = first_name user.last_name = last_name user.username = username user.is_bot = is_bot user.public_id = generate_id(length=6) if save: user.save() return user
def option_key_id(): """ Return a Key ID that doesn't already exist. """ key_id = utilities.generate_id() # See if this key already exists... key_found = ndb.Key(Option, key_id).get() if key_found is not None: # Try again! return option_key_id() return key_id
def search(self, text, my_user): anagram_id = my_user.key.id() + '/' + utilities.generate_id(text) anagram = ndb.Key(Anagram, anagram_id).get() if anagram: result = anagram.words result.remove(text) return result else: return None
def add(self, text, my_user): logging.debug('Add ' + text) if text is not None or text != '': anagram_id = utilities.generate_id(text) anagram_key = ndb.Key(Anagram, anagram_id) anagrams = anagram_key.get() if anagrams: utilities.add_to_anagram(my_user, text, anagram_key) else: utilities.add_new_anagram(my_user, text, anagram_id, anagram_key)
def add(self, text, my_user): logging.debug('Add ' + text) if text is not None or text != '': # Add anagram to datastore anagram_id = my_user.key.id() + '/' + utilities.generate_id(text) anagram_key = ndb.Key(Anagram, anagram_id) anagrams = anagram_key.get() if anagrams: # an anagram with this key already exists utilities.add_to_anagram(text, anagram_key) else: # this key doesnt exist --> write new anagram object to datastore utilities.add_new_anagram(my_user, text, anagram_id, anagram_key)
def get(self): logging.debug("GET") self.response.headers['Content-Type'] = 'text/html' if utilities.user_is_logged_in(): if not utilities.user_exists(): utilities.add_new_user(utilities.get_user()) renderer.render_searchtext( self, utilities.get_logout_url(self), utilities.generate_id(self.request.get('value')), utilities.prepare_text_input(self.request.get('value')), utilities.get_anagrams_of_user(utilities.get_my_user())) else: renderer.render_login(self, utilities.get_login_url(self))
def get(self): logging.debug("GET") self.response.headers['Content-Type'] = 'text/html' if utilities.user_is_logged_in(): # if myuser object is None --> No user with key found --> new user --> make new user in datastore if not utilities.user_exists(): utilities.add_new_user(utilities.get_user()) renderer.render_searchtext( self, utilities.get_logout_url(self), utilities.generate_id(self.request.get('value')), utilities.prepare_text_input(self.request.get('value')), utilities.get_anagrams_of_user(utilities.get_my_user())) # if no user is logged in create login url else: renderer.render_login(self, utilities.get_login_url(self))
def post(self): self.response.headers['Content-Type'] = 'text/html' my_user = utilities.get_my_user() button = self.request.get('button') input_text = utilities.prepare_text_input(self.request.get('value')) file = self.request.get('uploadFile') if button == 'Upload': openFile = open(file) readLine = openFile.readline() while readLine: word = (readLine.strip('\n\r')).lower() permutations = utilities.all_permutations(word) wordsinfo = utilities.filter_english_words(permutations) anagram_id = my_user.key.id() + '/' + utilities.generate_id( word) anagram_key = ndb.Key(Anagram, anagram_id) anagrams = anagram_key.get() if anagrams: utilities.add_to_anagram(word, wordsinfo, anagram_key) else: utilities.add_new_anagram(my_user, word, wordsinfo, anagram_id, anagram_key) readLine = openFile.readline() openFile.close() self.redirect('/') if button == 'Search': search_result = self.search(input_text, my_user) renderer.render_search(self, input_text, search_result) elif button == 'Generate': words = self.generate(input_text, my_user) renderer.render_search(self, input_text, words)
def incoming_message(): """ Respond to an incoming message. """ response_object = {} response_object['id'] = utilities.generate_id() return json.dumps(response_object)
def incoming_message(): """ Respond to an incoming message. """ response_object = {} response_object['id'] = utilities.generate_id() return json.dumps(response_object)