diff --git a/LICENSE b/LICENSE index 37ec93a..bafcd20 100644 --- a/LICENSE +++ b/LICENSE @@ -176,7 +176,7 @@ recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright [yyyy] [name of copyright owner] + Copyright 2013 Jeff Knupp Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/docs/.doctrees/environment.pickle b/docs/.doctrees/environment.pickle index 6330e67..01267cd 100644 Binary files a/docs/.doctrees/environment.pickle and b/docs/.doctrees/environment.pickle differ diff --git a/docs/.doctrees/index.doctree b/docs/.doctrees/index.doctree index 83cfd3f..8bfdbd4 100644 Binary files a/docs/.doctrees/index.doctree and b/docs/.doctrees/index.doctree differ diff --git a/docs/.doctrees/installation.doctree b/docs/.doctrees/installation.doctree index 089ce72..4448820 100644 Binary files a/docs/.doctrees/installation.doctree and b/docs/.doctrees/installation.doctree differ diff --git a/docs/.doctrees/modules.doctree b/docs/.doctrees/modules.doctree index 90fa1e7..0f6ac6e 100644 Binary files a/docs/.doctrees/modules.doctree and b/docs/.doctrees/modules.doctree differ diff --git a/docs/.doctrees/quickstart.doctree b/docs/.doctrees/quickstart.doctree index dfbc78d..0a3b8ff 100644 Binary files a/docs/.doctrees/quickstart.doctree and b/docs/.doctrees/quickstart.doctree differ diff --git a/docs/.doctrees/sandman.doctree b/docs/.doctrees/sandman.doctree index def6aad..9ffb27f 100644 Binary files a/docs/.doctrees/sandman.doctree and b/docs/.doctrees/sandman.doctree differ diff --git a/doc/conf.py b/docs/conf.py similarity index 100% rename from doc/conf.py rename to docs/conf.py diff --git a/docs/generated/.buildinfo b/docs/generated/.buildinfo new file mode 100644 index 0000000..0885fbe --- /dev/null +++ b/docs/generated/.buildinfo @@ -0,0 +1,4 @@ +# Sphinx build info version 1 +# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. +config: +tags: diff --git a/docs/generated/.doctrees/environment.pickle b/docs/generated/.doctrees/environment.pickle new file mode 100644 index 0000000..58a296c Binary files /dev/null and b/docs/generated/.doctrees/environment.pickle differ diff --git a/docs/generated/.doctrees/index.doctree b/docs/generated/.doctrees/index.doctree new file mode 100644 index 0000000..8bfdbd4 Binary files /dev/null and b/docs/generated/.doctrees/index.doctree differ diff --git a/docs/generated/.doctrees/installation.doctree b/docs/generated/.doctrees/installation.doctree new file mode 100644 index 0000000..4448820 Binary files /dev/null and b/docs/generated/.doctrees/installation.doctree differ diff --git a/docs/generated/.doctrees/modules.doctree b/docs/generated/.doctrees/modules.doctree new file mode 100644 index 0000000..0f6ac6e Binary files /dev/null and b/docs/generated/.doctrees/modules.doctree differ diff --git a/docs/generated/.doctrees/quickstart.doctree b/docs/generated/.doctrees/quickstart.doctree new file mode 100644 index 0000000..0a3b8ff Binary files /dev/null and b/docs/generated/.doctrees/quickstart.doctree differ diff --git a/docs/generated/.doctrees/sandman.doctree b/docs/generated/.doctrees/sandman.doctree new file mode 100644 index 0000000..9ffb27f Binary files /dev/null and b/docs/generated/.doctrees/sandman.doctree differ diff --git a/docs/_modules/index.html b/docs/generated/_modules/index.html similarity index 100% rename from docs/_modules/index.html rename to docs/generated/_modules/index.html diff --git a/docs/_modules/sandman/exception.html b/docs/generated/_modules/sandman/exception.html similarity index 100% rename from docs/_modules/sandman/exception.html rename to docs/generated/_modules/sandman/exception.html diff --git a/docs/_modules/sandman/model.html b/docs/generated/_modules/sandman/model.html similarity index 99% rename from docs/_modules/sandman/model.html rename to docs/generated/_modules/sandman/model.html index 6ae7120..ae35493 100644 --- a/docs/_modules/sandman/model.html +++ b/docs/generated/_modules/sandman/model.html @@ -62,7 +62,7 @@
endpoint.
:param cls: User-defined class derived from :class:`sandman.Resource` to be
- registered with the endpoint returned by :func:`endpoint()`
+ registered with the endpoint returned by :func:`endpoint()`
:type cls: :class:`sandman.Resource` or tuple
"""
diff --git a/docs/_modules/sandman/sandman.html b/docs/generated/_modules/sandman/sandman.html
similarity index 78%
rename from docs/_modules/sandman/sandman.html
rename to docs/generated/_modules/sandman/sandman.html
index 7d79ce2..de23451 100644
--- a/docs/_modules/sandman/sandman.html
+++ b/docs/generated/_modules/sandman/sandman.html
@@ -49,10 +49,14 @@ Navigation
Source code for sandman.sandman
"""Sandman REST API creator for Flask and SQLAlchemy"""
-from flask import jsonify, request, g, current_app, Response
+from flask import (jsonify, request, g,
+ current_app, Response, render_template,
+ make_response)
from . import app, db
from .exception import JSONException
+JSON, HTML = range(2)
+
def _get_session():
"""Return (and memoize) a database session"""
session = getattr(g, '_session', None)
@@ -60,6 +64,40 @@ Source code for sandman.sandman
session = g._session = db.session()
return session
+def _get_mimetype(current_request):
+ """Return the mimetype for this request."""
+ if 'Accept' not in current_request.headers:
+ return JSON
+
+ if 'json' in current_request.headers['Accept']:
+ return JSON
+ else:
+ return HTML
+
+def _single_resource_json_response(resource):
+ """Return the JSON representation of *resource*"""
+ return jsonify(resource.as_dict())
+
+def _single_resource_html_response(resource):
+ """Return the HTML representation of *resource*"""
+ tablename = resource.__tablename__
+ resource.pk = getattr(resource, resource.primary_key())
+ resource.attributes = resource.as_dict()
+ return make_response(render_template('resource.html', resource=resource,
+ tablename=tablename))
+
+def _collection_json_response(resources):
+ """Return the HTML representation of the collection *resources*"""
+ result_list = []
+ for resource in resources:
+ result_list.append(resource.as_dict())
+ return jsonify(resources=result_list)
+
+def _collection_html_response(resources):
+ """Return the HTML representation of the collection *resources*"""
+ return make_response(render_template('collection.html',
+ resources=resources))
+
def _validate(cls, method, resource=None):
"""Return ``True`` if the the given *cls* supports the HTTP *method* found
on the incoming HTTP request.
@@ -75,7 +113,7 @@ Source code for sandman.sandman
if not method in cls.__methods__:
return False
- class_validator_name = 'do_' + method
+ class_validator_name = 'validate_' + method
if hasattr(cls, class_validator_name):
class_validator = getattr(cls, class_validator_name)
@@ -83,18 +121,29 @@ Source code for sandman.sandman
return True
-[docs]def resource_created_response(resource):
+[docs]def resource_created_response(resource, current_request):
"""Return HTTP response with status code *201*, signaling a created *resource*
:param resource: resource created as a result of current request
:type resource: :class:`sandman.model.Resource`
:rtype: :class:`flask.Response`
"""
- response = jsonify(resource.as_dict())
+ if _get_mimetype(current_request) == JSON:
+ response = _single_resource_json_response(resource)
+ else:
+ response = _single_resource_html_response(resource)
response.status_code = 201
response.headers['Location'] = 'http://localhost:5000/' + resource.resource_uri()
return response
+[docs]def resource_response(resource, current_request):
+ result_dict = resource.as_dict()
+ if _get_mimetype(current_request) == JSON:
+ return _single_resource_json_response(resource)
+ else:
+ return _single_resource_html_response(resource)
+
+
[docs]def unsupported_method_response():
"""Return the appropriate *Response* with status code *403*, signaling the HTTP method used
in the request is not supported for the given endpoint.
@@ -149,13 +198,12 @@ Source code for sandman.sandman
setattr(resource, resource.primary_key(), lookup_id)
session.add(resource)
session.commit()
- return resource_created_response(resource)
+ return resource_created_response(resource, request)
else:
resource.from_dict(request.json)
session.merge(resource)
session.commit()
return no_content_response()
-
@app.route('/<collection>', methods=['POST'])
[docs]def add_resource(collection):
@@ -176,7 +224,7 @@ Source code for sandman.sandman
session = _get_session()
session.add(resource)
session.commit()
- return resource_created_response(resource)
+ return resource_created_response(resource, request)
@app.route('/<collection>/<lookup_id>', methods=['DELETE'])
[docs]def delete_resource(collection, lookup_id):
@@ -222,8 +270,7 @@ Source code for sandman.sandman
elif not _validate(cls, request.method, resource):
return unsupported_method_response()
- result_dict = resource.as_dict()
- return jsonify(**result_dict)
+ return resource_response(resource, request)
@app.route('/<collection>', methods=['GET'])
[docs]def collection_handler(collection):
@@ -242,10 +289,10 @@ Source code for sandman.sandman
if not _validate(cls, request.method, resources):
return unsupported_method_response()
- result_list = []
- for resource in resources:
- result_list.append(resource.as_dict())
- return jsonify(resources=result_list)
+ if _get_mimetype(request) == JSON:
+ return _collection_json_response(resources)
+ else:
+ return _collection_html_response(resources)
diff --git a/docs/_sources/index.txt b/docs/generated/_sources/index.txt
similarity index 100%
rename from docs/_sources/index.txt
rename to docs/generated/_sources/index.txt
diff --git a/docs/_sources/installation.txt b/docs/generated/_sources/installation.txt
similarity index 100%
rename from docs/_sources/installation.txt
rename to docs/generated/_sources/installation.txt
diff --git a/docs/_sources/modules.txt b/docs/generated/_sources/modules.txt
similarity index 100%
rename from docs/_sources/modules.txt
rename to docs/generated/_sources/modules.txt
diff --git a/docs/_sources/quickstart.txt b/docs/generated/_sources/quickstart.txt
similarity index 98%
rename from docs/_sources/quickstart.txt
rename to docs/generated/_sources/quickstart.txt
index 117e22e..6abdb0d 100644
--- a/docs/_sources/quickstart.txt
+++ b/docs/generated/_sources/quickstart.txt
@@ -13,7 +13,6 @@ Create one file with the following contents (which I call ``runserver.py``)::
from sandman import app, db
app.config['SQLALCHEMY_DATABASE_URI'] = 'R
resource_handler() (in module sandman.sandman)
+
+ resource_response() (in module sandman.sandman)
+
+
diff --git a/docs/index.html b/docs/generated/index.html
similarity index 100%
rename from docs/index.html
rename to docs/generated/index.html
diff --git a/docs/installation.html b/docs/generated/installation.html
similarity index 100%
rename from docs/installation.html
rename to docs/generated/installation.html
diff --git a/docs/modules.html b/docs/generated/modules.html
similarity index 100%
rename from docs/modules.html
rename to docs/generated/modules.html
diff --git a/docs/generated/objects.inv b/docs/generated/objects.inv
new file mode 100644
index 0000000..6b4bc97
Binary files /dev/null and b/docs/generated/objects.inv differ
diff --git a/docs/py-modindex.html b/docs/generated/py-modindex.html
similarity index 100%
rename from docs/py-modindex.html
rename to docs/generated/py-modindex.html
diff --git a/docs/quickstart.html b/docs/generated/quickstart.html
similarity index 83%
rename from docs/quickstart.html
rename to docs/generated/quickstart.html
index da3a5cd..3897a8b 100644
--- a/docs/quickstart.html
+++ b/docs/generated/quickstart.html
@@ -55,16 +55,16 @@ Navigation
Quickstart¶
Create one file with the following contents (which I call runserver.py):
-from sandman.model import register, Model
+from sandman.model import register, Model
-# Insert Models here
-# Register models here
-# register((Model1, Model2, Model3))
+# Insert Models here
+# Register models here
+# register((Model1, Model2, Model3))
-from sandman import app, db
-app.config['SQLALCHEMY_DATABASE_URI'] = '<your database connection string (using SQLAlchemy)'
-app.run()
-```
+from sandman import app, db
+app.config['SQLALCHEMY_DATABASE_URI'] = '<your database connection string (using SQLAlchemy)'
+app.run()
+
Then simply run:
python runserver.py
diff --git a/docs/sandman.html b/docs/generated/sandman.html
similarity index 93%
rename from docs/sandman.html
rename to docs/generated/sandman.html
index 90849c3..cf41eea 100644
--- a/docs/sandman.html
+++ b/docs/generated/sandman.html
@@ -94,12 +94,11 @@ sandman Package
-Parameters: cls – User-defined class derived from sandman.Resource to be
+Parameters: cls (sandman.Resource or tuple) – User-defined class derived from sandman.Resource to be
+registered with the endpoint returned by endpoint()
-
registered with the endpoint returned by endpoint()
-:type cls: sandman.Resource or tuple
@@ -208,7 +207,7 @@ sandman Package
-sandman.sandman.resource_created_response(resource)[source]¶
+sandman.sandman.resource_created_response(resource, current_request)[source]¶
Return HTTP response with status code 201, signaling a created resource