コード例 #1
0
class CommentRepresentation:
    id = Identifier()
    body = Text(required=True)
    created_at = DateTime()
    updated_at = DateTime()

    author = Nested(UserRepresentation, required=True)
コード例 #2
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)
コード例 #3
0
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)
コード例 #4
0
    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)
コード例 #5
0
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
コード例 #6
0
ファイル: elements.py プロジェクト: advishnuprasad/protean
class CommentVia(BaseEntity):
    content = Text()
    added_on = DateTime()
    posting_id = String()

    class Meta:
        aggregate_cls = PostVia
コード例 #7
0
ファイル: elements.py プロジェクト: advishnuprasad/protean
class CommentViaWithReference(BaseEntity):
    content = Text()
    added_on = DateTime()
    posting = Reference('tests.aggregate.elements.PostVia')

    class Meta:
        aggregate_cls = PostViaWithReference
コード例 #8
0
ファイル: elements.py プロジェクト: advishnuprasad/protean
class Comment(BaseEntity):
    content = Text(required=True)
    commented_at = DateTime(required=True, default=datetime.now())

    post = Reference(Post)

    class Meta:
        aggregate_cls = Post
コード例 #9
0
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")
コード例 #10
0
ファイル: elements.py プロジェクト: nadirhamid/protean
class Comment(BaseEntity):
    content = Text()
    added_on = DateTime()

    post = Reference("tests.aggregate.elements.Post")

    class Meta:
        aggregate_cls = Post
コード例 #11
0
ファイル: elements.py プロジェクト: advishnuprasad/protean
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')
コード例 #12
0
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")
コード例 #13
0
    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')
コード例 #14
0
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
コード例 #15
0
class Event(BaseAggregate):
    name = String(max_length=255)
    created_at = DateTime(default=datetime.utcnow())
    payload = Dict()
コード例 #16
0
ファイル: elements.py プロジェクト: advishnuprasad/protean
class Role(BaseAggregate):
    name = String(max_length=15, required=True)
    created_on = DateTime(default=datetime.today())
コード例 #17
0
ファイル: elements.py プロジェクト: abhirathmahipal/protean
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())
コード例 #18
0
class Follower:
    following = Reference(User, required=True)
    user = Reference(User, required=True)
    followed_on = DateTime(required=True, default=datetime.now())
コード例 #19
0
class TagsAdded:
    tag_list = List()
    added_at = DateTime(default=datetime.now())
コード例 #20
0
class NewTagsCommand:
    tag_list = List()
    added_at = DateTime(default=datetime.now())
コード例 #21
0
class Favorite:
    article = Reference('realworld.model.article.Article', required=True)
    user = Reference(User, required=True)
    favorited_on = DateTime(required=True, default=datetime.now())
コード例 #22
0
class EventLog(BaseAggregate):
    kind = String(max_length=50, required=True)
    payload = Dict(required=True)
    timestamp = DateTime(required=True, default=datetime.utcnow)
コード例 #23
0
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