ets-labs / python-domain-models

Domain models framework for Python projects
BSD 3-Clause "New" or "Revised" License
22 stars 4 forks source link

DomainModel Views #24

Closed rmk135 closed 7 years ago

rmk135 commented 8 years ago

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:

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:

boonya commented 7 years ago

Seems like it is pretty similar to #25