Beispiel #1
0
    def post(self):
        user = users.get_current_user()
        
        if user == None:
            self.redirect("/")
            return 
        
        # get category information
        category = self.request.get('newCategory') 
        if category != "":
            # check if exists
            c = Category.all()
            c.filter('user ='******'category =', category) # check
            if c.get() == None:# does not exist
                newCat = Category() # post a new category
                newCat.category = category
                newCat.user = user.email()
                newCat.username = user.nickname()
                newCat.put() # save 
            else:
                errorMessage = "Category %s already exists!" % category
                self.redirect("/category?error=%s" % errorMessage)
                return 

        self.redirect("/category")
Beispiel #2
0
    def post(self):  
        user = users.get_current_user()
        
        if user == None:
            self.redirect("/")
            return 

        content = self.request.get('content')
        if content == None or content == "":
            self.redirect("/category")
            return 
        xmldoc = minidom.parseString(content)
        category = xmldoc.getElementsByTagName('CATEGORY')[0]
        categoryName = category.getElementsByTagName('NAME')[0].childNodes[0].nodeValue
        itemList = category.getElementsByTagName('ITEM')
        
        cats = Category.all()
        cats.filter("category =", str(categoryName))
        c = cats.get()
        
        if c == None: # category not exist
            newCat = Category() # create a new category
            newCat.category = categoryName
            newCat.user = user.email()
            newCat.username = user.nickname()
            newCat.put() # save
            for item in itemList:
                itemName = item.getElementsByTagName('NAME')[0].childNodes[0].nodeValue
                newItem = Item(item = itemName, win = 0, lose = 0, rate = 0, parent = newCat.key())
                newItem.put()
            importMessage = 'Import new category "%s" successfully.' % categoryName
            self.redirect("/category?importMessage=%s" % importMessage)
        else: # category exists
            items = Item.all()
            items.ancestor(c.key())
            
            oldItemNameList = []
            for item in items.run():
                oldItemNameList.append(item.item)
            
            newItemNameList = []
            for item in itemList:
                itemName = item.getElementsByTagName('NAME')[0].childNodes[0].nodeValue
                newItemNameList.append(itemName)
                if itemName not in oldItemNameList: # new item not exist
                    newItem = Item(item = itemName, win = 0, lose = 0, rate = 0, parent = c.key())
                    newItem.put()
            
            for item in items: # get old items
                if item.item not in newItemNameList: # old item not in new list, delete
                    item.delete()
                    
            importMessage = 'Import existed category "%s" successfully.' % categoryName
            self.redirect("/category?importMessage=%s" % importMessage)