def get(self): user = users.get_current_user() if user: key = self.request.get('key') if key: #do something key = db.Key(key) parentCategory = Category.gql("WHERE __key__ = :1",key) categoryUser = key.parent().name() items = db.GqlQuery("SELECT * FROM Item WHERE ANCESTOR IS :1 ORDER BY wins DESC",Helper.getCategoryKey(categoryUser, parentCategory[0].name)) # items = Item.gql("WHERE ANCESTOR IS :1",getCategoryKey(user.email(), category)) template_values = { 'items': items, 'logoutURL' : users.create_logout_url('./') } else: categories = db.GqlQuery("SELECT * FROM Category") template_values = { 'categories': categories, 'logoutURL' : users.create_logout_url('./') } path = os.path.join(os.path.dirname(__file__), './html/results.html') self.response.out.write(template.render(path, template_values)) else: self.redirect(users.create_login_url(self.request.uri))
def get(self): user = users.get_current_user() if user: category = self.request.get("category") owner = self.request.get("owner") newCategoryName = self.request.get("newCategoryName").strip() newItemName = self.request.get("newItemName").strip() oldItemName = self.request.get("oldItemName").strip() displayItems = True if newCategoryName: owner = user.email() if newCategoryName != "delete": ifAlreadyExists = Category.gql("WHERE name = :1 and ANCESTOR IS :2",newCategoryName,Helper.getUserKey(user.email())) if ifAlreadyExists.count() == 0: newCategory = Category(parent=Helper.getUserKey(user.email())) newCategory.name = newCategoryName newCategory.put() items = Item.gql("WHERE ANCESTOR IS :1", Helper.getCategoryKey(user.email(), category)) for item in items: newItem = Item(parent=Helper.getCategoryKey(user.email(), newCategory.name)) newItem.name = item.name newItem.wins = item.wins newItem.loses = item.loses newItem.put() Item.delete(item) categoryToEdit = Category.gql("WHERE name = :1 and ANCESTOR IS :2",category,Helper.getUserKey(user.email()))[0] Category.delete(categoryToEdit) message = category + " renamed to " + newCategoryName category = newCategoryName else: message = "Category already exists" displayItems = True else: categoryToDelete = Category.gql("WHERE name = :1 and ANCESTOR IS :2",category,Helper.getUserKey(user.email()))[0] items = Item.gql("WHERE ANCESTOR IS :1", Helper.getCategoryKey(user.email(), category)) for item in items: Item.delete(item) Category.delete(categoryToDelete) message = category + " deleted " #self.redirect("./categories", permanent=False) categories = db.GqlQuery("SELECT * " "FROM Category ") # "WHERE ANCESTOR IS :1", # Helper.getUserKey(user.email())) template_values = { 'categories' : categories, 'user':user, 'logoutURL' : users.create_logout_url('./'), 'message' : message } displayItems = False path = os.path.join(os.path.dirname(__file__), './html/category.html') self.response.out.write(template.render(path, template_values)) if newItemName: owner = user.email() if newItemName != "delete": ifAlreadyExists = Item.gql("WHERE name = :1 and ANCESTOR IS :2",newItemName, Helper.getCategoryKey(user.email(), category)) if ifAlreadyExists.count() == 0: itemToEdit = Item.gql("WHERE name = :1 and ANCESTOR IS :2",oldItemName, Helper.getCategoryKey(user.email(), category))[0] itemToEdit.name = newItemName itemToEdit.wins = 0 itemToEdit.loses = 0 itemToEdit.put() message = oldItemName + " renamed to " + newItemName else: message = "Item already exists" displayItems = True else: itemToDelete = Item.gql("WHERE name = :1 and ANCESTOR IS :2",oldItemName, Helper.getCategoryKey(user.email(), category))[0] Item.delete(itemToDelete) message = oldItemName + " deleted " displayItems = True if displayItems: items = db.GqlQuery("SELECT * FROM Item WHERE ANCESTOR IS :1",Helper.getCategoryKey(owner, category)) template_values = { 'items' : items, 'owner': owner, 'user' : user, 'category' : category, 'logoutURL' : users.create_logout_url('./'), 'ItemComment':ItemComment, 'Helper':Helper } path = os.path.join(os.path.dirname(__file__), './html/items.html') self.response.out.write(template.render(path, template_values)) else: self.redirect(users.create_login_url(self.request.uri))
def get(self): user = users.get_current_user() if user: #check for key, if not exist show all categories from all users key = self.request.get("key") wKey = self.request.get("wKey") lKey = self.request.get("lKey") if (key and ( not (wKey and lKey) ) ): #item_1 = Item.all().order('rand_num').filter('rand_num >=', rand_num).filter(k, value) .get() key = db.Key(key) parentCategory = Category.gql("WHERE __key__ = :1",key) categoryUser = key.parent().name() items = db.GqlQuery("SELECT * FROM Item WHERE ANCESTOR IS :1",Helper.getCategoryKey(categoryUser, parentCategory[0].name)) if( items.count() >= 2): item_1 = "Not Enough Items" item_2 = "Not Enough Items" randomNumber_1 = random.randint(0,items.count() - 1 ) randomNumber_2 = randomNumber_1 while(randomNumber_1 == randomNumber_2): randomNumber_2 = random.randint(0,items.count() - 1 ) item_1 = items[randomNumber_1] item_2 = items[randomNumber_2] message="Click on one of the Items to vote" template_values = { 'item_1': item_1, 'item_2': item_2, 'message':message, 'key':key, 'logoutURL' : users.create_logout_url('./') } else: message = "Not enough items in this category. Choose another category." template_values = { 'message':message, 'key':key, 'logoutURL' : users.create_logout_url('./') } path = os.path.join(os.path.dirname(__file__), './html/vote.html') self.response.out.write(template.render(path, template_values)) elif (key and ( (wKey and lKey) )): key = db.Key(key) wKey = db.Key(wKey) lKey = db.Key(lKey) winningItem = Item.gql("WHERE __key__ = :1",wKey)[0] losingItem = Item.gql("WHERE __key__ = :1",lKey)[0] winningItem.wins = winningItem.wins + 1 winningItem.put() losingItem.loses = losingItem.loses + 1 losingItem.put() #self.redirect("./vote?category=%s", permanent=False) message = winningItem.name + ' wins over ' + losingItem.name #get 2 new random items key = self.request.get("key") key = db.Key(key) parentCategory = Category.gql("WHERE __key__ = :1",key) categoryUser = key.parent().name() items = db.GqlQuery("SELECT * FROM Item WHERE ANCESTOR IS :1",Helper.getCategoryKey(categoryUser, parentCategory[0].name)) item_1 = "Not Enough Items" item_2 = "Not Enough Items" if( items.count() != 0): randomNumber_1 = random.randint(0,items.count() - 1 ) randomNumber_2 = randomNumber_1 while(randomNumber_1 == randomNumber_2): randomNumber_2 = random.randint(0,items.count() - 1 ) item_1 = items[randomNumber_1] item_2 = items[randomNumber_2] template_values = { 'item_1': item_1, 'item_2': item_2, 'key':key, 'logoutURL' : users.create_logout_url('./'), 'message' : message } path = os.path.join(os.path.dirname(__file__), './html/vote.html') self.response.out.write(template.render(path, template_values)) else: categories = Category.all() template_values = { 'categories': categories, 'logoutURL' : users.create_logout_url('./') } path = os.path.join(os.path.dirname(__file__), './html/vote.html') self.response.out.write(template.render(path, template_values)) else: self.redirect(users.create_login_url('./'))
def post(self): user = users.get_current_user() action = self.request.get("action") inputXML = self.request.get("chooseFile") message=" " if user: if (action == "import"): if inputXML: try: root = ET.fromstring(inputXML) #self.response.out.write("ROOT IS - "+ root.tag) if (root is not None) and (root.tag == "CATEGORY"): cat = root.find('NAME') if (cat is not None) and (cat.text): category = Category.gql("WHERE name = :1 and ANCESTOR IS :2", cat.text ,Helper.getUserKey(user.email())) if category.count() == 0: #self.response.out.write( "Cat doesnt exist. Creating new category and adding all new items") category = Category(parent=Helper.getUserKey(user.email())) category.name = cat.text category.put() for item in root.findall('ITEM'): if len(item.findall('NAME')) == 1 and (item.find('NAME').text.strip()): itemName = item.find('NAME').text.strip() newItem = Item(parent=Helper.getCategoryKey(user.email(), category.name)) newItem.name = itemName newItem.wins = 0 newItem.loses = 0 newItem.put() else: #self.response.out.write("Invalid XML more/less than one name for Item") message= "Invalid XML more/less than one name for Item" Category.delete(category) else: category = category[0] #self.response.out.write("<br/>Category already exists<br/>") #check for new items in XML and add them in datastore itemsInXml = [] itemsSaved = [] escape = False for item in root.findall('ITEM'): #check if there are more than one Item names if (len(item.findall('NAME')) == 1) and (item.find('NAME').text.strip()): itemName = item.find('NAME').text.strip() #self.response.out.write("Checking for Item "+itemName + "<br/>") newItem = Item.gql("WHERE name = :1 AND ANCESTOR IS :2",itemName,Helper.getCategoryKey(user.email(), category.name)) #self.response.out.write("Count " + str(newItem.count()) +"<br/>") if newItem.count() == 0: # self.response.out.write("Item "+ itemName+ " doesnot exist. Adding it<br/>") newItem = Item(parent=Helper.getCategoryKey(user.email(), category.name)) newItem.name = itemName newItem.wins = 0 newItem.loses = 0 newItem.put() itemsSaved.append(item) itemsInXml.append(itemName) else: # self.response.out.write("Invalid XML more/less than one name for Item") message = "Invalid XML more/less than one name for Item" for item in itemsSaved: Item.delete(item) escape = True #raise Exception("IGNORE") if not escape: #check for items in Datastore that are not in XML. Delete them #items = Item.all() items = Item.gql("WHERE ANCESTOR IS :1",Helper.getCategoryKey(user.email(), category.name)) for item in items: #self.response.out.write("Checking for "+ item.name + " in XML <br/>") try: itemsInXml.remove(item.name) # self.response.out.write(item.name + " is there in XML. Retain it <br/>") except ValueError: # self.response.out.write("Item "+ item.name+ " is not there in XML. Deleting<br/>") Item.delete(item) else: #self.response.out.write("Invalid XML no Cat name") message = "Invalid XML no Cat name" else: # self.response.out.write("Invalid XML") message = "Invalid XML" except: # self.response.out.write("Invalid File") message = "Invalid File" else: #self.response.out.write("Please choose file") message = "Please choose a file" categories = db.GqlQuery("SELECT * " "FROM Category ") #message = "invalid xml" template_values = { 'categories': categories, 'user':user, 'message':message, 'logoutURL' : users.create_logout_url('./') } path = os.path.join(os.path.dirname(__file__), './html/category.html') self.response.out.write(template.render(path, template_values)) #self.redirect('./categories', permanent=False) else: newCategoryName=self.request.get("category_name").strip() ifAlreadyExists = Category.gql("WHERE name = :1 AND ANCESTOR IS :2",newCategoryName,Helper.getUserKey(user.email())) if (ifAlreadyExists.count() == 0) and newCategoryName: category = Category(parent=Helper.getUserKey(user.email())) category.name = self.request.get("category_name") category.put() else: if newCategoryName: message = "Category already exists" else: message = "Category name cannot be empty" categories = db.GqlQuery("SELECT * " "FROM Category ") # "WHERE ANCESTOR IS :1", # Helper.getUserKey(user.email())) template_values = { 'categories': categories, 'user':user, 'message':message, 'logoutURL' : users.create_logout_url('./') } path = os.path.join(os.path.dirname(__file__), './html/category.html') self.response.out.write(template.render(path, template_values)) else: self.redirect(users.create_login_url(self.request.uri))