Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
timsavage committed Oct 24, 2017
2 parents ded150b + 5ad7f47 commit d71d7f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions odin/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import copy
import six

# Typing imports
from typing import TypeVar, Dict, Any # noqa

from odin import bases
from odin import exceptions, registration
from odin.exceptions import ValidationError
Expand Down Expand Up @@ -470,8 +473,9 @@ def full_clean(self, exclude=None):

def clean_fields(self, exclude=None):
errors = {}
meta = getmeta(self)

for f in getmeta(self).fields:
for f in meta.fields:
if exclude and f.name in exclude:
continue

Expand All @@ -493,7 +497,8 @@ def clean_fields(self, exclude=None):
except ValidationError as e:
errors.setdefault(f.name, []).extend(e.messages)

setattr(self, f.attname, raw_value)
if f not in meta.readonly_fields:
setattr(self, f.attname, raw_value)

if errors:
raise ValidationError(errors)
Expand Down Expand Up @@ -562,7 +567,11 @@ def create_resource_from_iter(i, resource, full_clean=True, default_to_not_provi
return new_resource


R = TypeVar("R")


def create_resource_from_dict(d, resource=None, full_clean=True, copy_dict=True, default_to_not_provided=False):
# type: (Dict[str, Any], R, bool, bool, bool) -> Instance[R]
"""
Create a resource from a dict.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from odin.resources import create_resource_from_dict
from odin.utils import getmeta
from odin import proxy

Expand Down Expand Up @@ -144,3 +145,8 @@ def test_extra_property(self):
target = BookProxy.proxy(book)

assert target.expensive

def test_create_resource_from_dict(self):
actual = create_resource_from_dict({'title': '1984', 'isbn': '1234567', 'num_pages': 1}, BookProxy)

assert actual.title == '1984'

0 comments on commit d71d7f2

Please sign in to comment.