Esempio n. 1
0
    def post(self):
        itemName = self.request.get("itemName")
        categoryName = self.request.get("categoryName")
        userEmail = self.request.get("userEmail")
        comment = self.request.get("comment")
        owner = self.request.get("owner")
        
        comment = str(comment).strip()
        message = ""
        if comment:
            ifAlreadyExists = []
            
            #self.response.out.write("Key is "+ str(Helper.getItemKey(userEmail, categoryName, itemName)))
            ifAlreadyExists = ItemComment.gql("WHERE ANCESTOR IS :1",Helper.getItemKey(userEmail, categoryName, itemName))
            
            #self.response.out.write("Key is "+ str(Helper.getItemKey(userEmail, categoryName, itemName)))
            #self.response.out.write("Count is "+ str(ifAlreadyExists.count()))
    
            
                    
            if (ifAlreadyExists.count() == 0):
                #self.response.out.write("Adding comment for " + itemName + " " + categoryName + " " + userEmail)
                itemComment = ItemComment(parent=Helper.getItemKey(userEmail, categoryName, itemName))
                itemComment.comment = comment
                itemComment.commenter = userEmail
                itemComment.item = itemName
                itemComment.category = categoryName
                itemComment.put()
                message="Comment Saved"
            else:
                message="You can comment only once on an item"

        else:
            #self.response.out.write("empty comment")
            message = "Cannot enter empty comment"
            
            
        items = Item.gql("WHERE ANCESTOR IS :1",Helper.getCategoryKey(owner, categoryName))
        template_values = {
                           'items' : items,
                           'owner': owner,
                           'user' : users.get_current_user(),
                           'category' : categoryName,
                           'logoutURL' : users.create_logout_url('./'),
                           'message' : message
                  }
        path = os.path.join(os.path.dirname(__file__), './html/items.html')
        self.response.out.write(template.render(path, template_values))
Esempio n. 2
0
 def post(self):
     searchItem = self.request.get("searchBox")
     matchedCategories = []
     matchedItems = []
     searchFound=False
     if searchItem:
         #find matching categories
         cats = Category.all()
         
         
         
         for cat in cats:
             searchresult = re.search(str(searchItem).lower(),str(cat.name).lower())
             if searchresult is not None:
                 matchedCategories.append(cat)
                 searchFound = True
                 
         #find matching items
         items = Item.all()
         
         
         
         for item in items:
             searchresult = re.search(str(searchItem).lower(),str(item.name).lower())
             if searchresult is not None:
                 matchedItems.append(item)
                 searchFound = True
                 
     template_values = {
             'matchedCategories': matchedCategories,
             'matchedItems': matchedItems,
             'logoutURL' : users.create_logout_url('./'),
             'searchItem':searchItem,
             'searchFound':searchFound
                     }
     path = os.path.join(os.path.dirname(__file__), './html/search.html')
     self.response.out.write(template.render(path, template_values))
     
     #below query looks from abc at beginning of string
     #db.GqlQuery("SELECT * FROM MyModel WHERE prop >= :1 AND prop < :2", "abc", u"abc" + u"\ufffd")
     
     
     
     
Esempio n. 3
0
 def post(self):
     user = users.get_current_user()
     message=""
     if user:
         category = self.request.get("category")
         isExport = self.request.get("isExport")
         owner = self.request.get("owner")
         
         if isExport:
             self.exportToXml(owner,category)
         else:
             newItemName = self.request.get("item_name").strip()
             ifAlreadyExists  = Item.gql("WHERE name = :1 AND ANCESTOR IS :2",newItemName,Helper.getCategoryKey(user.email(), category))
             
             if (ifAlreadyExists.count() == 0) and newItemName:
                 item = Item(parent=Helper.getCategoryKey(user.email(), category))
                 item.name = self.request.get("item_name")
                 item.wins = 0
                 item.loses = 0
                 item.put()
             else:
                 if newItemName:
                     message = "Item already exists"
                 else:
                     message = "Item name cannot be empty or spaces"
             
             items = db.GqlQuery("SELECT * FROM Item WHERE ANCESTOR IS :1",Helper.getCategoryKey(user.email(), category))
             
             template_values = {
                                'items' : items,
                                'category' : category,
                                'message' : message,
                                'logoutURL' : users.create_logout_url('./'),
                                'user':user,
                                'owner' : owner
                                }
             
             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))
Esempio n. 4
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))
Esempio n. 5
0
    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('./'))
            
Esempio n. 6
0
    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))