Skip to content

Commit

Permalink
Migrate to nanohttp 1x
Browse files Browse the repository at this point in the history
  • Loading branch information
meyt committed Oct 25, 2018
1 parent 08f3792 commit 5f0f8ec
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions microhttp_restful/base_model.py
Expand Up @@ -9,7 +9,7 @@

from webtest_docgen import FormParam

from nanohttp import context, HttpNotFound
from nanohttp import context, HTTPNotFound

from microhttp_restful import MetadataField, Field
from microhttp_restful.validation import validate_form
Expand Down Expand Up @@ -119,7 +119,7 @@ def expose(cls, func):
def wrapper(*args, **kwargs):
result = super(BaseModel, cls).expose(func)(*args, **kwargs)
if result is None:
raise HttpNotFound
raise HTTPNotFound

return result

Expand Down
10 changes: 5 additions & 5 deletions microhttp_restful/field.py
@@ -1,7 +1,7 @@
import re
from sqlalchemy import Unicode, String
from sqlalchemy_dict import Field as SADictField
from nanohttp import HttpBadRequest
from nanohttp import HTTPBadRequest


# noinspection PyAbstractClass
Expand Down Expand Up @@ -64,24 +64,24 @@ def _validate_pattern(self, value):
if value is None:
return
if not re.match(self.info['pattern'], value):
raise HttpBadRequest('Cannot match field: %s with value "%s" by acceptable pattern' % (self.name, value))
raise HTTPBadRequest('Cannot match field: %s with value "%s" by acceptable pattern' % (self.name, value))
return value

def _validate_length(self, value, min_length, max_length):
if value is None:
return

if not isinstance(value, str):
raise HttpBadRequest('Invalid type: %s for field: %s' % (type(value), self.name))
raise HTTPBadRequest('Invalid type: %s for field: %s' % (type(value), self.name))

value_length = len(value)
if min_length is not None:
if value_length < min_length:
raise HttpBadRequest('Please enter at least %d characters for field: %s.' % (min_length, self.name))
raise HTTPBadRequest('Please enter at least %d characters for field: %s.' % (min_length, self.name))

if max_length is not None:
if value_length > max_length:
raise HttpBadRequest('Cannot enter more than: %d in field: %s.' % (max_length, self.name))
raise HTTPBadRequest('Cannot enter more than: %d in field: %s.' % (max_length, self.name))

def validate(self, value):
if 'pattern' in self.info:
Expand Down
10 changes: 5 additions & 5 deletions microhttp_restful/mixins/filtering.py
@@ -1,6 +1,6 @@
from sqlalchemy import between

from nanohttp import context, HttpBadRequest
from nanohttp import context, HTTPBadRequest


class FilteringMixin:
Expand All @@ -13,8 +13,8 @@ def filter_by_request(cls, query=None):
for c in cls.iter_dict_columns():
# noinspection PyUnresolvedReferences
json_name = cls.get_dict_key(c)
if json_name in context.query_string:
value = context.query_string[json_name]
if json_name in context.query:
value = context.query[json_name]
query = cls._filter_by_column_value(query, c, value)

return query
Expand All @@ -23,7 +23,7 @@ def filter_by_request(cls, query=None):
def _filter_by_column_value(cls, query, column, value):
import_value = getattr(cls, 'import_value')
if not isinstance(value, str):
raise HttpBadRequest()
raise HTTPBadRequest()

if value.startswith('^') or value.startswith('!^'):
value = value.split(',')
Expand All @@ -32,7 +32,7 @@ def _filter_by_column_value(cls, query, column, value):
items = [first_item] + value[1:]
items = [i for i in items if i.strip()]
if not len(items):
raise HttpBadRequest('Invalid query string: %s' % value)
raise HTTPBadRequest('Invalid query string: %s' % value)
expression = column.in_([import_value(column, j) for j in items])
if not_:
expression = ~expression
Expand Down
2 changes: 1 addition & 1 deletion microhttp_restful/mixins/ordering.py
Expand Up @@ -22,7 +22,7 @@ def sort_by_request(cls, query=None):
# noinspection PyUnresolvedReferences
query = query or cls.query

sort_exp = context.query_string.get('sort', '').strip()
sort_exp = context.query.get('sort', '').strip()
if not sort_exp:
return query

