5186daa9abffe61cb2640db060ca6282e53d5986
[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
13 from sqlalchemy.exceptions import IntegrityError
14
15
16 class ArtController(BaseController):
17 def __before__(self, id=None):
18 super(ArtController, self).__before__()
19 # Awesome refactoring!
20 if id:
21 c.art = h.get_object_or_404(Art, id=id)
22
23 # def index():
24 # c.artwork = Art.query.order_by(Art.id.desc()).all()
25 # return render
26
27 def new(self):
28 """ New Art! """
29 return render("/art/new.mako")
30
31 # TODO: login required
32 def create(self):
33 c.art = Art(uploader=c.user, **request.params)
34
35 try:
36 elixir.session.commit()
37 redirect(url('show_art', id=c.art.id))
38 except IntegrityError:
39 # hurr, there must be a better way to do this but I am lazy right now
40 hash = c.art.hash
41 elixir.session.rollback()
42 duplicate_art = Art.get_by(hash=hash)
43 h.flash("We already have that one.")
44 redirect(url('show_art', id=duplicate_art.id))
45
46
47 def show(self, id):
48 # c.art = h.get_object_or_404(Art, id=id)
49 if c.user:
50 c.your_score = c.art.user_score(c.user)
51 return render("/art/show.mako")
52
53
54 # TODO: login required
55 def rate(self, id):
56 # c.art = h.get_object_or_404(Art, id=id)
57 score = request.params.get("score")
58 if score and score.isnumeric():
59 score = int(score)
60 else:
61 score = Rating.reverse_options.get(score)
62
63 c.art.rate(score, c.user)
64 elixir.session.commit()
65
66 redirect(url('show_art', id=c.art.id))