The idea is to create easy way for specification of custom model views. Model views here are an algorithms that define how model's data should look like.
Case:
We have domain model of user's profile - Profile. Profile model has "public" and "private" views. "Public view" of Profile model is a set of data that could be shown to any other user without any restrictions, it contains less data then Profile model (doesn't contain private data). At the same time "Private view" of Profile model is a full set of Profile model data (may be even with some extra information).
from domain_models import models
from domain_models import fields
from domain_models import views
class Profile(models.DomainModel):
id = fields.Int()
name = fields.String()
birth_date = fields.Date()
business_address = fields.String()
home_address = fields.String()
public_view = views.View(include=[name, business_address])
private_view = views.View(include=[name, birth_date, business_address, home_address])
profile = Profile(id=1,
name='John',
birth_date=datetime.date(1950, 4, 18),
business_address='John works here',
home_address='John lives here')
assert profile.public_view() == {
'name': 'John',
'business_address': 'John works here'
}
assert profile.private_view() == {
'name': 'John',
'birth_date': datetime.date(1950, 4, 18),
'business_address': 'John works here',
'home_address': 'John lives here'
}
Notes:
views.View should have two arguments - include and exclude, that are sequences (list, tuple, set, etc...) of model fields.
views.View arguments include and exclude could not be specified simultaneously.
The idea is to create easy way for specification of custom model views. Model views here are an algorithms that define how model's data should look like.
Case:
Profile
.Profile
model has "public" and "private" views. "Public view" ofProfile
model is a set of data that could be shown to any other user without any restrictions, it contains less data thenProfile
model (doesn't contain private data). At the same time "Private view" ofProfile
model is a full set ofProfile
model data (may be even with some extra information).Notes:
views.View
should have two arguments -include
andexclude
, that are sequences (list
,tuple
,set
, etc...) of model fields.views.View
argumentsinclude
andexclude
could not be specified simultaneously.