Skip to content

Commit

Permalink
Merge pull request #34 from xethorn/mo-16-sept-new-response
Browse files Browse the repository at this point in the history
Migrate Sukimu Response to Oto
  • Loading branch information
xethorn authored Sep 28, 2016
2 parents 3eb3b1b + ac7e971 commit eca77fa
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 289 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: python
python:
- "3.5"
- "3.4"
- "3.3"

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
boto==2.35.2
oto==1.0.1
py==1.4.31
pytest==3.0.2
pytest-cov==2.3.1
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='Sukimu',
version='0.0.7',
version='1.0.1',
url='https://github.com/xethorn/sukimu',
author='Michael Ortali',
author_email='github@xethorn.net',
Expand All @@ -17,4 +17,5 @@
classifiers=[
'Development Status :: Alpha',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
],)
3 changes: 3 additions & 0 deletions sukimu/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@

SORT_ASCENDING = 2
SORT_DESCENDING = 1

ERROR_CODE_VALIDATION = 'validation_errors'
ERROR_CODE_DUPLICATE_KEY = 'duplicate_key'
29 changes: 11 additions & 18 deletions sukimu/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"""

from boto.dynamodb2 import table
from oto import response
from oto import status

from sukimu import consts
from sukimu import operations
from sukimu import response
from sukimu import schema


Expand Down Expand Up @@ -72,7 +73,7 @@ def update(self, item, data):
Response: The response of the update.
"""

if not item.success:
if not item:
return item

item = item.message
Expand All @@ -81,7 +82,7 @@ def update(self, item, data):

save = item.partial_save()
return response.Response(
status=response.Status.OK if save else response.Status.ERROR,
status=status.OK if save else status.ACCEPTED,
message=item)

def delete(self, item):
Expand All @@ -95,8 +96,7 @@ def delete(self, item):

deleted = item.delete()
return response.Response(
status=response.Status.OK if deleted else response.Status.ERROR,
message=None)
status=status.OK if deleted else status.BAD_REQUEST)

