233410654711bfbf67a2d14df36d09f8e5760ac2
[mediagoblin.git] / mediagoblin / plugins / api / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import json
18 import logging
19
20 import six
21
22 from werkzeug.exceptions import BadRequest
23 from werkzeug.wrappers import Response
24
25 from mediagoblin.tools.translate import pass_to_ugettext as _
26 from mediagoblin.tools.response import json_response
27 from mediagoblin.decorators import require_active_login
28 from mediagoblin.meddleware.csrf import csrf_exempt
29 from mediagoblin.media_types import FileTypeNotSupported
30 from mediagoblin.plugins.api.tools import api_auth, get_entry_serializable
31 from mediagoblin.submit.lib import \
32 check_file_field, submit_media, get_upload_file_limits, \
33 FileUploadLimit, UserUploadLimit, UserPastUploadLimit
34
35 _log = logging.getLogger(__name__)
36
37
38 @csrf_exempt
39 @api_auth
40 @require_active_login
41 def post_entry(request):
42 _log.debug('Posting entry')
43
44 if request.method == 'OPTIONS':
45 return json_response({'status': 200})
46
47 if request.method != 'POST':
48 _log.debug('Must POST against post_entry')
49 raise BadRequest()
50
51 if not check_file_field(request, 'file'):
52 _log.debug('File field not found')
53 raise BadRequest()
54
55 upload_limit, max_file_size = get_upload_file_limits(request.user)
56
57 callback_url = request.form.get('callback_url')
58 if callback_url:
59 callback_url = six.text_type(callback_url)
60 try:
61 entry = submit_media(
62 mg_app=request.app, user=request.user,
63 submitted_file=request.files['file'],
64 filename=request.files['file'].filename,
65 title=six.text_type(request.form.get('title')),
66 description=six.text_type(request.form.get('description')),
67 license=six.text_type(request.form.get('license', '')),
68 tags_string=six.text_type(request.form.get('tags', '')),
69 upload_limit=upload_limit, max_file_size=max_file_size,
70 callback_url=callback_url)
71
72 return json_response(get_entry_serializable(entry, request.urlgen))
73
74 # Handle upload limit issues
75 except FileUploadLimit:
76 raise BadRequest(
77 _(u'Sorry, the file size is too big.'))
78 except UserUploadLimit:
79 raise BadRequest(
80 _('Sorry, uploading this file will put you over your'
81 ' upload limit.'))
82 except UserPastUploadLimit:
83 raise BadRequest(
84 _('Sorry, you have reached your upload limit.'))
85 except FileTypeNotSupported as e:
86 raise BadRequest(e)
87
88
89 @api_auth
90 @require_active_login
91 def api_test(request):
92 user_data = {
93 'username': request.user.username,
94 'email': request.user.email}
95
96 # TODO: This is the *only* thing using Response() here, should that
97 # not simply use json_response()?
98 return Response(json.dumps(user_data, sort_keys=True))
99
100
101 def get_entries(request):
102 entries = request.db.MediaEntry.query
103
104 # TODO: Make it possible to fetch unprocessed media, or media in-processing
105 entries = entries.filter_by(state=u'processed')
106
107 # TODO: Add sort order customization
108 entries = entries.order_by(request.db.MediaEntry.created.desc())
109
110 # TODO: Fetch default and upper limit from config
111 entries = entries.limit(int(request.GET.get('limit') or 10))
112
113 entries_serializable = []
114
115 for entry in entries:
116 entries_serializable.append(get_entry_serializable(entry, request.urlgen))
117
118 return json_response(entries_serializable)