import giveaminute.idea as mIdea
import giveaminute.keywords as mKeywords
import giveaminute.project as mProject
import framework.util as util
from framework.controller import *

class Idea(Controller):
    def GET(self, action = None, id = None):
        if (action == 'related'):
            return self.getRelatedProjects()
        else:
            return self.not_found()
            
            
    def POST(self, action=None):
        if (action == 'flag'):
            return self.flagIdea()
        elif (action == 'remove'):
            log.info("*** remove POST")
            return self.removeIdea()
        else:
            return self.newIdea()
        
    def newIdea(self):
        description = self.request('text')
        locationId = util.try_f(int, self.request('location_id'), -1)

        if (self.user):
            userId = self.user.id
            email = self.user.email
        else:
            userId = None
            email = self.request('email')
        
        ideaId = mIdea.createIdea(self.db, description, locationId, 'web', userId, email)
        
        return ideaId if ideaId else False 
        
    def flagIdea(self):
        ideaId = self.request('idea_id')
        
        if (ideaId):
            return mIdea.flagIdea(self.db, ideaId)
        else:
            return False
            
    def removeIdea(self):
        if (not self.user.isAdmin and not self.user.isModerator):
            log.warning("*** unauthorized idea removal attempt by user_id = %s" % self.user.id)
            return False
    
        ideaId = self.request('idea_id')
        
        if (ideaId):
            return mIdea.setIdeaIsActive(self.db, ideaId, 0)
        else:
            return False
        
        
    def getRelatedProjects(self):
        ideaId = self.request('idea_id')
        limit = int(self.request('n_limit')) if self.request('n_limit') else 5
        relatedProjects = []
        citywideProjects = []
        kw = []
        isLocationOnlyMatch = False
        
        if (not ideaId):
            log.error("No idea id")
        else: 
            idea = mIdea.Idea(self.db, ideaId)
        
            if (idea):
                kw = mKeywords.getKeywords(self.db, idea.description)
                
                if (idea.locationId != -1):
                    relatedProjects = mProject.searchProjects(self.db, kw, idea.locationId, limit)
                    
                    if (len(relatedProjects) == 0):
                        isLocationOnlyMatch = True
                        relatedProjects = mProject.searchProjects(self.db, [], idea.locationId, limit)
                    
                citywideProjects = mProject.searchProjects(self.db, kw, -1, limit)
            else:
                log.error("No idea found for id = %s" % ideaId)
            
        obj = dict(is_location_only_match = isLocationOnlyMatch, related = relatedProjects, citywide = citywideProjects, search_terms = ','.join(kw))
            
        return self.json(obj)
        

    
@@ -534,48 +534,8 @@
 '):%0A
-            log.info(%22*** remove POST%22)%0A
     
@@ -1314,32 +1314,195 @@
 moveIdea(self):%0A
+        ideaId = self.request('idea_id')%0A        %0A        if (ideaId):%0A            idea = mIdea.Idea(self.db, ideaId)%0A        %0A            if (idea.data):%0A        
         if (not 
@@ -1523,16 +1523,37 @@
 min and 
+%0A                    
 not self
@@ -1569,19 +1569,89 @@
 oderator
-):%0A
+ and%0A                    not self.user.id == idea.data.user_id):%0A        
         
@@ -1742,32 +1742,40 @@
 id)%0A            
+        
 return False%0A   
@@ -1771,33 +1771,32 @@
 n False%0A    
-%0A
         
 ideaId = sel
@@ -1787,71 +1787,26 @@
     
-ideaId = self.request('idea_id')%0A        %0A        if (ideaId):%0A
+    else:%0A        
     
@@ -1862,38 +1862,199 @@
 aId, 0)%0A        
+    
 else:%0A
+                log.error(%22*** idea does not exist for idea id %25s%22 %25 ideaId)%0A        else:%0A            log.error(%22*** attempting to delete idea with no id%22)%0A
             retu

allow users to delete their own ideas