Added parent property/links for comments, and a Return link on comment threads.
[zzz-floof.git] / floof / controllers / art.py
1 import logging
2
3 from pylons import request, response, session, tmpl_context as c, h
4 from pylons.controllers.util import abort, redirect
5 from pylons import url
6 from floof.lib.base import BaseController, render
7
8 log = logging.getLogger(__name__)
9
10 import elixir
11 from floof.model.art import Art, Rating
12 from floof.model.comments import Discussion
13
14 from sqlalchemy.exceptions import IntegrityError
15
16
17 class ArtController(BaseController):
18 def __before__(self, id=None):
19 super(ArtController, self).__before__()
20 # Awesome refactoring!
21 if id:
22 c.art = h.get_object_or_404(Art, id=id)
23
24 # def index():
25 # c.artwork = Art.query.order_by(Art.id.desc()).all()
26 # return render
27
28 def new(self):
29 """ New Art! """
30 return render("/art/new.mako")
31
32 # TODO: login required
33 def create(self):
34 c.art = Art(uploader=c.user, **request.params)
35 c.art.discussion = Discussion(count=0)
36
37 try:
38 elixir.session.commit()
39 redirect(url('show_art', id=c.art.id))
40 except IntegrityError:
41 # hurr, there must be a better way to do this but I am lazy right now
42 hash = c.art.hash
43 elixir.session.rollback()
44 duplicate_art = Art.get_by(hash=hash)
45 h.flash("We already have that one.")
46 redirect(url('show_art', id=duplicate_art.id))
47
48
49 def show(self, id):
50 # c.art = h.get_object_or_404(Art, id=id)
51 if c.user:
52 c.your_score = c.art.user_score(c.user)
53 return render("/art/show.mako")
54
55
56 # TODO: login required
57 def rate(self, id):
58 # c.art = h.get_object_or_404(Art, id=id)
59 score = request.params.get("score")
60 if score and score.isnumeric():
61 score = int(score)
62 else:
63 score = Rating.reverse_options.get(score)
64
65 c.art.rate(score, c.user)
66 elixir.session.commit()
67
68 redirect(url('show_art', id=c.art.id))