adding new art with user relations is there, sort of. Still needs adding relations...
[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 import Art, Rating, UserRelation
13 from floof.model.comments import Discussion
14
15 from sqlalchemy.exceptions import IntegrityError
16
17 from wtforms.validators import ValidationError
18 from wtforms import *
19
20
21 class ArtUploadForm(Form):
22 by = TextField('Artists')
23 file = FileField('Upload')
24 url = TextField('Link')
25
26 # TODO: make this general purpose
27 def validate_file(self, field):
28 if field.data == u'':
29 raise ValidationError('File is required')
30
31 # Also make this into a general User List field validator
32 """ PLEASE NOTE! I just realized that I need to have a __str__ method on User
33 to get it to write the usernames back in the form when it redisplays them, since
34 this validator turns them into user objects instead. This fact actually sounds dangerous
35 to me in the future, since it means I proably shouldn't be changing the data input
36 by the user right here in the validator, or the user will see the post-mangled data instead
37 of what they actually typed. Hm.
38
39 One solution to this could be to only look up the users after normal validation is over,
40 and then manually add validation errors to the form if that fails. But I think that kind of
41 sucks. Perhaps the ideology in Formish, where they keep Validation and Conversion as
42 separate tasks, is a better way of doing it? That way there is less risk of changing the user's
43 input -- you do that at the conversiot stage -- yet it is still encapsulated in the form workflow.
44 Hm. But that means I'd have to query for the users in the validation step and throw them away,
45 or something equally stupid. Guess there's no perfect solution here, but I thought it was
46 worth discussing.
47
48 Btw, this is meant to be used by a field with multi user autocompletion on it (like on stackoverflow tags),
49 so the user should never actually submit anything invalid unless they disable javascript and force it.
50 """
51 def validate_by(self, field):
52 if not field.data:
53 raise ValidationError("Needs at least one creator")
54 user_names = field.data.split()
55 users = []
56 # TODO: Could totally do a filter__in here instead of picking them out individually
57 for user_name in user_names:
58 user = User.get_by(name=user_name)
59 if not user:
60 raise ValidationError("Couldn't find user %s" % user_name)
61 users.append(user)
62 field.data = users
63
64 class ArtController(BaseController):
65 def __before__(self, id=None):
66 super(ArtController, self).__before__()
67 # Awesome refactoring!
68 if id:
69 c.art = h.get_object_or_404(Art, id=id)
70
71 def new(self):
72 """ New Art! """
73 c.form = ArtUploadForm()
74 return render("/art/new.mako")
75
76 # TODO: login required
77 def create(self):
78 c.form = ArtUploadForm(request.params)
79 if c.form.validate():
80
81 c.art = Art(uploader=c.user, **request.params)
82 c.art.discussion = Discussion(count=0)
83
84 for artist in c.form.by.data:
85 UserRelation(user=artist, kind="by", creator=c.user, art=c.art)
86
87 file = request.params['file']
88
89 try:
90 elixir.session.commit()
91 redirect(url('show_art', id=c.art.id))
92 except IntegrityError:
93 # hurr, there must be a better way to do this but I am lazy right now
94 hash = c.art.hash
95 elixir.session.rollback()
96 duplicate_art = Art.get_by(hash=hash)
97 h.flash("We already have that one.")
98 redirect(url('show_art', id=duplicate_art.id))
99
100 else:
101 ## TODO: JavaScript should be added to the upload form so that it is
102 ## impossible to submit the form when it contains any invalid users,
103 ## so this never happens. Only autocompled usernames should be allowed.
104 return render("/art/new.mako")
105
106
107 def show(self, id):
108 # c.art = h.get_object_or_404(Art, id=id)
109 if c.user:
110 c.your_score = c.art.user_score(c.user)
111 return render("/art/show.mako")
112
113
114 # TODO: login required
115 def rate(self, id):
116 # c.art = h.get_object_or_404(Art, id=id)
117 score = request.params.get("score")
118 if score and score.isnumeric():
119 score = int(score)
120 else:
121 score = Rating.reverse_options.get(score)
122
123 c.art.rate(score, c.user)
124 elixir.session.commit()
125
126 redirect(url('show_art', id=c.art.id))