Exemple #1
0
    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 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))