Skip to content

Commit

Permalink
base model
Browse files Browse the repository at this point in the history
  • Loading branch information
felipao-mx committed Sep 19, 2021
1 parent d45e26d commit 495635f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
3 changes: 3 additions & 0 deletions fast_agave/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .base import BaseModel

__all__ = ['BaseModel']
23 changes: 23 additions & 0 deletions fast_agave/models/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import ClassVar, Dict

from mongoengine_plus.models.helpers import mongo_to_dict


class BaseModel:
_excluded: ClassVar = []
_hidden: ClassVar = []

def __init__(self, *args, **values):
return super().__init__(*args, **values)

def to_dict(self) -> Dict:
private_fields = [f for f in dir(self) if f.startswith('_')]
excluded = self._excluded + private_fields
mongo_dict: dict = mongo_to_dict(self, excluded)

for field in self._hidden:
mongo_dict[field] = '********'
return mongo_dict

def __repr__(self) -> str:
return str(self.to_dict()) # pragma: no cover
2 changes: 1 addition & 1 deletion fast_agave/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.4.dev0'
__version__ = '0.0.4.dev1'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
install_requires=[
'cuenca-validations>=0.9.4,<1.0.0',
'fastapi>=0.63.0,<1.0.0',
'mongoengine-plus>=0.0.2,<1.0.0',
'mongoengine-plus>=0.0.2,<0.1.0',
'starlette-context>=0.3.2,<0.4.0',
],
classifiers=[
Expand Down
Empty file added tests/models/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions tests/models/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from enum import Enum

from mongoengine import Document, StringField
from mongoengine_plus.types import EnumField

from fast_agave.models import BaseModel


class ModelType(Enum):
test = 'test'


class TestModel(BaseModel, Document):
id = StringField()
secret_field = StringField()
type = EnumField(ModelType)
__test__ = False
_hidden = ['secret_field']


def test_hide_field():
model = TestModel(id='12345', secret_field='secret', type=ModelType.test)
model_dict = model.to_dict()
assert model_dict['secret_field'] == '********'
assert model_dict['id'] == '12345'
assert model_dict['type'] == 'test'

0 comments on commit 495635f

Please sign in to comment.