d598292d063627c1f0551ef38b485fb9d751e53d
[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 new(self):
25 """ New Art! """
26 return render("/art/new.mako")
27
28 # TODO: login required
29 def create(self):
30 c.art = Art(uploader=c.user, **request.params)
31 c.art.discussion = Discussion(count=0)
32
33 try:
34 elixir.session.commit()
35 redirect(url('show_art', id=c.art.id))
36 except IntegrityError:
37 # hurr, there must be a better way to do this but I am lazy right now
38 hash = c.art.hash
39 elixir.session.rollback()
40 duplicate_art = Art.get_by(hash=hash)
41 h.flash("We already have that one.")
42 redirect(url('show_art', id=duplicate_art.id))
43
44
45 def show(self, id):
46 # c.art = h.get_object_or_404(Art, id=id)
47 if c.user:
48 c.your_score = c.art.user_score(c.user)
49 return render("/art/show.mako")
50
51
52 # TODO: login required
53 def rate(self, id):
54 # c.art = h.get_object_or_404(Art, id=id)
55 score = request.params.get("score")
56 if score and score.isnumeric():
57 score = int(score)
58 else:
59 score = Rating.reverse_options.get(score)
60
61 c.art.rate(score, c.user)
62 elixir.session.commit()
63
64 redirect(url('show_art', id=c.art.id))