Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/jfinkels/flask-restless
Browse files Browse the repository at this point in the history
  • Loading branch information
alekzvik committed Apr 11, 2012
2 parents f33ceec + 78fbbdc commit 0e579ab
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -129,8 +129,11 @@ This is a Python module which, when executed, simply runs all unit tests.
Validation is not provided directly by Flask-Restless, but it does provide a
way for users to indicate exceptions to catch. If you wish to test validation
of SQLAlchemy models with a real external SQLAlchemy validation library,
install [SAValidation][sav]. The test suite will automatically skip these tests
if it is not installed.
install the development version of [SAValidation][sav]:

pip install -e "hg+http://bitbucket.org/rsyring/sqlalchemy-validation#egg=savlidation-dev"

The test suite will automatically skip these tests if it is not installed.

[sav]: http://pypi.python.org/pypi/SAValidation

Expand Down
33 changes: 33 additions & 0 deletions flask_restless/helpers.py
@@ -0,0 +1,33 @@
"""
flask.ext.restless.helpers
~~~~~~~~~~~~~~~~~~~~~~~~~~
Helper functions for Flask-Restless.
:copyright: 2012 Jeffrey Finkelstein <jeffrey.finkelstein@gmail.com>
:license: GNU AGPLv3+ or BSD
"""


def unicode_keys_to_strings(dictionary):
"""Returns a new dictionary with the same mappings as `dictionary`, but
with each of the keys coerced to a string (by calling :func:`str(key)`).
This function is intended to be used for Python 2.5 compatibility when
unpacking a dictionary to provide keyword arguments to a function or
method. For example::
>>> def func(a=1, b=2):
... return a + b
...
>>> d = {u'a': 10, u'b': 20}
>>> func(**d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() keywords must be strings
>>> func(**unicode_keys_to_strings(d))
30
"""
return dict((str(k), v) for k, v in dictionary.iteritems())
12 changes: 3 additions & 9 deletions flask_restless/search.py
Expand Up @@ -15,6 +15,8 @@
"""
import inspect

from .helpers import unicode_keys_to_strings

#: The mapping from operator name (as accepted by the search method) to a
#: function which returns the SQLAlchemy expression corresponding to that
#: operator.
Expand Down Expand Up @@ -65,14 +67,6 @@
}


def _unicode_keys_to_strings(dictionary):
"""Returns a new dictionary with the same mappings as `dictionary`, but
with each of the keys coerced to a string (by calling :func:`str(key)`).
"""
return dict((str(k), v) for k, v in dictionary.iteritems())


class OrderBy(object):
"""Represents an "order by" in a SQL query expression."""

Expand Down Expand Up @@ -226,7 +220,7 @@ def from_dictionary(dictionary):
filters = [from_dict(f) for f in dictionary.get('filters', [])]
# HACK In Python 2.5, unicode dictionary keys are not allowed.
order_by_list = dictionary.get('order_by', [])
order_by_list = (_unicode_keys_to_strings(o) for o in order_by_list)
order_by_list = (unicode_keys_to_strings(o) for o in order_by_list)
order_by = [OrderBy(**o) for o in order_by_list]
limit = dictionary.get('limit')
offset = dictionary.get('offset')
Expand Down
7 changes: 5 additions & 2 deletions flask_restless/views.py
Expand Up @@ -40,6 +40,7 @@
from sqlalchemy.orm.properties import RelationshipProperty as RelProperty
from sqlalchemy.sql import func

from .helpers import unicode_keys_to_strings
from .search import create_query
from .search import search

Expand Down Expand Up @@ -771,8 +772,10 @@ def post(self):
params = self._strings_to_dates(params)

try:
# Instantiate the model with the parameters
instance = self.model(**dict([(i, params[i]) for i in props]))
# Instantiate the model with the parameters.
modelargs = dict([(i, params[i]) for i in props])
# HACK Python 2.5 requires __init__() keywords to be strings.
instance = self.model(**unicode_keys_to_strings(modelargs))

# Handling relations, a single level is allowed
for col in set(relations).intersection(paramkeys):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_validation.py
Expand Up @@ -24,6 +24,7 @@
try:
import savalidation as _sav
import savalidation.validators as sav
sav_version = tuple(int(n) for n in _sav.VERSION.split('.'))
has_savalidation = True
except:
has_savalidation = False
Expand Down Expand Up @@ -245,7 +246,8 @@ def test_presence_validations(self):

# skipUnless should be used as a decorator, but Python 2.5 doesn't have
# decorators.
SAVTest = skipUnless(has_savalidation, 'savalidation not found.')(SAVTest)
SAVTest = skipUnless(has_savalidation and sav_version >= (0, 2),
'savalidation not found.')(SAVTest)


def load_tests(loader, standard_tests, pattern):
Expand Down

0 comments on commit 0e579ab

Please sign in to comment.