aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoar Wandborg <git@wandborg.com>2012-09-15 15:25:26 +0200
committerJoar Wandborg <git@wandborg.com>2012-09-15 15:25:26 +0200
commit42c837523e5ac70a03fb310dbb15bec03d4108cd (patch)
tree08c5b454065e7823adc10fc1a6cd4d919362b3c3
parenta062149e90731cfd730d8a539a32354065a8c9e8 (diff)
downloadmediagoblin-42c837523e5ac70a03fb310dbb15bec03d4108cd.tar.lz
mediagoblin-42c837523e5ac70a03fb310dbb15bec03d4108cd.tar.xz
mediagoblin-42c837523e5ac70a03fb310dbb15bec03d4108cd.zip
Added /api/entries view
-rw-r--r--mediagoblin/plugins/api/__init__.py4
-rw-r--r--mediagoblin/plugins/api/tools.py61
-rw-r--r--mediagoblin/plugins/api/views.py16
-rw-r--r--mediagoblin/plugins/oauth/__init__.py22
4 files changed, 80 insertions, 23 deletions
diff --git a/mediagoblin/plugins/api/__init__.py b/mediagoblin/plugins/api/__init__.py
index f3526aa1..9d4b58df 100644
--- a/mediagoblin/plugins/api/__init__.py
+++ b/mediagoblin/plugins/api/__init__.py
@@ -33,7 +33,9 @@ def setup_plugin():
routes = [
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)
diff --git a/mediagoblin/plugins/api/tools.py b/mediagoblin/plugins/api/tools.py
index a8873b06..4d306274 100644
--- a/mediagoblin/plugins/api/tools.py
+++ b/mediagoblin/plugins/api/tools.py
@@ -15,15 +15,74 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
+import json
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.storage.filestorage import BasicFileStorage
_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):
@wraps(controller)
def wrapper(request, *args, **kw):
diff --git a/mediagoblin/plugins/api/views.py b/mediagoblin/plugins/api/views.py
index 7d6ef572..49c16ee6 100644
--- a/mediagoblin/plugins/api/views.py
+++ b/mediagoblin/plugins/api/views.py
@@ -17,7 +17,8 @@
import json
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
@@ -30,3 +31,16 @@ def api_test(request):
'email': request.user.email}
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)
diff --git a/mediagoblin/plugins/oauth/__init__.py b/mediagoblin/plugins/oauth/__init__.py
index 04aa7815..95919728 100644
--- a/mediagoblin/plugins/oauth/__init__.py
+++ b/mediagoblin/plugins/oauth/__init__.py
@@ -23,6 +23,7 @@ from webob import exc
from mediagoblin.tools import pluginapi
from mediagoblin.tools.response import render_to_response
from mediagoblin.plugins.oauth.models import OAuthToken
+from mediagoblin.plugins.api.tools import Auth
_log = logging.getLogger(__name__)
@@ -45,26 +46,7 @@ def setup_plugin():
pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
-class OAuthAuth(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 __init__(self):
- pass
-
+class OAuthAuth(Auth):
def trigger(self, request):
return True