Skip to content

Commit

Permalink
Fixing jsonapi_permissions bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ColtonProvias committed Mar 20, 2016
1 parent c830745 commit 9b8a8db
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
12 changes: 12 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# SQLAlchemy-JSONAPI Changelog

## 4.0.5

*2016-03-20*

* Fixed missing jsonapi_permissions attribute when patching relationships.

## 4.0.4

*2016-02-27*

* Fixed session being flushed error during POST to collection.

## 4.0.1 - 4.0.3

*2016-01-21*
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
requirements.append('enum34')

setup(name='SQLAlchemy-JSONAPI',
version='4.0.3',
version='4.0.5',
url='http://github.com/coltonprovias/sqlalchemy-jsonapi',
license='MIT',
author='Colton J. Provias',
Expand Down
9 changes: 5 additions & 4 deletions sqlalchemy_jsonapi/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __init__(self):
self.status_code = 200
self.data = {
'jsonapi': {'version': '1.0'},
'meta': {'sqlalchemy_jsonapi_version': '4.0.3'}
'meta': {'sqlalchemy_jsonapi_version': '4.0.5'}
}


Expand All @@ -152,7 +152,7 @@ def get_permission_test(model, field, permission, instance=None):
:param field: Name of the field or None for instance/model-wide
:param permission: Permission to check for
"""
return model.__jsonapi_permissions__\
return getattr(model, '__jsonapi_permissions__', {})\
.get(field, {})\
.get(permission, lambda x: True)

Expand Down Expand Up @@ -1050,8 +1050,9 @@ def post_collection(self, session, data, api_type):
setter = get_attr_desc(resource, key, AttributeActions.SET)
setter(resource, data['data']['attributes'][api_key])

session.add(resource)
session.commit()
session.add(resource)
session.commit()

except IntegrityError as e:
session.rollback()
raise ValidationError(str(e.orig))
Expand Down
93 changes: 93 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import re
from collections import OrderedDict

TESTS = {
'email == "[email protected]"': 'User.email == "[email protected]"',
'email.lower() == "[email protected]"': 'User.email.lower() == "[email protected]"',
'email ilike \'cj@%\'': 'User.email.ilike(\'cj@%\')',
'published_at!=null': 'User.published_at != None',
'(5<score<=10)||published_at!=null': '(5 < User.score <= 10) or published_at != None',
'id in [0, 2.3, 4]': 'User.id in [0, 2.3, 4]',
'text == "sample == true"': 'User.text == "sample == true"',
'-missing == 5': '-User.missing == 5'
}

STRING = r"(?P<STRING>('([^'\\]*(?:\\.[^'\\]*)*)'" + r'|"([^"\\]*(?:\\.[^"\\]*)*)"))'

REGEX = [STRING,
r'(?P<WHITESPACE>\s+)',
r'(?P<NAME>\b[a-zA-Z_][a-zA-Z0-9_]*\b)',
r'(?P<PAREN_OPEN>\()',
r'(?P<PAREN_CLOSE>\))',
r'(?P<BRACKET_OPEN>\[)',
r'(?P<BRACKET_CLOSE>\])',
r'(?P<GT>\>)',
r'(?P<LT>\<)',
r'(?P<GTE>\>\=)',
r'(?P<LTE>\<\=)',
r'(?P<OR>(\|\||or))',
r'(?P<AND>(\&\&|and))',
r'(?P<NOT>(\!|not))',
r'(?P<IN>in)',
r'(?P<DOT>\.)',
r'(?P<COMMA>,)',
r'(?P<EQUAL>\=\=)',
r'(?P<NOT_EQUAL>\!\=)',
r'(?P<ADD>\+)',
r'(?P<SUBTRACT>\-)',
r'(?P<MULTIPLY>\*)',
r'(?P<DIVIDE>\/)',
r'(?P<FLOOR>\/\/)',
r'(?P<MODULO>\%)',
r'(?P<CLARKSON>\*\*)',
r'(?P<INTEGER>\d+)',
r'(?P<FLOAT>\d+\.\d+)'
]

###
# EQ
# NE
# LT
# LE
# GT
# GE
# NEG
# GETITEM
# CONCAT
# LIKE
# ILIKE
# IN_
# NOTIN_
# NOTLIKE
# NOTILIKE
# IS_
# ISNOT
# STARTSWITH
# ENDSWITH
# CONTAINS
# MATCH
# ADD
# SUBTRACT
# MULTIPLY
# DIVIDE
# MODULUS
# FLOORDIV

BINARY_OPERATORS = OrderedDict([
['EQ', r'(?P<EQ>\=\=)'],
['NE', r'(?P<NE>\!\=)']
])


compiled = re.compile(r'|'.join(reversed(REGEX)), re.IGNORECASE)

for test, final in TESTS.items():
print(test)
print(final)
finalized = []
matched = compiled.finditer(test)
prev_key = None
for item in matched:
key = item.lastgroup
value = item.group(0)
print(' {:20}:{}'.format(key, value))

0 comments on commit 9b8a8db

Please sign in to comment.