3 from pylons
import request
, response
, session
, tmpl_context
as c
, h
4 from pylons
.controllers
.util
import abort
, redirect
6 from floof
.lib
.base
import BaseController
, render
8 log
= logging
.getLogger(__name__
)
11 from floof
.model
.users
import User
12 from floof
.model
import Art
, Rating
, UserRelation
13 from floof
.model
.comments
import Discussion
15 from sqlalchemy
.exceptions
import IntegrityError
17 from wtforms
.validators
import ValidationError
21 class ArtUploadForm(Form
):
22 by
= TextField('Artists')
23 file = FileField('Upload')
24 url
= TextField('Link')
26 # TODO: make this general purpose
27 def validate_file(self
, field
):
29 raise ValidationError('File is required')
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.
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
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.
51 def validate_by(self
, field
):
53 raise ValidationError("Needs at least one creator")
54 user_names
= field
.data
.split()
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
)
60 raise ValidationError("Couldn't find user %s" % user_name
)
64 class ArtController(BaseController
):
65 def __before__(self
, id=None):
66 super(ArtController
, self
).__before__()
67 # Awesome refactoring!
69 c
.art
= h
.get_object_or_404(Art
, id=id)
73 c
.form
= ArtUploadForm()
74 return render("/art/new.mako")
76 # TODO: login required
78 c
.form
= ArtUploadForm(request
.params
)
81 c
.art
= Art(uploader
=c
.user
, **request
.params
)
82 c
.art
.discussion
= Discussion(count
=0)
84 for artist
in c
.form
.by
.data
:
85 UserRelation(user
=artist
, kind
="by", creator
=c
.user
, art
=c
.art
)
87 file = request
.params
['file']
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
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))
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")
108 # c.art = h.get_object_or_404(Art, id=id)
110 c
.your_score
= c
.art
.user_score(c
.user
)
111 return render("/art/show.mako")
114 # TODO: login required
116 # c.art = h.get_object_or_404(Art, id=id)
117 score
= request
.params
.get("score")
118 if score
and score
.isnumeric():
121 score
= Rating
.reverse_options
.get(score
)
123 c
.art
.rate(score
, c
.user
)
124 elixir
.session
.commit()
126 redirect(url('show_art', id=c
.art
.id))