def fetch(self, query, sort=None, limit=None, index=None):
"""Fetch one or more entries.
Expand Down Expand Up @@ -153,11 +153,10 @@ def fetch(self, query, sort=None, limit=None, index=None):

if not len(dynamo):
return response.Response(
status=response.Status.NOT_FOUND,
status=status.NOT_FOUND,
message=[])

return response.Response(
status=response.Status.OK,
message=[obj for obj in dynamo])

def fetch_many(self, key, values, index=None):
Expand All @@ -179,13 +178,11 @@ def fetch_many(self, key, values, index=None):
self.fetch_one(
index=index, **{key: operations.Equal(value)}).message)

status = response.Status.OK
data_status = status.OK
if not message:
status = response.Status.NOT_FOUND
data_status = status.NOT_FOUND

return response.Response(
status=response.Status.OK,
message=message)
return response.Response(status=data_status, message=message)

def fetch_one(self, index=None, **query):
"""Get one item.
Expand All @@ -200,9 +197,7 @@ def fetch_one(self, index=None, **query):
if not found, the status is set to NOT_FOUND.
"""

default_response = response.Response(
status=response.Status.NOT_FOUND,
message=None)
default_response = response.Response(status=status.NOT_FOUND)
field_names = list(query.keys())

required = 1
Expand Down Expand Up @@ -231,9 +226,7 @@ def fetch_one(self, index=None, **query):
item = data[0]

if item:
return response.Response(
status=response.Status.OK,
message=item)
return response.Response(message=item)

return default_response

Expand Down
104 changes: 0 additions & 104 deletions sukimu/response.py

This file was deleted.

56 changes: 25 additions & 31 deletions sukimu/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ def profile(obj):
from copy import deepcopy
from threading import Thread

from oto import response
from oto import status

from sukimu import consts
from sukimu import exceptions
from sukimu import operations
from sukimu import response
from sukimu import utils


Expand Down Expand Up @@ -123,7 +126,7 @@ def validate(self, values, operation):
items = set(values.keys())

if operation is operations.READ and not values:
return response.create_success_response()
return response.Response()

if operation is operations.CREATE:
items = set(self.fields.keys())
Expand All @@ -147,14 +150,11 @@ def validate(self, values, operation):
errors[name] = e
status = False

status = response.Status.OK
if errors:
status = response.Status.INVALID_FIELDS
return response.create_error_response(
consts.ERROR_CODE_VALIDATION, errors)

return response.Response(
status=status,
message=data,
errors=errors)
return response.Response(message=data)

def ensure_indexes(self, validation_response, current=None):
"""Ensure index unicity.
Expand All @@ -176,7 +176,7 @@ def ensure_indexes(self, validation_response, current=None):
Response: The response
"""

if not validation_response.success:
if not validation_response:
return validation_response

data = validation_response.message
Expand All @@ -202,19 +202,16 @@ def ensure_indexes(self, validation_response, current=None):
continue

ancestor = self.fetch_one(**query)
if ancestor.success:
if ancestor:
if not current or dict(ancestor.message) != dict(current):
errors.update({
key: exceptions.FIELD_ALREADY_USED for key in keys})

status = response.Status.OK
if errors:
status = response.Status.FIELD_VALUE_ALREADY_USED
return response.create_error_response(
consts.ERROR_CODE_DUPLICATE_KEY, errors)

return response.Response(
message=None,
status=status,
errors=errors)
return response.Response()

def generated(self, **dependencies):
"""Register a generated field.
Expand Down Expand Up @@ -263,12 +260,12 @@ def fetch(self, fields=None, limit=None, sort=None, index=None,
"""

validation_response = self.validate(query, operation=operations.READ)
if not validation_response.success:
if not validation_response:
return validation_response

schema_response = self.table.fetch(
query, sort=sort, limit=limit, index=index)
if schema_response.success and fields:
if schema_response and fields:
self.decorate_response(schema_response, fields, context=context)

return schema_response
Expand All @@ -287,11 +284,11 @@ def fetch_one(self, fields=None, context=None, **query):

validation_response = self.validate(query, operation=operations.READ)

if not validation_response.success:
if not validation_response:
return validation_response

schema_response = self.table.fetch_one(**query)
if schema_response.success and fields:
if schema_response and fields:
self.decorate_response(schema_response, fields, context=context)

return schema_response
Expand Down Expand Up @@ -381,17 +378,15 @@ def create(self, **data):
"""

validation = self.validate(data, operation=operations.CREATE)
if not validation.success:
if not validation:
return validation

check = self.ensure_indexes(validation)
if not check.success:
if not check:
return check

data = self.table.create(validation.message)
return response.Response(
message=data,
status=response.Status.OK)
return response.Response(message=data)

def update(self, source, **data):
"""Update the model from the data passed.
Expand All @@ -403,21 +398,20 @@ def update(self, source, **data):

data = utils.key_exclude(data, source.keys())
data = self.validate(data, operation=operations.READ)
if not data.success:
if not data:
return data

# Recreate the object - check ancestors.
current = self.fetch_one(**{
key: operations.Equal(val) for key, val in source.items()})
if not current.success:
if not current:
return current

fields = response.Response(
message=dict(list(source.items()) + list(data.message.items())),
status=response.Status.OK)
message=dict(list(source.items()) + list(data.message.items())))

ancestors = self.ensure_indexes(fields, current.message)
if not ancestors.success:
if not ancestors:
return ancestors

return self.table.update(current, fields.message)
Expand All @@ -427,7 +421,7 @@ def delete(self, **source):
"""

item = self.fetch_one(**source)
if not item.success:
if not item:
return item

return self.table.delete(item.message)
Expand Down
Loading

0 comments on commit eca77fa

Please sign in to comment.