class OrderedPerson(BaseEntity): first_name = String(max_length=50, required=True) last_name = String(max_length=50) age = Integer(default=21) class Meta: order_by = "first_name"
class PersonSQLite(BaseAggregate): first_name = String(max_length=50, required=True) last_name = String(max_length=50, required=True) age = Integer(default=21) class Meta: provider = "sqlite"
class DbPerson(BaseEntity): first_name = String(max_length=50, required=True) last_name = String(max_length=50) age = Integer(default=21) class Meta: schema_name = "pepes"
class ListArticlesCommand: token = String(max_length=1024) tag = String(max_length=50) author = String(max_length=50) favorited = String(max_length=50) limit = Integer(default=20) offset = Integer(default=0)
class ArticleDTO: slug = String(max_length=250) title = String(required=True, max_length=250) description = Text(required=True) body = Text(required=True) tag_list = List() created_at = DateTime(default=datetime.now()) updated_at = DateTime(default=datetime.now()) favorited = Boolean(default=False) favorites_count = Integer() @classmethod def for_article(cls, article: Article, user: User): favorited = article in [ favorite.article for favorite in user.favorites ] return ArticleDTO(slug=article.slug, title=article.title, description=article.description, body=article.body, tag_list=article.tag_list, created_at=article.created_at, updated_at=article.updated_at, author=article.author, favorited=favorited)
class Post(BaseAggregate): title = String(required=True, max_length=1000) slug = String(required=True, max_length=1024) content = Text(required=True) posted_at = DateTime(required=True, default=datetime.now()) post_meta = HasOne("tests.repository.child_entities.PostMeta") comments = HasMany("tests.repository.child_entities.Comment")
class Post(BaseAggregate): title = String(required=True, max_length=1000) slug = String(required=True, max_length=1024) content = Text(required=True) posted_at = DateTime(required=True, default=datetime.now()) meta = HasOne("tests.unit_of_work.aggregate_elements.PostMeta") comments = HasMany("tests.unit_of_work.aggregate_elements.Comment")
class Author(BaseEntity): first_name = String(required=True, max_length=25) last_name = String(max_length=25) posts = HasMany('tests.aggregate.elements.Post') account = Reference('tests.aggregate.elements.Account') class Meta: aggregate_cls = Account
class Post(BaseAggregate): title = String(required=True, max_length=1000) slug = String(required=True, max_length=1024) content = Text(required=True) posted_at = DateTime(required=True, default=datetime.now()) meta = HasOne('PostMeta') comments = HasMany('Comment')
class UserRepresentation: id = Identifier() email = String(required=True, max_length=250) username = String(required=True, max_length=50) bio = String(max_length=1024) token = Method('token_only_if_active') def token_only_if_active(self, user): if user.token_valid_until and user.token_valid_until > datetime.now(): return user.token else: return None
class Person(BaseAggregate): first_name = String(max_length=50, required=True) last_name = String(max_length=50, required=True) age = Integer(default=21) @classmethod def add_newcomer(cls, person_dict): """Factory method to add a new Person to the system""" newcomer = Person( first_name=person_dict['first_name'], last_name=person_dict['last_name'], age=person_dict['age'], ) return newcomer
class CreateArticleDTO: title = String(required=True, max_length=250) description = Text(required=True) body = Text(required=True) tag_list = List() author = CustomObject(User, required=True)
class ProfileDTO: username = String(required=True, max_length=50) bio = String(max_length=1024) image = String(max_length=1024) following = Boolean(default=False) @classmethod def for_user(cls, user: User, profile_user: User): following = profile_user in [item.following for item in user.follows] return ProfileDTO( username=profile_user.username, bio=profile_user.bio, image=profile_user.image, following=following )
class CommentVia(BaseEntity): content = Text() added_on = DateTime() posting_id = String() class Meta: aggregate_cls = PostVia
class Email(BaseValueObject): """An email address value object, with two clearly identified parts: * local_part * domain_part """ # This is the external facing data attribute address = String(max_length=254, required=True) @classmethod def from_address(cls, address): """ Construct an Email VO from an email address. email = Email.from_address('*****@*****.**') """ if not cls.validate(address): raise ValueError("Email address is invalid") return cls(address=address) @classmethod def validate(cls, address): """ Business rules of Email address """ if ( type(address) is not str or "@" not in address or address.count("@") > 1 or len(address) > 255 ): return False return True
class Building(BaseValueObject): name = String(max_length=50) floors = Integer() status = String(choices=BuildingStatus) def defaults(self): if not self.status: if self.floors == 4: self.status = BuildingStatus.DONE.value else: self.status = BuildingStatus.WIP.value def clean(self): errors = defaultdict(list) if self.floors >= 4 and self.status != BuildingStatus.DONE.value: errors["status"].append("should be DONE") return errors
class Person(BaseAggregate): first_name = String(max_length=50, required=True) last_name = String(max_length=50, required=True) age = Integer(default=21) @classmethod def add_newcomer(cls, person_dict): """Factory method to add a new Person to the system""" newcomer = Person( first_name=person_dict['first_name'], last_name=person_dict['last_name'], age=person_dict['age'], ) # Publish Event via the domain current_domain.publish(PersonAdded(person=newcomer)) return newcomer
def test_choice(self): """ Test choices validations for the string field """ class StatusChoices(enum.Enum): """ Set of choices for the status""" PENDING = 'Pending' SUCCESS = 'Success' ERROR = 'Error' status = String(max_length=10, choices=StatusChoices) assert status is not None # Test loading of values to the status field assert status._load('Pending') == 'Pending' with pytest.raises(ValidationError) as e_info: status._load('Failure') assert e_info.value.messages == { 'unlinked': [ "Value `'Failure'` is not a valid choice. " "Must be one of ['Pending', 'Success', 'Error']" ] }
class UserUpdateCommand: token = String(required=True, max_length=1024) email = String(max_length=250) username = String(max_length=50) password = String(max_length=255) bio = String(max_length=1024) image = String(max_length=1024)
class Tag: name = String(required=True, max_length=50) added_at = DateTime(default=datetime.now()) last_seen_at = DateTime(default=datetime.now()) @classmethod def create(self, tag_name, added_at=datetime.now()): return Tag(name=tag_name.lower(), added_at=added_at, last_seen_at=added_at) def touch(self, updated_at=datetime.now()): self.last_seen_at = updated_at return self
class Email(BaseValueObject): REGEXP = r'\"?([-a-zA-Z0-9.`?{}]+@\w+\.\w+)\"?' # This is the external facing data attribute address = String(max_length=254, required=True) def clean(self): """ Business rules of Email address """ errors = defaultdict(list) if not bool(re.match(Email.REGEXP, self.address)): errors['address'].append('is invalid') return errors
class Balance(BaseValueObject): """A composite amount object, containing two parts: * currency code - a three letter unique currency code * amount - a float value """ currency = String(max_length=3, choices=Currency) amount = Float() def clean(self): errors = defaultdict(list) if self.amount and self.amount < -1000000000000.0: errors["amount"].append("cannot be less than 1 Trillion") return errors def replace(self, **kwargs): # FIXME Find a way to do this generically and move method to `BaseValueObject` currency = kwargs.pop("currency", None) amount = kwargs.pop("amount", None) return Balance(currency=currency or self.currency, amount=amount or self.amount)
class Person(BaseAggregate): first_name = String(max_length=50, required=True) last_name = String(max_length=50, required=True) age = Integer(default=21)
class EventLog(BaseAggregate): kind = String(max_length=50, required=True) payload = Dict(required=True) timestamp = DateTime(required=True, default=datetime.utcnow)
class User(BaseAggregate): email = String(max_length=255, required=True, unique=True) password = String(max_length=3026)
class Person(BaseAggregate): first_name = String(max_length=50, required=True) last_name = String(max_length=50, required=True) age = Integer(default=21) created_at = DateTime(default=datetime.now())
class UserRegistrationCommand: email = String(required=True, max_length=250) username = String(required=True, max_length=50) password = String(required=True, max_length=255)
class ChangePasswordCommand: old_password = String(required=True, max_length=255) new_password = String(required=True, max_length=255)
class ComplexUser(BaseAggregate): email = ValueObjectField(Email, required=True) password = String(required=True, max_length=255)
class OrderedRoleSubclass(Role): bar = String(max_length=25) class Meta: order_by = 'bar'