Skip to content

Commit

Permalink
Merge pull request #86 from proteanhq/75-associations
Browse files Browse the repository at this point in the history
75 HasOne and HasMany Associations
  • Loading branch information
abhishek-ram authored Feb 28, 2019
2 parents df0f9bf + 934a66b commit b180cf6
Show file tree
Hide file tree
Showing 13 changed files with 790 additions and 84 deletions.
24 changes: 18 additions & 6 deletions src/protean/core/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from protean.core.exceptions import ObjectNotFoundError
from protean.core.exceptions import ValidationError
from protean.core.field import Auto
from protean.core.field import Field, Reference
from protean.core.field import Field, Reference, ReferenceField
from protean.utils.generic import classproperty
from protean.utils.query import Q

Expand Down Expand Up @@ -48,11 +48,14 @@ def __new__(mcs, name, bases, attrs, **kwargs):
# Load declared fields from Base class, in case this Entity is subclassing another
new_class._load_base_class_fields(bases, attrs)

# Lookup an already defined ID field or create an `Auto` field
new_class._set_id_field()

# Set up Relation Fields
new_class._set_up_reference_fields()

# Lookup an already defined ID field or create an `Auto` field
new_class._set_id_field()
# Load list of Attributes from declared fields, depending on type of fields
new_class._load_attributes()

# Construct an empty QuerySet associated with this Entity class
new_class.query = QuerySet(name)
Expand Down Expand Up @@ -119,6 +122,11 @@ def _create_id_field(new_class):
new_class._meta.declared_fields['id'] = id_field
new_class._meta.id_field = id_field

def _load_attributes(new_class):
"""Load list of attributes from declared fields"""
for field_name, field_obj in new_class._meta.declared_fields.items():
new_class._meta.attributes[field_obj.get_attribute_name()] = field_obj


class EntityMeta:
""" Metadata information for the entity including any options defined."""
Expand All @@ -129,6 +137,7 @@ def __init__(self, meta):
# Initialize Options
self.entity_cls = None
self.declared_fields = {}
self.attributes = {}
self.id_field = None

@property
Expand Down Expand Up @@ -488,9 +497,7 @@ def __init__(self, *template, **kwargs):
# for required fields
for field_name, field_obj in self._meta.declared_fields.items():
if field_name not in loaded_fields:
# Check that the field is not set already, which would happen if we are
# dealing with reference fields
if getattr(self, field_name, None) is None:
if not isinstance(field_obj, (Reference, ReferenceField)):
setattr(self, field_name, None)

# Raise any errors found during load
Expand Down Expand Up @@ -558,6 +565,11 @@ def declared_fields(cls):
"""Pass through method to retrieve declared fields defined for entity"""
return cls._meta.declared_fields

@classproperty
def attributes(cls):
"""Pass through method to retrieve attributes defined for entity"""
return cls._meta.attributes

@classproperty
def auto_fields(cls):
"""Pass through method to retrieve `Auto` fields defined for entity"""
Expand Down
4 changes: 2 additions & 2 deletions src/protean/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class ObjectNotFoundError(Exception):
"""Object was not found, can raise 404"""


class ValueError(Exception):
"""Object of incorrect type, or with invalid state was assigned"""
class NotSupportedError(Exception):
"""Object does not support the operation being performed"""


class ValidationError(Exception):
Expand Down
4 changes: 2 additions & 2 deletions src/protean/core/field/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Package for defining Field type and its implementations"""

from .association import Reference
from .association import Reference, ReferenceField
from .base import Field
from .basic import Auto
from .basic import Boolean
Expand All @@ -18,4 +18,4 @@

__all__ = ('Field', 'String', 'Boolean', 'Integer', 'Float', 'List', 'Dict',
'Auto', 'Date', 'DateTime', 'Text', 'StringShort', 'StringMedium',
'StringLong', 'Reference')
'StringLong', 'Reference', 'ReferenceField')
Loading

0 comments on commit b180cf6

Please sign in to comment.