Merge branch 'stable'
[mediagoblin.git] / mediagoblin / plugins / api / views.py
CommitLineData
a062149e
JW
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
17import json
4504dbba 18import logging
4504dbba 19
e49b7e02
BP
20import six
21
131b7495 22from werkzeug.exceptions import BadRequest
74af60bb 23from werkzeug.wrappers import Response
a062149e 24
131b7495 25from mediagoblin.tools.translate import pass_to_ugettext as _
1c694fbe 26from mediagoblin.tools.response import json_response
4504dbba 27from mediagoblin.decorators import require_active_login
4504dbba 28from mediagoblin.meddleware.csrf import csrf_exempt
54b4b28f 29from mediagoblin.media_types import FileTypeNotSupported
04e08d42 30from mediagoblin.plugins.api.tools import api_auth, get_entry_serializable
131b7495
CAW
31from mediagoblin.submit.lib import \
32 check_file_field, submit_media, get_upload_file_limits, \
33 FileUploadLimit, UserUploadLimit, UserPastUploadLimit
a062149e 34
4504dbba
JW
35_log = logging.getLogger(__name__)
36
37
38@csrf_exempt
39@api_auth
40@require_active_login
41def post_entry(request):
42 _log.debug('Posting entry')
09e528ac
JW
43
44 if request.method == 'OPTIONS':
45 return json_response({'status': 200})
46
4504dbba 47 if request.method != 'POST':
09e528ac 48 _log.debug('Must POST against post_entry')
cfa92229 49 raise BadRequest()
4504dbba 50
2ef2f46e 51 if not check_file_field(request, 'file'):
09e528ac 52 _log.debug('File field not found')
cfa92229 53 raise BadRequest()
4504dbba 54
131b7495
CAW
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:
e49b7e02 59 callback_url = six.text_type(callback_url)
131b7495
CAW
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,
e49b7e02
BP
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', '')),
f6bad0eb 68 tags_string=six.text_type(request.form.get('tags', '')),
131b7495
CAW
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.'))
54b4b28f
BB
85 except FileTypeNotSupported as e:
86 raise BadRequest(e)
4504dbba 87
a062149e
JW
88
89@api_auth
78fa73bc 90@require_active_login
a062149e 91def api_test(request):
a062149e
JW
92 user_data = {
93 'username': request.user.username,
94 'email': request.user.email}
95
74af60bb
SS
96 # TODO: This is the *only* thing using Response() here, should that
97 # not simply use json_response()?
6fa97824 98 return Response(json.dumps(user_data, sort_keys=True))
42c83752
JW
99
100
101def get_entries(request):
102 entries = request.db.MediaEntry.query
103
c92aa0d0 104 # TODO: Make it possible to fetch unprocessed media, or media in-processing
42c83752
JW
105 entries = entries.filter_by(state=u'processed')
106
c92aa0d0
JW
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
42c83752
JW
113 entries_serializable = []
114
115 for entry in entries:
85726f73 116 entries_serializable.append(get_entry_serializable(entry, request.urlgen))
42c83752
JW
117
118 return json_response(entries_serializable)