Expand Down
8 changes: 4 additions & 4 deletions microhttp_restful/mixins/pagination.py
@@ -1,4 +1,4 @@
from nanohttp import context, HttpBadRequest
from nanohttp import context, HTTPBadRequest


class PaginationMixin:
Expand All @@ -13,17 +13,17 @@ def paginate_by_request(cls, query=None):

try:
take = int(
context.query_string.get('take') or context.environ.get(cls.__take_header_key__) or cls.__max_take__)
context.query.get('take') or context.environ.get(cls.__take_header_key__) or cls.__max_take__)
except ValueError:
take = cls.__max_take__

try:
skip = int(context.query_string.get('skip') or context.environ.get(cls.__skip_header_key__) or 0)
skip = int(context.query.get('skip') or context.environ.get(cls.__skip_header_key__) or 0)
except ValueError:
skip = 0

if take > cls.__max_take__:
raise HttpBadRequest()
raise HTTPBadRequest()

context.response_headers.add_header('X-Pagination-Take', str(take))
context.response_headers.add_header('X-Pagination-Skip', str(skip))
Expand Down
4 changes: 2 additions & 2 deletions microhttp_restful/mixins/soft_delete.py
Expand Up @@ -3,7 +3,7 @@
from sqlalchemy import DateTime
from sqlalchemy.events import event

from nanohttp import HttpConflict
from nanohttp import HTTPConflict

from microhttp_restful import Field

Expand Down Expand Up @@ -35,7 +35,7 @@ def soft_undelete(self, ignore_errors=False):

@staticmethod
def before_delete(mapper, connection, target):
raise HttpConflict('Cannot remove this object: %s' % target)
raise HTTPConflict('Cannot remove this object: %s' % target)

@classmethod
def __declare_last__(cls):
Expand Down
8 changes: 4 additions & 4 deletions microhttp_restful/tests/test_filtering_mixin.py
Expand Up @@ -2,7 +2,7 @@

from sqlalchemy import Integer, Unicode

from nanohttp import HttpBadRequest
from nanohttp import HTTPBadRequest
from nanohttp.contexts import Context

from microhttp.ext import db
Expand Down Expand Up @@ -32,8 +32,8 @@ def test_filtering_mixin(self):

# Bad Value
with Context({'QUERY_STRING': 'id=1'}, self.application) as context:
context.query_string['id'] = 1
self.assertRaises(HttpBadRequest, FilteringObject.filter_by_request)
context.query['id'] = 1
self.assertRaises(HTTPBadRequest, FilteringObject.filter_by_request)

# IN
with Context({'QUERY_STRING': 'id=^1,2,3'}, self.application):
Expand All @@ -45,7 +45,7 @@ def test_filtering_mixin(self):

# IN (error)
with Context({'QUERY_STRING': 'id=^'}, self.application):
self.assertRaises(HttpBadRequest, FilteringObject.filter_by_request)
self.assertRaises(HTTPBadRequest, FilteringObject.filter_by_request)

# Between
with Context({'QUERY_STRING': 'id=~1,3'}, self.application):
Expand Down
10 changes: 5 additions & 5 deletions microhttp_restful/tests/test_orm.py
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy import Integer, Unicode, ForeignKey, Boolean, Table, Date, Time, Float
from sqlalchemy.orm import synonym

from nanohttp import HttpBadRequest
from nanohttp import HTTPBadRequest

from microhttp.ext import db
from microhttp_restful import ModifiedMixin, Field, composite, relationship
Expand Down Expand Up @@ -132,20 +132,20 @@ def test_orm(self):
self.assertEqual(post1.id, 1)

# Validation, Type
with self.assertRaises(HttpBadRequest):
with self.assertRaises(HTTPBadRequest):
Author(title=234)

# Validation, Pattern
with self.assertRaises(HttpBadRequest):
with self.assertRaises(HTTPBadRequest):
Author(email='invalidEmailAddress')

# Validation, Min length
with self.assertRaises(HttpBadRequest):
with self.assertRaises(HTTPBadRequest):
Author(title='1')

# Validation, Max length
# Validation, Max length
with self.assertRaises(HttpBadRequest):
with self.assertRaises(HTTPBadRequest):
Author(phone='12321321321312321312312')

