-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d45e26d
commit 495635f
Showing
6 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .base import BaseModel | ||
|
||
__all__ = ['BaseModel'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '0.0.4.dev0' | ||
__version__ = '0.0.4.dev1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |