saved searches is going awesome. Partial work on adding them as galleries
[zzz-floof.git] / floof / controllers / search.py
index 5240a0b..3b6faef 100644 (file)
@@ -1,6 +1,6 @@
 import logging
 
-from pylons import request, response, session, tmpl_context as c
+from pylons import request, response, session, tmpl_context as c, h
 from pylons.controllers.util import abort, redirect_to
 
 from floof.lib.base import BaseController, render
@@ -8,48 +8,48 @@ from floof.lib.base import BaseController, render
 log = logging.getLogger(__name__)
 
 from floof.model.art import Art, Tag, TagText
+from floof.model.search import SavedSearch
 import elixir
 
 class SearchController(BaseController):
 
     def index(self):
-        # Return a rendered template
-        #return render('/search.mako')
-        # or, return a response
-        return 'Hello World'
-
-    def results(self):
-        """ Search, implemented the stupid way! """
-        query = request.params.get('query','')
-        words = query.split()
+        if request.params.get('button') == 'Save':
+            return self.save()
         
-        tags = []
-        for word in words:
-            components = word.split(':')
-            if len(components) == 1:
-                # tags are plain.
-                tags.append(word)
-            elif components[0] == "rating":
-                if components[1].isnumeric():
-                    score = int(components[1])
-                else:
-                    score = Rating.reverse_options.get(components[1])
-                
-                if -1 <= score <= 3:
-                    pass
-                    # TODO: Find stuff that has this rating
-                    # Rating.query.filter(Rating.s)
-                
-                
+        c.query = request.params.get('query', '')
+        tags = c.query.split()
         
-
         tagtexts = TagText.query.filter(TagText.text.in_(tags))
-        tagtext_ids = map(lambda x:x.id, tagtexts)
+        tagtext_ids = [_.id for _ in tagtexts]
+
+        # Fetch art that has all the tags
+        c.artwork = Art.query.join(Tag) \
+                       .filter(Tag.tagtext_id.in_(tagtext_ids)) \
+                       .all()
 
-        # TODO: this is wrong.  Please fix it so it returns art that has all the tags.
-        art_tag_pairs = elixir.session.query(Art,Tag).filter(Art.id == Tag.art_id).\
-            filter(Tag.tagtext_id.in_(tagtext_ids)).all()
+        return render('/index.mako')
+        
+    # TODO: login required
+    def save(self):
+        c.query = request.params.get('query', '')
+        saved_search = SavedSearch(author=c.user, string=c.query)
+        elixir.session.commit()
+        redirect_to(action="list")
+        # TODO: do something better than this.
+        
+    
+    # TODO: login required
+    def list(self):
+        c.searches = c.user.searches
+        return render('/searches.mako')
+    
+    # TODO: login required
+    def display(self, id):
+        c.search = h.get_object_or_404(SavedSearch, id=id)
+        # TODO: create a gallery widget
+        
+        redirect_to(controller="users", action="view", name=c.user.name)
+        
         
-        # just the art please.
-        c.artwork = map(lambda x: x[0], art_tag_pairs)
-        return render('/index.mako')
\ No newline at end of file
+        
\ No newline at end of file