def leave(user_key, group_key): #Find the user to add the key to user = User.find_by_key(user_key) if (user == None): raise Exception("Invalid key") #Find the user to add the key to group = Group.find_by_key(group_key) if (group == None): raise Exception("Invalid group key") #Remove the user from the pending users group_usernames = JSON.unquote(group.usernames) group_usernames.remove(user.username) group.usernames = JSON.encode(group_usernames) group.increment_version() group.put() #Remove the list from the users lists user_groups = JSON.unquote(user.groups) user_groups.remove(group_key) user.groups = JSON.encode(user_groups) user.increment_version() user.put() return "Group left"
def accept(user_key, group_key): #Find the user to add the key to user = User.find_by_key(user_key) if (user == None): raise Exception("Invalid key") #Find the user to add the key to group = Group.find_by_key(group_key) if (group == None): raise Exception("Invalid group key") #Remove the user from the pending users group_pending_usernames = JSON.unquote(group.pending_usernames) group_pending_usernames.remove(user.username) group.pending_usernames = JSON.encode(group_pending_usernames) #Put the user in the confirmed users group_usernames = JSON.unquote(group.usernames) group_usernames.append(user.username) group.usernames = JSON.encode(group_usernames) group.increment_version() group.put() user.increment_version() return "Group request accepted"
def increment_version(self): self.version += 1 self.put() usernames = JSON.unquote(self.usernames) for username in usernames: user = User.find_by_username(username) user.increment_version() user.put() usernames = JSON.unquote(self.pending_usernames) for username in usernames: user = User.find_by_username(username) user.increment_version() user.put()
def test_unquote(self): self.assertEqual( json.unquote( "%22%3Cscript%3Ealert%28%5C%22hello%5C%22%29%3C%5C/script%3E%22" ), '<script>alert("hello")</script>', )
def post(self): user_key = self.request.get('user_key') versions = self.request.get('versions') try: #user_key = User.get_groups(user_key) # Respond with the user key self.response.set_status(200) self.response.write(User.get_groups(user_key, JSON.unquote(versions))) except Exception as e: # Respond with the error self.response.set_status(406) self.response.write(str(e))
def update_content(list_key, changed_content): """Adds and deletes items from the list (see readme for more info) Keyword arguments: user_key - the string key of the user list_key - the string key of the grocery list changed_content - the items to add and delete from the list """ #Find the grocery list item_list = ItemList.find_by_key(list_key) if (item_list == None): raise Exception("Invalid list key") #Get the contents of the current grocery list content = JSON.unquote(item_list.content) for item in changed_content: # Remove the hidden operation code from the item opcode = item[ItemList.UPDATE_CONTENT_OPCODE] item.pop(ItemList.UPDATE_CONTENT_OPCODE) # Add the item to the list if the op code was to add it if (opcode == ItemList.UPDATE_CONTENT_OPCODE_ADD): content.append(item) # Delete the item from the list if the op code was to delete it elif (opcode == ItemList.UPDATE_CONTENT_OPCODE_DELETE): for item2 in content: if ((ItemList.UPDATE_CONTENT_ID in item2) and (item2[ItemList.UPDATE_CONTENT_ID] == item[ItemList.UPDATE_CONTENT_ID])): content.remove(item2) break elif (opcode == ItemList.UPDATE_CONTENT_OPCODE_CHECKED): for item2 in content: if ((ItemList.UPDATE_CONTENT_ID in item2) and (item2[ItemList.UPDATE_CONTENT_ID] == item[ItemList.UPDATE_CONTENT_ID])): item2["checked"] = item["checked"] break #Increment the version number item_list.increment_version() # Put the new list content in the database item_list.content = JSON.encode(content) item_list.put() return (str(json.dumps(item_list.content)).replace("\\\"","\""))[1:-1]
def add_users(group_key, usernames): #Find the user to add the key to group = Group.find_by_key(group_key) if (group == None): raise Exception("Invalid group key") #Get the pending usernames and current usernames group_usernames = JSON.decode(group.usernames) group_pending_usernames = JSON.decode(group.pending_usernames) usernames = JSON.unquote(usernames) for username in usernames: #Find the user to add the key to user = User.find_by_username(username) if (user != None): if (user.username not in group_pending_usernames) and (user.username not in group_usernames): #Add the username to the list of pending users group_pending_usernames.append(user.username) #Add the group to the list of user groups user_groups = JSON.decode(user.groups) user_groups.append(group_key) user.groups = JSON.encode(user_groups) user.increment_version() user.put() group.pending_usernames = JSON.encode(group_pending_usernames) group.increment_version() group.put() #Return the currently pending users return str(json.dumps(group_pending_usernames)).replace("\\\"","\"")
def get_item_lists(self, versions): """Get the grocery lists that a user has access Keyword arguments: user_key - the key of the user versions - the json contaning the client versions of each list Returns: The grocery lists in a json """ response = [] item_lists = ItemList.query(ItemList.group_key==(self.put()).urlsafe()) for item_list in item_lists: list_key_str = (item_list.put()).urlsafe() if ((list_key_str not in versions) or (versions[list_key_str] < item_list.version)): response.append( { "list_key" : list_key_str, "list_name" : item_list.name, "list_version" : item_list.version, "list_contents" : JSON.unquote(item_list.content) }) else: response.append( { "list_key" : list_key_str, "list_name" : item_list.name, "list_version" : item_list.version, "list_contents" : "" }) response = JSON.encode(response) return response
def test_unquote(self): self.assertEqual(json.unquote('%22%3Cscript%3Ealert%28%5C%22hello%5C%22%29%3C%5C/script%3E%22'), '<script>alert("hello")</script>')