Added /api/entries view
This commit is contained in:
parent
a062149e90
commit
42c837523e
@ -33,7 +33,9 @@ def setup_plugin():
|
|||||||
|
|
||||||
routes = [
|
routes = [
|
||||||
Route('mediagoblin.plugins.api.test', '/api/test',
|
Route('mediagoblin.plugins.api.test', '/api/test',
|
||||||
controller='mediagoblin.plugins.api.views:api_test')]
|
controller='mediagoblin.plugins.api.views:api_test'),
|
||||||
|
Route('mediagoblin.plugins.api.entries', '/api/entries',
|
||||||
|
controller='mediagoblin.plugins.api.views:get_entries')]
|
||||||
|
|
||||||
pluginapi.register_routes(routes)
|
pluginapi.register_routes(routes)
|
||||||
|
|
||||||
|
@ -15,15 +15,74 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import json
|
||||||
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from webob import exc
|
from webob import exc, Response
|
||||||
|
|
||||||
|
from mediagoblin import mg_globals
|
||||||
from mediagoblin.tools.pluginapi import PluginManager
|
from mediagoblin.tools.pluginapi import PluginManager
|
||||||
|
from mediagoblin.storage.filestorage import BasicFileStorage
|
||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Auth(object):
|
||||||
|
'''
|
||||||
|
An object with two significant methods, 'trigger' and 'run'.
|
||||||
|
|
||||||
|
Using a similar object to this, plugins can register specific
|
||||||
|
authentication logic, for example the GET param 'access_token' for OAuth.
|
||||||
|
|
||||||
|
- trigger: Analyze the 'request' argument, return True if you think you
|
||||||
|
can handle the request, otherwise return False
|
||||||
|
- run: The authentication logic, set the request.user object to the user
|
||||||
|
you intend to authenticate and return True, otherwise return False.
|
||||||
|
|
||||||
|
If run() returns False, an HTTP 403 Forbidden error will be shown.
|
||||||
|
|
||||||
|
You may also display custom errors, just raise them within the run()
|
||||||
|
method.
|
||||||
|
'''
|
||||||
|
def trigger(self, request):
|
||||||
|
raise NotImplemented()
|
||||||
|
|
||||||
|
def __call__(self, request, *args, **kw):
|
||||||
|
raise NotImplemented()
|
||||||
|
|
||||||
|
|
||||||
|
def json_response(serializable):
|
||||||
|
response = Response(json.dumps(serializable))
|
||||||
|
response.headers['Content-Type'] = 'application/json'
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
def get_entry_serializable(entry):
|
||||||
|
return {
|
||||||
|
'user': entry.get_uploader.username,
|
||||||
|
'user_id': entry.get_uploader.id,
|
||||||
|
'id': entry.id,
|
||||||
|
'created': entry.created.isoformat(),
|
||||||
|
'title': entry.title,
|
||||||
|
'license': entry.license,
|
||||||
|
'description': entry.description,
|
||||||
|
'description_html': entry.description_html,
|
||||||
|
'media_type': entry.media_type,
|
||||||
|
'media_files': get_media_file_paths(entry.media_files)}
|
||||||
|
|
||||||
|
|
||||||
|
def get_media_file_paths(media_files):
|
||||||
|
if isinstance(mg_globals.public_store, BasicFileStorage):
|
||||||
|
pass # TODO
|
||||||
|
|
||||||
|
media_urls = {}
|
||||||
|
|
||||||
|
for key, val in media_files.items():
|
||||||
|
media_urls[key] = mg_globals.public_store.file_url(val)
|
||||||
|
|
||||||
|
return media_urls
|
||||||
|
|
||||||
|
|
||||||
def api_auth(controller):
|
def api_auth(controller):
|
||||||
@wraps(controller)
|
@wraps(controller)
|
||||||
def wrapper(request, *args, **kw):
|
def wrapper(request, *args, **kw):
|
||||||
|
@ -17,7 +17,8 @@
|
|||||||
import json
|
import json
|
||||||
from webob import exc, Response
|
from webob import exc, Response
|
||||||
|
|
||||||
from mediagoblin.plugins.api.tools import api_auth
|
from mediagoblin.plugins.api.tools import api_auth, get_entry_serializable, \
|
||||||
|
json_response
|
||||||
|
|
||||||
|
|
||||||
@api_auth
|
@api_auth
|
||||||
@ -30,3 +31,16 @@ def api_test(request):
|
|||||||
'email': request.user.email}
|
'email': request.user.email}
|
||||||
|
|
||||||
return Response(json.dumps(user_data))
|
return Response(json.dumps(user_data))
|
||||||
|
|
||||||
|
|
||||||
|
def get_entries(request):
|
||||||
|
entries = request.db.MediaEntry.query
|
||||||
|
|
||||||
|
entries = entries.filter_by(state=u'processed')
|
||||||
|
|
||||||
|
entries_serializable = []
|
||||||
|
|
||||||
|
for entry in entries:
|
||||||
|
entries_serializable.append(get_entry_serializable(entry))
|
||||||
|
|
||||||
|
return json_response(entries_serializable)
|
||||||
|
@ -23,6 +23,7 @@ from webob import exc
|
|||||||
from mediagoblin.tools import pluginapi
|
from mediagoblin.tools import pluginapi
|
||||||
from mediagoblin.tools.response import render_to_response
|
from mediagoblin.tools.response import render_to_response
|
||||||
from mediagoblin.plugins.oauth.models import OAuthToken
|
from mediagoblin.plugins.oauth.models import OAuthToken
|
||||||
|
from mediagoblin.plugins.api.tools import Auth
|
||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -45,26 +46,7 @@ def setup_plugin():
|
|||||||
pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
|
pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
|
||||||
|
|
||||||
|
|
||||||
class OAuthAuth(object):
|
class OAuthAuth(Auth):
|
||||||
'''
|
|
||||||
An object with two significant methods, 'trigger' and 'run'.
|
|
||||||
|
|
||||||
Using a similar object to this, plugins can register specific
|
|
||||||
authentication logic, for example the GET param 'access_token' for OAuth.
|
|
||||||
|
|
||||||
- trigger: Analyze the 'request' argument, return True if you think you
|
|
||||||
can handle the request, otherwise return False
|
|
||||||
- run: The authentication logic, set the request.user object to the user
|
|
||||||
you intend to authenticate and return True, otherwise return False.
|
|
||||||
|
|
||||||
If run() returns False, an HTTP 403 Forbidden error will be shown.
|
|
||||||
|
|
||||||
You may also display custom errors, just raise them within the run()
|
|
||||||
method.
|
|
||||||
'''
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def trigger(self, request):
|
def trigger(self, request):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user