diff options
author | Joar Wandborg <git@wandborg.com> | 2012-09-15 15:54:22 +0200 |
---|---|---|
committer | Joar Wandborg <git@wandborg.com> | 2012-09-15 15:54:22 +0200 |
commit | 85726f7360e2a7dbc74764c26501ac633bc10049 (patch) | |
tree | 6bd7252aafcd076552b2b77114c03c9ee83ba4f8 /mediagoblin/plugins/api/tools.py | |
parent | 42c837523e5ac70a03fb310dbb15bec03d4108cd (diff) | |
download | mediagoblin-85726f7360e2a7dbc74764c26501ac633bc10049.tar.lz mediagoblin-85726f7360e2a7dbc74764c26501ac633bc10049.tar.xz mediagoblin-85726f7360e2a7dbc74764c26501ac633bc10049.zip |
Added fields to /api/entries, wrote docstrings for api.tools
Diffstat (limited to 'mediagoblin/plugins/api/tools.py')
-rw-r--r-- | mediagoblin/plugins/api/tools.py | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/mediagoblin/plugins/api/tools.py b/mediagoblin/plugins/api/tools.py index 4d306274..e3b15b23 100644 --- a/mediagoblin/plugins/api/tools.py +++ b/mediagoblin/plugins/api/tools.py @@ -51,16 +51,37 @@ class Auth(object): raise NotImplemented() -def json_response(serializable): - response = Response(json.dumps(serializable)) +def json_response(serializable, *args, **kw): + ''' + Serializes a json objects and returns a webob.Response object with the + serialized value as the response body and Content-Type: application/json. + + :param serializable: A json-serializable object + + Any extra arguments and keyword arguments are passed to the + webob.Response.__init__ method. + ''' + response = Response(json.dumps(serializable), *args, **kw) response.headers['Content-Type'] = 'application/json' return response -def get_entry_serializable(entry): +def get_entry_serializable(entry, urlgen): + ''' + Returns a serializable dict() of a MediaEntry instance. + + :param entry: A MediaEntry instance + :param urlgen: An urlgen instance, can be found on the request object passed + to views. + ''' return { 'user': entry.get_uploader.username, 'user_id': entry.get_uploader.id, + 'user_bio': entry.get_uploader.bio, + 'user_bio_html': entry.get_uploader.bio_html, + 'user_permalink': urlgen('mediagoblin.user_pages.user_home', + user=entry.get_uploader.username, + qualified=True), 'id': entry.id, 'created': entry.created.isoformat(), 'title': entry.title, @@ -68,10 +89,18 @@ def get_entry_serializable(entry): 'description': entry.description, 'description_html': entry.description_html, 'media_type': entry.media_type, - 'media_files': get_media_file_paths(entry.media_files)} + 'permalink': entry.url_for_self(urlgen, qualified=True), + 'media_files': get_media_file_paths(entry.media_files, urlgen)} + +def get_media_file_paths(media_files, urlgen): + ''' + Returns a dictionary of media files with `file_handle` => `qualified URL` -def get_media_file_paths(media_files): + :param media_files: dict-like object consisting of `file_handle => `listy + filepath` pairs. + :param urlgen: An urlgen object, usually found on request.urlgen. + ''' if isinstance(mg_globals.public_store, BasicFileStorage): pass # TODO @@ -84,6 +113,11 @@ def get_media_file_paths(media_files): def api_auth(controller): + ''' + Decorator, allows plugins to register auth methods that will then be + evaluated against the request, finally a worthy authenticator object is + chosen and used to decide whether to grant or deny access. + ''' @wraps(controller) def wrapper(request, *args, **kw): auth_candidates = [] |