5216da7d80365f5fdf8ed1a89510d4b28a43f652
[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.users import User
12 from floof.model.art import Art, Rating, UserRelation
13 from floof.model.comments import Discussion
14
15 from sqlalchemy.exceptions import IntegrityError
16
17
18 class ArtController(BaseController):
19 def __before__(self, id=None):
20 super(ArtController, self).__before__()
21 # Awesome refactoring!
22 if id:
23 c.art = h.get_object_or_404(Art, id=id)
24
25 def new(self):
26 """ New Art! """
27 return render("/art/new.mako")
28
29 # TODO: login required
30 def create(self):
31 # if 'file' not in request.params or not request.params['file']:
32 # return "Validation Error: Needs a File"
33
34
35 c.art = Art(uploader=c.user, **request.params)
36 c.art.discussion = Discussion(count=0)
37
38
39 artist = User.get_by(name=request.params['artist'])
40 if not artist:
41 return "Validation Error: Artist not found"
42
43 relation = UserRelation(user=artist, kind="by", creator=c.user, art=c.art)
44
45 try:
46 elixir.session.commit()
47 redirect(url('show_art', id=c.art.id))
48 except IntegrityError:
49 # hurr, there must be a better way to do this but I am lazy right now
50 hash = c.art.hash
51 elixir.session.rollback()
52 duplicate_art = Art.get_by(hash=hash)
53 h.flash("We already have that one.")
54 redirect(url('show_art', id=duplicate_art.id))
55
56
57 def show(self, id):
58 # c.art = h.get_object_or_404(Art, id=id)
59 if c.user:
60 c.your_score = c.art.user_score(c.user)
61 return render("/art/show.mako")
62
63
64 # TODO: login required
65 def rate(self, id):
66 # c.art = h.get_object_or_404(Art, id=id)
67 score = request.params.get("score")
68 if score and score.isnumeric():
69 score = int(score)
70 else:
71 score = Rating.reverse_options.get(score)
72
73 c.art.rate(score, c.user)
74 elixir.session.commit()
75
76 redirect(url('show_art', id=c.art.id))