class CommentRepresentation: id = Identifier() body = Text(required=True) created_at = DateTime() updated_at = DateTime() author = Nested(UserRepresentation, required=True)
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 Comment: body = Text(required=True) created_at = DateTime(default=datetime.now()) updated_at = DateTime(default=datetime.now()) article = Reference(Article, required=True) author = Reference(User, required=True)
def test_init(self): """Test successful DateTime Field initialization""" created_at = DateTime() assert created_at is not None value = datetime.now() assert value == created_at._load(value)
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 CommentVia(BaseEntity): content = Text() added_on = DateTime() posting_id = String() class Meta: aggregate_cls = PostVia
class CommentViaWithReference(BaseEntity): content = Text() added_on = DateTime() posting = Reference('tests.aggregate.elements.PostVia') class Meta: aggregate_cls = PostViaWithReference
class Comment(BaseEntity): content = Text(required=True) commented_at = DateTime(required=True, default=datetime.now()) post = Reference(Post) class Meta: aggregate_cls = Post
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 Comment(BaseEntity): content = Text() added_on = DateTime() post = Reference("tests.aggregate.elements.Post") class Meta: aggregate_cls = Post
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 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")
def test_type_casting(self): """ Test type casting and validation for the Field""" created_at = DateTime() today = datetime.now() # Test date being passed as value assert created_at._load(today.date()) == datetime( today.year, today.month, today.day) # Test string dates being passed as value assert created_at._load('2018-03-16') == datetime(2018, 3, 16) assert created_at._load('2018-03-16 10:23:32') == datetime( 2018, 3, 16, 10, 23, 32) # Test for invalid datetime with pytest.raises(ValidationError): assert created_at._load('2018-03-16 10 23 32')
class Article: 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()) author = Reference(User, required=True) comments = HasMany('realworld.model.article.Comment') @classmethod def create(self, article_dto: CreateArticleDTO): article = Article(title=article_dto.title, slug=slugify(article_dto.title), description=article_dto.description, body=article_dto.body, tag_list=article_dto.tag_list, author=article_dto.author) current_domain.publish(TagsAdded(tag_list=article_dto.tag_list)) return article def update(self, **kwargs): valid_fields = [ field for field in kwargs if field in ['title', 'description', 'body'] ] for field in valid_fields: setattr(self, field, kwargs[field]) if 'title' in valid_fields: setattr(self, 'slug', slugify(self.title)) ################### # Comment methods # ################### def add_comment(self, body: String, logged_in_user: User): new_comment = Comment(body=body, article=self, author=logged_in_user) self.comments.add(new_comment) return self, new_comment def delete_comment(self, comment_identifier): [old_comment] = [ comment for comment in self.comments if comment.id == comment_identifier ] self.comments.remove(old_comment) return self, old_comment def get_comment_by_identifier(self, comment_identifier): [comment] = [ comment for comment in self.comments if comment.id == comment_identifier ] return comment
class Event(BaseAggregate): name = String(max_length=255) created_at = DateTime(default=datetime.utcnow()) payload = Dict()
class Role(BaseAggregate): name = String(max_length=15, required=True) created_on = DateTime(default=datetime.today())
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 Follower: following = Reference(User, required=True) user = Reference(User, required=True) followed_on = DateTime(required=True, default=datetime.now())
class TagsAdded: tag_list = List() added_at = DateTime(default=datetime.now())
class NewTagsCommand: tag_list = List() added_at = DateTime(default=datetime.now())
class Favorite: article = Reference('realworld.model.article.Article', required=True) user = Reference(User, required=True) favorited_on = DateTime(required=True, default=datetime.now())
class EventLog(BaseAggregate): kind = String(max_length=50, required=True) payload = Dict(required=True) timestamp = DateTime(required=True, default=datetime.utcnow)
class User: email = String(required=True, max_length=250) username = String(required=True, max_length=50) password = String(required=True, max_length=255) bio = String(max_length=1024) token = String(max_length=1024) token_valid_until = DateTime() image = String(max_length=1024) follows = HasMany('realworld.model.user.Follower', via='user_id') followers = HasMany('realworld.model.user.Follower', via='following_id') favorites = HasMany('realworld.model.user.Favorite', via='user_id') @classmethod def register(self, user_dto: UserRegistrationDTO): return User(email=user_dto.email, username=user_dto.username, password=user_dto.password) def authenticate(self, password: str): return password == self.password def refresh_token(self): token = generate_token(self.id) self.token = token self.token_valid_until = datetime.now() + timedelta(days=1) def update(self, **kwargs): valid_fields = [ field for field in kwargs if field in ['email', 'username', 'password', 'image', 'bio']] for field in valid_fields: setattr(self, field, kwargs[field]) #################### # Follower methods # #################### def follow(self, user: '******'): if user not in [follow_object.following for follow_object in self.follows]: self.follows.add(Follower(following=user, user=self)) return self def unfollow(self, user: '******'): [follow_object] = [ follow_object for follow_object in self.follows if follow_object.following == user] self.follows.remove(follow_object) return self ###################### # Favoriting methods # ###################### def favorite(self, article): if article not in [favorite.article for favorite in self.favorites]: self.favorites.add(Favorite(article=article, user=self)) return self def unfavorite(self, article): try: [favorited_article] = [ favorite for favorite in self.favorites if favorite.article == article] except ValueError: favorited_article = None if favorited_article: self.favorites.remove(favorited_article) return self