3 from pylons
import config
, request
, response
, session
, tmpl_context
as c
4 from pylons
.controllers
.util
import abort
, redirect
6 from floof
.lib
.base
import BaseController
, render
8 log
= logging
.getLogger(__name__
)
10 from floof
.lib
import file_storage
as storage
, helpers
as h
11 from floof
.model
import Art
, Rating
, ArtUser
12 from floof
.model
.art
import ArtUserType
13 from floof
.model
.comments
import Discussion
14 from floof
.model
.users
import User
, UserRelationship
21 from sqlalchemy
.exceptions
import IntegrityError
22 from sqlalchemy
.orm
.exc
import NoResultFound
23 from wtforms
.validators
import ValidationError
27 class ArtUploadForm(Form
):
28 by
= TextField('Artists')
29 by_me
= BooleanField('me')
30 file = FileField('Upload')
31 url
= TextField('Link')
33 # TODO: make this general purpose
34 def validate_file(self
, field
):
36 raise ValidationError('File is required')
39 class ArtController(BaseController
):
40 def __before__(self
, id=None):
41 super(ArtController
, self
).__before__()
42 # Awesome refactoring!
44 c
.art
= h
.get_object_or_404(Art
, id=id)
48 c
.form
= ArtUploadForm()
49 return render("/art/new.mako")
51 # TODO: login required
53 c
.form
= ArtUploadForm(request
.params
)
54 if not c
.form
.validate():
55 ## TODO: JavaScript should be added to the upload form so that it is
56 ## impossible to submit the form when it contains any invalid users,
57 ## so this never happens. Only autocompled usernames should be allowed.
58 return render("/art/new.mako")
61 upload
= request
.params
['file']
62 hash = storage
.save_file('art/original', upload
.file)
64 # Create a thumbnail and a medium view
66 config
['app_conf']['static_root']
67 + storage
.get_path('art/original', hash)[1:]
69 for subdir
, config_size
in [('medium', config
['medium_size']),
70 ('thumbnail', config
['thumbnail_size'])]:
71 path
= config
['app_conf']['static_root'] + storage
.get_path("art/{0}".format(subdir
), hash)
73 dir, _
= os
.path
.split(path
)
74 if not os
.path
.exists(dir):
77 size
= int(config_size
)
78 shrunken_img
= img
.copy()
79 shrunken_img
.thumbnail((size
, size
), PIL
.Image
.ANTIALIAS
)
80 shrunken_img
.save(path
, img
.format
)
82 #magicker = magic.Magic(mime=True)
83 buffer = upload
.file.read(64)
85 mimetype
= 'image/unknown' #magickery.from_buffer(buffer)
87 # XXX Ensure we can actually handle the mimetype
92 original_filename
=upload
.filename
,
96 c
.art
.discussion
= Discussion(count
=0)
99 # if c.form.by_me and c.user not in c.form.by.data:
100 # UserRelation(user=c.user, creator=c.user, kind="by", art=c.art)
102 # for artist in c.form.by.data:
103 # UserRelation(user=artist, creator=c.user, kind="by", art=c.art)
105 # For the moment, cheerfully assume that people are uploading their own
107 ArtUser(art
=c
.art
, user
=c
.user
, type=ArtUserType
.BY
)
108 # >>>>>>> origin/master
112 elixir
.session
.commit()
113 redirect(url('show_art', id=c
.art
.id))
114 except IntegrityError
:
115 # XXX Check this as early as possible, and clean up the filesystem!
116 # Right now this replaces the original file!
118 elixir
.session
.rollback()
119 duplicate_art
= Art
.get_by(hash=hash)
120 h
.flash("We already have that one.")
121 redirect(url('show_art', id=duplicate_art
.id))
126 # c.art = h.get_object_or_404(Art, id=id)
128 c
.your_score
= c
.art
.user_score(c
.user
)
129 return render("/art/show.mako")
132 # TODO: login required
134 # c.art = h.get_object_or_404(Art, id=id)
135 score
= request
.params
.get("score")
136 if score
and score
.isnumeric():
139 score
= Rating
.reverse_options
.get(score
)
141 c
.art
.rate(score
, c
.user
)
142 elixir
.session
.commit()
144 redirect(url('show_art', id=c
.art
.id))
147 def watchstream(self
, name
):
148 """Watchstream for a certain user."""
150 c
.watching_user
= User
.get_by(name
=name
)
151 except NoResultFound
:
154 # This user has watches which are users which have art
155 # XXX use artist, not uploader
156 c
.artwork
= Art
.query
.join(Art
.uploader
,
157 User
.target_of_relationships
) \
158 .filter(UserRelationship
.user_id
== c
.watching_user
.id)
160 return render('/index.mako')