Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
added field filtering on serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed May 1, 2017
1 parent 5a389ac commit cb9c103
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
[ ] Generalize use of API backend (So that it can use any ORM or OGM other than neomodel)
[x] Fix __validation_rules__ property to use datatypes of the model (bind them together somehow!)
[x] Include unit-tests
[ ] Include serialization filtering on fields (return only specified fields)
[ ] Add unit-tests for put, patch and delete verbs
[x] Include serialization filtering on fields (return only specified fields)
[x] Add unit-tests for put, patch and delete verbs
3 changes: 3 additions & 0 deletions examples/extended_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Person(StructuredNode, utils.Node):
last_name = StringProperty()
phone_number = StringProperty(unique_index=True, required=True)

secret_field = StringProperty(default="secret", required=False)

pets = RelationshipTo(Pet, "HAS_PET")


Expand All @@ -44,6 +46,7 @@ class PersonsView(GRest):
"secondary": {
"pets": "pet_id"
}}
__filtered_fields__ = ["secret_field"]


class PetsView(GRest):
Expand Down
4 changes: 4 additions & 0 deletions grest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

class Node(object):
__validation_rules__ = {}
__filtered_fields__ = []

def __init__(self):
super(self.__class__, self)
Expand All @@ -29,6 +30,9 @@ def serialize(self):
blocked_properties = ["id", "password",
"current_otp", "validation_rules"]

if self.__filtered_fields__:
blocked_properties.extend(self.__filtered_fields__)

removable_keys = set()
for prop in properties.keys():
# remove null key/values
Expand Down
6 changes: 5 additions & 1 deletion tests/test_extended_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,17 @@ def test_api_post_person(client):
global uid
res = client.post("/persons",
data=json.dumps(
{"first_name": "test1", "last_name": "test2", "phone_number": "123"}),
{"first_name": "test1", "last_name": "test2",
"phone_number": "123", "secret_field": "MY_SECRET"}),
headers={"Content-Type": "application/json"})
assert res.status_code == 200
if ("uid" in res.json):
uid = res.json["uid"]
assert "uid" in res.json

# check if `secret_field` is filtered
assert "secret_field" not in res.json

# post existing person
res = client.post("/persons",
data=json.dumps(
Expand Down

0 comments on commit cb9c103

Please sign in to comment.