Skip to content

Commit

Permalink
Merge pull request #49 from Martlark/2.1.3
Browse files Browse the repository at this point in the history
2.1.3 Allow sorting by lambda. Remove unsupported md elements.
  • Loading branch information
Martlark authored Mar 5, 2023
2 parents b275e12 + bdcddbc commit e9c72c2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# flask-serialize

|PyPI Version|

# DB Model JSON serialization with PUT, POST write for Flask applications using SQLAlchemy

## Installation
Expand Down Expand Up @@ -520,6 +518,16 @@ ascending use this example:
__fs_order_by_field__ = 'id'
```

A `lambda` or `method` can be used as the `__fs_order_by_field__`, in which case custom sorting can be achieved. The
passed value to the `lambda` is a dictionary of the field and properties of a result row.

Example:

```python
__fs_order_by_field__ = lambda r: -int(r["value"])
```


## Filtering query results using `__fs_can_access__` and user.

The `fs_query_by_access` method can be used to filter a SQLAlchemy result set so that
Expand Down Expand Up @@ -930,6 +938,7 @@ Version 2.0.1 changes most of the properties, hooks and methods to use a more no

## Release Notes

- 2.1.3 - Allow sorting by lambda
- 2.1.2 - Fix readme table format
- 2.1.1 - Improve sqlite JSON handling
- 2.1.0 - Convert readme to markdown. Add support for JSON columns. Withdraw Python 3.6 Support. Use unittest instead of pytest. NOTE: Changes `__fs_convert_types__` to a `dict`.
Expand Down Expand Up @@ -962,6 +971,3 @@ Version 2.0.1 changes most of the properties, hooks and methods to use a more no
## Licensing

- Apache 2.0

.. |PyPI Version| image:: https://img.shields.io/pypi/v/flask-serialize.svg
:target: https://pypi.python.org/pypi/flask-serialize
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.2
2.1.3
14 changes: 10 additions & 4 deletions flask_serialize/flask_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,16 @@ def fs_json_list(cls, query_result, prop_filters=None):

# ascending
if cls.__fs_order_by_field__:
items = sorted(
items,
key=lambda i: i[cls._fs_get_field_name(cls.__fs_order_by_field__)],
)
if callable(cls.__fs_order_by_field__):
items = sorted(
items,
key=cls.__fs_order_by_field__,
)
else:
items = sorted(
items,
key=lambda i: i[cls._fs_get_field_name(cls.__fs_order_by_field__)],
)

# descending
elif cls.__fs_order_by_field_desc__:
Expand Down
16 changes: 8 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
wtforms==3.0.1
flask==2.2.2
SQLAlchemy==1.4.44
flask==2.2.3
SQLAlchemy==2.0.4
Permissive-Dict==1.0.4
flask-sqlalchemy==3.0.2
flask-wtf==1.0.1
setuptools==65.6.3
flask-sqlalchemy==3.0.3
flask-wtf==1.1.1
setuptools==67.4.0
wheel==0.38.4
twine==4.0.1
black==22.10.0
twine==4.0.2
black==23.1.0
flask-unittest==0.1.3
flask-migrate==4.0.0
flask-migrate==4.0.4
13 changes: 13 additions & 0 deletions test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def tearDown(self, app, client):


class TestAll(TestBase):
def tearDown(self, app, client):
Setting.__fs_order_by_field__ = "value"
Setting.__fs_order_by_field_desc__ = None

def add_setting(self, client, key=random_string(), value="test-value", number=0):
rv = client.post(
"/setting_add",
Expand Down Expand Up @@ -93,6 +97,15 @@ def test__fs_order_by_field__(self, app, client):
sorted_list = sorted(json_settings, key=lambda i: i["value"], reverse=True)
for z in range(count):
assert json_settings[z]["value"] == sorted_list[z]["value"]
# method
Setting.__fs_order_by_field__ = lambda r: -int(r["value"])
Setting.__fs_order_by_field_desc__ = None
rv = client.get("/setting_get_all")
json_settings = rv.json
sorted_list = sorted(json_settings, key=lambda i: -int(i["value"]))
for z in range(count):
assert json_settings[z]["value"] == sorted_list[z]["value"]
Setting.__fs_order_by_field__ = "value"

def test_get_filter(self, app, client):
key_1 = random_string()
Expand Down

0 comments on commit e9c72c2

Please sign in to comment.