Skip to content

Commit

Permalink
Merge branch 'develop' into feature/rel_links
Browse files Browse the repository at this point in the history
Conflicts:
	sandman/model/__init__.py
	sandman/model/models.py
	sandman/templates/resource.html
  • Loading branch information
Jeff Knupp committed Aug 23, 2013
2 parents 8d0ea28 + a948720 commit 309f825
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
12 changes: 7 additions & 5 deletions sandman/model/__init__.py
Expand Up @@ -28,10 +28,10 @@ def register(cls, use_admin=True):
if isinstance(cls, (list, tuple)):
for entry in cls:
_register_internal_data(entry)
entry._use_admin = use_admin
entry.use_admin = use_admin
else:
_register_internal_data(cls)
cls._use_admin = use_admin
cls.use_admin = use_admin
Model.prepare(db.engine)

def _register_internal_data(cls):
Expand All @@ -40,14 +40,16 @@ def _register_internal_data(cls):
current_app.table_to_endpoint[cls.__tablename__] = cls.endpoint()
current_app.classes_by_name[cls.__name__] = cls

def prepare_relationships():
def _prepare_relationships():
"""Enrich the registered Models with SQLAlchemy ``relationships``
so that related tables are correctly processed up by the admin."""
inspector = reflection.Inspector.from_engine(db.engine)
with app.app_context():
for cls in current_app.classes_by_name.values():
for foreign_key in inspector.get_foreign_keys(cls.__tablename__):
other = current_app.classes_by_name[foreign_key['referred_table']]
other.__related_tables__.append(cls)
cls.__related_tables__.append(other)
other.__related_tables__.add(cls)
cls.__related_tables__.add(other)
# Necessary to get Flask-Admin to register the relationship
setattr(other, '_ref_' + cls.__tablename__.lower(), relationship(cls.__tablename__, backref='_fk_' + other.__tablename__.lower()))

Expand Down
2 changes: 1 addition & 1 deletion sandman/model/models.py
Expand Up @@ -35,7 +35,7 @@ class Model(object):
__table__ = None
"""Will be populated by SQLAlchemy with the table's meta-information."""

__related_tables__ = list()
__related_tables__ = set()
"""List of Models for which this model has a foreign key relationship
with."""

Expand Down
16 changes: 10 additions & 6 deletions sandman/sandman.py
Expand Up @@ -8,9 +8,10 @@
from .exception import InvalidAPIUsage

JSON, HTML = range(2)
JSON_CONTENT_TYPES = ('application/json',)
HTML_CONTENT_TYPES = ('text/html', 'application/x-www-form-urlencoded')
ACCEPTABLE_CONTENT_TYPES = JSON_CONTENT_TYPES + HTML_CONTENT_TYPES
JSON_CONTENT_TYPES = set(['application/json',])
HTML_CONTENT_TYPES = set(['text/html', 'application/x-www-form-urlencoded'])
ALL_CONTENT_TYPES = set(['*/*'])
ACCEPTABLE_CONTENT_TYPES = JSON_CONTENT_TYPES | HTML_CONTENT_TYPES | ALL_CONTENT_TYPES

FORWARDED_EXCEPTION_MESSAGE = 'Request could not be completed. Exception: [{}]'
FORBIDDEN_EXCEPTION_MESSAGE = """Method [{}] not acceptable for resource type [{}].
Expand All @@ -32,11 +33,12 @@ def _perform_database_action(action, *args):

def _get_acceptable_response_type():
"""Return the mimetype for this request."""
if 'Accept' not in request.headers:
if 'Accept' not in request.headers or request.headers['Accept'] in ALL_CONTENT_TYPES:
return JSON
if request.headers['Accept'] in HTML_CONTENT_TYPES:
acceptable_content_types = set(request.headers['ACCEPT'].strip().split(','))
if acceptable_content_types & HTML_CONTENT_TYPES:
return HTML
elif request.headers['Accept'] in JSON_CONTENT_TYPES:
elif acceptable_content_types & JSON_CONTENT_TYPES:
return JSON
else:
# HTTP 406 Not Acceptable
Expand All @@ -58,6 +60,7 @@ def handle_exception(error):
# type in the request's 'Accept' header, which is a more important
# error, so return that instead of what was originally raised.
response = jsonify(error.to_dict())
print request.headers, e, error
response.status_code = 415
return response

Expand Down Expand Up @@ -139,6 +142,7 @@ def get_resource_data(request):
return request.form
else:
# HTTP 415: Unsupported Media Type
print request.headers
raise InvalidAPIUsage(415,
UNSUPPORTED_CONTENT_TYPE_MESSAGE.format(
request.headers['Content-type']))
Expand Down
4 changes: 4 additions & 0 deletions sandman/templates/resource.html
Expand Up @@ -8,7 +8,11 @@ <h4>{{ resource.pk }}</h4>
{% if attribute == 'links' %}
<li>Links<ul>
{% for link in value %}
{% if link.rel != 'self' %}
<li>link_{{link.rel}}: <a href="{{ link.uri }}">{{ link.uri }}</a></li>
{% else %}
<li>{{link.rel}}: <a href="{{ link.uri }}">{{ link.uri }}</a></li>
{% endif %}
{% endfor %}
</ul></li>
{% else %}
Expand Down

0 comments on commit 309f825

Please sign in to comment.