ratings work. Searching for ratings, not so much yet.
authorNick Retallack <nickretallack@gmil.com>
Sun, 4 Oct 2009 21:36:52 +0000 (14:36 -0700)
committerNick Retallack <nickretallack@gmil.com>
Sun, 4 Oct 2009 21:36:52 +0000 (14:36 -0700)
floof/controllers/art.py
floof/controllers/search.py
floof/lib/dbhelpers.py
floof/model/art.py
floof/public/layout.css
floof/templates/art/show.mako

index e988c7b..99027aa 100644 (file)
@@ -29,10 +29,18 @@ class ArtController(BaseController):
 
     def show(self, id):
         c.art = Art.get(id)
+        c.your_score = c.art.user_score(c.user)
         return render("/art/show.mako")
         
+    # should force logged in on these things
     def tag(self, id):
         art = Art.get(id)
         art.add_tags(request.params["tags"], c.user)
         elixir.session.commit()
-        redirect_to(action="show", id=art.id)
\ No newline at end of file
+        redirect_to(action="show", id=art.id)
+    
+    def rate(self, id):
+        art = Art.get(id)
+        art.rate(request.params["score"], c.user)
+        elixir.session.commit()
+        redirect_to(action="show", id=art.id)
index 75926e7..5240a0b 100644 (file)
@@ -21,7 +21,27 @@ class SearchController(BaseController):
     def results(self):
         """ Search, implemented the stupid way! """
         query = request.params.get('query','')
-        tags = query.split()
+        words = query.split()
+        
+        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)
+                
+                
+        
 
         tagtexts = TagText.query.filter(TagText.text.in_(tags))
         tagtext_ids = map(lambda x:x.id, tagtexts)
index 4d10030..0da968b 100644 (file)
@@ -2,4 +2,18 @@ def find_or_create(model, **kwargs):
     instance = model.get_by(**kwargs)
     if not instance:
         instance = model(**kwargs)
+    return instance
+    
+def update_or_create(model, get_by, update_with):
+    instance = model.get_by(**get_by)
+    if instance:
+        # set new values
+        for key,value in update_with.items():
+            setattr(instance, key, value)
+    else:
+        # create it
+        both = {}
+        both.update(get_by)
+        both.update(update_with)
+        instance = model(**both)
     return instance
\ No newline at end of file
index 2c631f7..7d93e6b 100644 (file)
@@ -11,7 +11,7 @@ import elixir
 from pylons import config
 
 from floof.lib.file_storage import get_path, save_file
-from floof.lib.dbhelpers import find_or_create
+from floof.lib.dbhelpers import find_or_create, update_or_create
 
 
 class Art(Entity):
@@ -64,6 +64,17 @@ class Art(Entity):
                 tag     = find_or_create(Tag, art=self, tagger=user, tagtext=tagtext)
 
 
+
+
+    def rate(self, score, user):
+        return update_or_create(Rating, {"rater":user, "art":self}, {"score":score})
+        
+    def user_score(self, user):
+        rating = Rating.get_by(rater=user, art=self)
+        if rating:
+            return rating.score
+        return Rating.default
+
     def __unicode__(self):
         return self.get_path()
 
@@ -71,7 +82,7 @@ class Art(Entity):
 class Tag(Entity):
     # look into how ondelete works.  This just sets a database property.
     art = ManyToOne('Art', ondelete='cascade')
-    tagger = ManyToOne('User')
+    tagger = ManyToOne('User', ondelete='cascade')
     tagtext = ManyToOne('TagText')
 
     # this text setter is no longer useful since I changed the way Art#add_tags works
@@ -94,4 +105,17 @@ class TagText(Entity):
     tags = OneToMany('Tag')
     
     def __unicode__(self):
-        return self.text
\ No newline at end of file
+        return self.text
+        
+
+class Rating(Entity):
+    art = ManyToOne('Art', ondelete='cascade')
+    rater = ManyToOne('User', ondelete='cascade')
+    score = Field(Integer)
+        
+    options = {-1:"sucks", 0:"undecided", 1:"good", 2:"great"}
+    default = 0
+    # options = ["sucks","neutral","good","great"]
+    
+
+Rating.reverse_options = dict (zip(Rating.options.values(), Rating.options.keys()))
\ No newline at end of file
index 0f65486..e32995e 100644 (file)
@@ -5,4 +5,5 @@
 
 #footer { padding: 1em; background: #c0c0c0; }
 
-.full {display:block;}
\ No newline at end of file
+.full {display:block;}
+.selected {color:red;}
\ No newline at end of file
index d77f364..f2320fd 100644 (file)
@@ -1,5 +1,7 @@
 <%inherit file="/base.mako" />
 
+<%! from floof.model.art import Rating %>
+
 <h1>View Art</h1>
 
 ${h.form (h.url_for (controller='art', action='tag', id=c.art.id), multipart=True)}
@@ -12,6 +14,14 @@ ${h.end_form()}
 <a href="${h.url_for (controller='search', action='results')}?query=${tag}">${tag}</a>
 % endfor
 
+What do you think?
+% for score,text in sorted(Rating.options.items()):
+<a href="${h.url_for(controller='art', action='rate', id=c.art.id)}?score=${score}" \
+% if c.your_score == score:
+class="selected" \
+% endif
+>${text}</a> 
+% endfor
 
 <img class="full" src="${c.art.get_path()}">