From: Nick Retallack Date: Mon, 5 Oct 2009 17:44:08 +0000 (-0700) Subject: merged with mine X-Git-Url: http://git.veekun.com/zzz-floof.git/commitdiff_plain/d4609fff2bfd910366c41116f1da35e4037da7d4?hp=bc1cccee5faefbcfe6e28ce8f49694cca47b85cb merged with mine --- diff --git a/floof/config/routing.py b/floof/config/routing.py index 5bd87d9..c8b94b9 100644 --- a/floof/config/routing.py +++ b/floof/config/routing.py @@ -43,4 +43,9 @@ def make_map(): map.connect('/search', controller='search', action='index') + # default routing is back so we can test stuff. + # please don't take it away until we have some more core features in. + map.connect('/{controller}/{action}') + map.connect('/{controller}/{action}/{id}') + return map diff --git a/floof/controllers/art.py b/floof/controllers/art.py index 8d40321..7378f7a 100644 --- a/floof/controllers/art.py +++ b/floof/controllers/art.py @@ -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) diff --git a/floof/controllers/search.py b/floof/controllers/search.py index 88dfbe3..abedd16 100644 --- a/floof/controllers/search.py +++ b/floof/controllers/search.py @@ -16,13 +16,13 @@ class SearchController(BaseController): """Search, implemented the stupid way!""" query = request.params.get('query', '') tags = query.split() - + tagtexts = TagText.query.filter(TagText.text.in_(tags)) tagtext_ids = [_.id for _ in tagtexts] # Fetch art that has all the tags c.artwork = Art.query.join(Tag) \ - .filter(Tag.id.in_(tagtext_ids)) \ + .filter(Tag.tagtext_id.in_(tagtext_ids)) \ .all() - return render('/index.mako') + return render('/index.mako') \ No newline at end of file diff --git a/floof/lib/dbhelpers.py b/floof/lib/dbhelpers.py index 4d10030..0da968b 100644 --- a/floof/lib/dbhelpers.py +++ b/floof/lib/dbhelpers.py @@ -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 diff --git a/floof/model/art.py b/floof/model/art.py index 58601a7..6fd2036 100644 --- a/floof/model/art.py +++ b/floof/model/art.py @@ -11,8 +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): title = Field(Unicode(120)) @@ -57,13 +56,22 @@ class Art(Entity): raise "Long Tag!" # can we handle this more gracefully? # sqlite seems happy to store strings much longer than the supplied limit... - - # elixir should really have its own find_or_create. tagtext = find_or_create(TagText, text=text) 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 +79,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 +102,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 diff --git a/floof/public/layout.css b/floof/public/layout.css index f6e671f..74a3c05 100644 --- a/floof/public/layout.css +++ b/floof/public/layout.css @@ -10,8 +10,6 @@ body { font-family: sans-serif; font-size: 12px; } .full {display:block;} - - /*** Common bits and pieces ***/ /* General form layout */ dl.form { margin: 1em 0; padding-left: 1em; border-left: 0.5em solid gray; } @@ -21,3 +19,4 @@ dl.form dd { margin-bottom: 0.5em; } /*** Individual page layout ***/ +.selected {color:red;} diff --git a/floof/templates/art/show.mako b/floof/templates/art/show.mako index 02f61d4..2ac5305 100644 --- a/floof/templates/art/show.mako +++ b/floof/templates/art/show.mako @@ -1,5 +1,7 @@ <%inherit file="/base.mako" /> +<%! from floof.model.art import Rating %> +

View Art

${h.form (h.url_for (controller='art', action='tag', id=c.art.id), multipart=True)} @@ -12,6 +14,14 @@ ${h.end_form()} ${tag} % endfor +What do you think? +% for score,text in sorted(Rating.options.items()): +${text} +% endfor