# Metadata
Expand Down
4 changes: 2 additions & 2 deletions microhttp_restful/tests/test_pagination_mixin.py
Expand Up @@ -2,7 +2,7 @@

from sqlalchemy import Integer, Unicode

from nanohttp import HttpBadRequest
from nanohttp import HTTPBadRequest
from nanohttp.contexts import Context

from microhttp.ext import db
Expand Down Expand Up @@ -41,7 +41,7 @@ def test_pagination_mixin(self):
self.assertEqual(PagingObject.paginate_by_request().count(), 4)

with Context({'QUERY_STRING': 'take=5'}, self.application):
self.assertRaises(HttpBadRequest, PagingObject.paginate_by_request)
self.assertRaises(HTTPBadRequest, PagingObject.paginate_by_request)


if __name__ == '__main__': # pragma: no cover
Expand Down
4 changes: 2 additions & 2 deletions microhttp_restful/tests/test_soft_delete_mixin.py
Expand Up @@ -2,7 +2,7 @@

from sqlalchemy import Unicode

from nanohttp import HttpConflict
from nanohttp import HTTPConflict

from microhttp.ext import db
from microhttp_restful import SoftDeleteMixin, Field
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_soft_delete_mixin(self):
self.assertEqual(SoftDeleteCheckingModel.exclude_deleted().count(), 1)

db_session.delete(instance)
self.assertRaises(HttpConflict, db_session.commit)
self.assertRaises(HTTPConflict, db_session.commit)


if __name__ == '__main__': # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion microhttp_restful/tests/test_validation_blacklist.py
Expand Up @@ -17,7 +17,7 @@ class ValidationBlackListController(RestController):
admin={'blacklist': ['blacklistParamForAdmin']})
def post(self):
result = copy.deepcopy(context.form)
result.update(context.query_string)
result.update(context.query)
return result


Expand Down
2 changes: 1 addition & 1 deletion microhttp_restful/tests/test_validation_exact.py
Expand Up @@ -18,7 +18,7 @@ class ValidationController(RestController):
admin={'exact': ['exactParamForAdmin']})
def post(self):
result = copy.deepcopy(context.form)
result.update(context.query_string)
result.update(context.query)
return result


Expand Down
2 changes: 1 addition & 1 deletion microhttp_restful/tests/test_validation_exclude.py
Expand Up @@ -18,7 +18,7 @@ class ValidationExcludeController(RestController):
admin={'exclude': ['excludedParamForAdmin']})
def post(self):
result = copy.deepcopy(context.form)
result.update(context.query_string)
result.update(context.query)
return result


Expand Down
2 changes: 1 addition & 1 deletion microhttp_restful/tests/test_validation_filter.py
Expand Up @@ -20,7 +20,7 @@ class ValidationFilterController(RestController):
)
def post(self):
result = copy.deepcopy(context.form)
result.update(context.query_string)
result.update(context.query)
return result


Expand Down
2 changes: 1 addition & 1 deletion microhttp_restful/tests/test_validation_pattern.py
Expand Up @@ -35,7 +35,7 @@ class ValidationPatternController(RestController):
)
def post(self):
result = copy.deepcopy(context.form)
result.update(context.query_string)
result.update(context.query)
return result


Expand Down
2 changes: 1 addition & 1 deletion microhttp_restful/tests/test_validation_requires.py
Expand Up @@ -18,7 +18,7 @@ class ValidationRequiresController(RestController):
admin={'requires': ['requiresParamForAdmin']})
def post(self):
result = copy.deepcopy(context.form)
result.update(context.query_string)
result.update(context.query)
return result


Expand Down
2 changes: 1 addition & 1 deletion microhttp_restful/tests/test_validation_types.py
Expand Up @@ -34,7 +34,7 @@ class ValidationTypesController(RestController):
)
def post(self):
result = copy.deepcopy(context.form)
result.update(context.query_string)
result.update(context.query)
return result


Expand Down
2 changes: 1 addition & 1 deletion microhttp_restful/tests/test_validation_whitelist.py
Expand Up @@ -18,7 +18,7 @@ class ValidationWhitelistController(RestController):
admin={'whitelist': ['whitelistParamForAdmin']})
def post(self):
result = copy.deepcopy(context.form)
result.update(context.query_string)
result.update(context.query)
return result


Expand Down

0 comments on commit 5f0f8ec

Please sign in to comment.