Added MIT license.
[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 from floof.model.users import User, UserRelationship
14
15 from sqlalchemy import func
16 from sqlalchemy.exceptions import IntegrityError
17 from sqlalchemy.orm.exc import NoResultFound
18
19
20 class ArtController(BaseController):
21 def __before__(self, id=None):
22 super(ArtController, self).__before__()
23 # Awesome refactoring!
24 if id:
25 c.art = h.get_object_or_404(Art, id=id)
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 c.art.discussion = Discussion(count=0)
35
36 try:
37 elixir.session.commit()
38 redirect(url('show_art', id=c.art.id))
39 except IntegrityError:
40 # hurr, there must be a better way to do this but I am lazy right now
41 hash = c.art.hash
42 elixir.session.rollback()
43 duplicate_art = Art.get_by(hash=hash)
44 h.flash("We already have that one.")
45 redirect(url('show_art', id=duplicate_art.id))
46
47
48 def show(self, id):
49 # c.art = h.get_object_or_404(Art, id=id)
50 if c.user:
51 c.your_score = c.art.user_score(c.user)
52 return render("/art/show.mako")
53
54
55 # TODO: login required
56 def rate(self, id):
57 # c.art = h.get_object_or_404(Art, id=id)
58 score = request.params.get("score")
59 if score and score.isnumeric():
60 score = int(score)
61 else:
62 score = Rating.reverse_options.get(score)
63
64 c.art.rate(score, c.user)
65 elixir.session.commit()
66
67 redirect(url('show_art', id=c.art.id))
68
69
70 def watchstream(self, name):
71 """Watchstream for a certain user."""
72 try:
73 c.watching_user = User.query.filter(func.lower(User.name) == name) \
74 .one()
75 except NoResultFound:
76 abort(404)
77
78 # This user has watches which are users which have art
79 # XXX use artist, not uploader
80 c.artwork = Art.query.join(Art.uploader,
81 User.target_of_relationships) \
82 .filter(UserRelationship.user_id == c.watching_user.id)
83
84 return render('/index.mako')