예제 #1
0
 def test_is_correct_value_datetime_should_return_true(self):
     # given
     column = Column('datetime_column', ColumnType.DATETIME)
     # when
     result = column.is_correct_value_type(datetime.now())
     # then
     self.assertTrue(result)
예제 #2
0
 def test_is_correct_value_string_should_return_true(self):
     # given
     column = Column('string_column', ColumnType.STRING)
     # when
     result = column.is_correct_value_type('test')
     # then
     self.assertTrue(result)
예제 #3
0
 def test_is_correct_value_datetime_should_return_false(self):
     # given
     column = Column('datetime_column', ColumnType.DATETIME)
     # when
     result = column.is_correct_value_type('test')
     # then
     self.assertFalse(result)
예제 #4
0
class ModelExample(Base):
    columns = [
        Column('id', ColumnType.STRING, primary_key=True),
        Column('name', ColumnType.STRING)
    ]

    def __init__(self, name):
        self._id = None
        self._name = name

    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, id):
        self._id = id

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, name):
        self._name = name
예제 #5
0
class ModelExample(Base):
    """ Class used for tests """
    model_name = 'example'
    columns = [
        Column('id', ColumnType.STRING, primary_key=True),
        Column('name', ColumnType.STRING),
        Column('creation_date', ColumnType.DATETIME)
    ]
예제 #6
0
class ModelExample(Base):
    """ Class used for tests """
    model_name = 'example'
    columns = [
        Column('name', ColumnType.STRING)
    ]

    def __init__(self, **kwargs):
        super(ModelExample, self).__init__(**kwargs)
        self._name = kwargs.get('name')

    @property
    def name(self):
        return self._name
예제 #7
0
 def test_init_should_set_instance_variables(self):
     column = Column('custom_name', ColumnType.STRING)
     self.assertEqual(column.column_name, 'custom_name')
     self.assertEqual(column.column_type, ColumnType.STRING)
예제 #8
0
class Idea(Base):
    """This describes the model for an Idea """

    columns = [
        Column('id', ColumnType.STRING, primary_key=True),
        Column('discussion_id', ColumnType.STRING, foreign_key=True),
        Column('parent_idea_id', ColumnType.STRING,
               foreign_key=True),  # Adjacency list relationship
        Column('creator_id', ColumnType.STRING, foreign_key=True),
        Column('title', ColumnType.STRING),
        Column('description', ColumnType.STRING),
        Column('creation_date', ColumnType.DATETIME),
    ]

    def __init__(self,
                 discussion_id,
                 title,
                 description,
                 creator_id,
                 parent_idea_id=None,
                 creation_date=datetime.now(),
                 **kwargs):
        super(Idea, self).__init__(**kwargs)
        self._id = None
        self._discussion_id = discussion_id
        self._creator_id = creator_id
        self._title = title
        self._description = description
        self._parent_idea_id = parent_idea_id
        self._creation_date = creation_date
        self._ideas = []

    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, id):
        self._id = id

    @property
    def discussion_id(self):
        return self._discussion_id

    @discussion_id.setter
    def discussion_id(self, discussion_id):
        self._discussion_id = discussion_id

    @property
    def title(self):
        return self._title

    @title.setter
    def title(self, title):
        self._title = title

    @property
    def description(self):
        return self._description

    @description.setter
    def description(self, description):
        self._description = description

    @property
    def parent_idea_id(self):
        return self._parent_idea_id

    @parent_idea_id.setter
    def parent_idea_id(self, parent_idea_id):
        self._parent_idea_id = parent_idea_id

    @property
    def creator_id(self):
        return self._creator_id

    @creator_id.setter
    def creator_id(self, creator_id):
        self._creator_id = creator_id

    @property
    def creation_date(self):
        return self._creation_date

    @creation_date.setter
    def creation_date(self, creation_date):
        self._creation_date = creation_date

    @property
    def ideas(self):
        return self._ideas

    @ideas.setter
    def ideas(self, ideas):
        self._ideas = ideas

    @permissions_check(PermissionType.ADD_IDEA)
    def persist(self):
        return super(Idea, self).persist()

    @permissions_check(PermissionType.REMOVE_IDEA)
    def delete(self):
        return super(Idea, self).delete()

    def associate_to_idea(self, idea):
        """ Define an Idea as a sub-idea for the current Idea
        @:return updated Idea """
        if self.discussion_id == idea.discussion_id:
            idea.parent_idea_id = self.id
            self.ideas.append(idea)
            return self.db.upsert(idea)
        else:
            raise ForbiddenActionException('')

    def get_all_children_ideas(self):
        """ Given an Idea, retrieve all children ideas associated
        @:return list of children of an Idea"""
        return self.__find_children_ideas(self.db.find_all(self), [self.id])

    def __find_children_ideas(self, ideas, children_ids):
        """ Recursively find children ideas
        @:return list of children of an Idea"""
        children = self.db.find_list_by(self,
                                        'parent_idea_id',
                                        children_ids,
                                        in_operator=True)
        if len(children) > 0:
            return children + self.__find_children_ideas(
                ideas, [child.id for child in children])
        else:
            return []

    def __get_posts_associated_to_idea(self):
        """ Get all posts associated to an idea including the posts associated to sub-ideas
        @:return list of post """
        # retrieve all children ideas ids including the current idea
        ideas_ids = [self.id
                     ] + [idea.id for idea in self.get_all_children_ideas()]
        # get each posts matching at least on of these ids
        return self.db.find_list_by(Post,
                                    'ideas_ids',
                                    ideas_ids,
                                    in_operator=True,
                                    join=JoinType.LEFT)

    def number_of_messages(self):
        """ Retrieve number of Posts associated to the Idea
        @:return number"""
        posts_associated = self.__get_posts_associated_to_idea()
        # exclude redundant ids
        unique_posts = {
            post_id
            for post_id in [post.id for post in posts_associated]
        }
        return len(unique_posts)

    def number_of_participants(self):
        """ Retrieve number of users who posted something about this Idea
        @:return number"""
        # exclude redundant ids
        unique_users = {
            creator_id
            for creator_id in [
                post.creator_id
                for post in self.__get_posts_associated_to_idea()
            ]
        }

        return len(unique_users)

    def print_all_ideas(self):
        """ Print a tree representing the Idea and its sub-ideas """
        print(self.title)
        self.__print_ideas_recursively(self.db.find_all(self), self.id)

    def __print_ideas_recursively(self, ideas, parent_id, level=1):
        """ Recursively print children ideas hierarchy
        @:return list of children of an Idea"""

        children = self.db.find_list_by(self, 'parent_idea_id', parent_id)
        if len(children) > 0:
            for child in children:
                print('    ' * level + child.title)
                self.__print_ideas_recursively(ideas, child.id, level + 1)
            else:
                level -= 1
예제 #9
0
class User(Base):
    """This describes the model for a User. """

    columns = [
        Column('id', ColumnType.STRING, primary_key=True),
        Column('username', ColumnType.STRING),
        Column('email', ColumnType.STRING),
        Column('password', ColumnType.STRING)
    ]

    def __init__(self, username, email, password, **kwargs):
        super(User, self).__init__(**kwargs)
        self._id = None
        self._username = username
        self._email = email
        self._password = password
        self._permissions = []

    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, id):
        self._id = id

    @property
    def username(self):
        return self._username

    @username.setter
    def username(self, username):
        self._username = username

    @property
    def email(self):
        return self._email

    @email.setter
    def email(self, email):
        self._email = email

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, password):
        self._password = password

    @property
    def permissions(self):
        return self._permissions

    @permissions.setter
    def permissions(self, permissions):
        self._permissions = permissions

    def set_global_permissions(self, permissions):
        self.permissions = permissions

    def set_local_permissions(self):
        raise NotImplementedError()
예제 #10
0
class Post(Base):
    """This describes the model for a Post. """

    columns = [
        Column('id', ColumnType.STRING, primary_key=True),
        Column('discussion_id', ColumnType.STRING, foreign_key=True),
        Column('parent_post_id', ColumnType.STRING, foreign_key=True),  # adjacency list relationship
        Column('creator_id', ColumnType.STRING, foreign_key=True),
        Column('text', ColumnType.STRING),
        Column('upvote_count', ColumnType.NUMERIC),
        Column('creation_date', ColumnType.DATETIME),
    ]

    def __init__(self, discussion_id, text, creator_id, parent_post_id=None, parent_idea_id=None, upvote_count=0,
                 creation_date=datetime.now(), **kwargs):
        super(Post, self).__init__(**kwargs)
        self._id = None
        self._discussion_id = discussion_id
        self._creator_id = creator_id
        self._parent_post_id = parent_post_id
        self._parent_idea_id = parent_idea_id
        self._text = text
        self._upvote_count = upvote_count
        self._creation_date = creation_date
        # list of posts that replies to the current post
        self._posts = []
        # list of ideas ids associated to the post
        self._ideas_ids = []

    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, id):
        self._id = id

    @property
    def discussion_id(self):
        return self._discussion_id

    @discussion_id.setter
    def discussion_id(self, discussion_id):
        self._discussion_id = discussion_id

    @property
    def creator_id(self):
        return self._creator_id

    @creator_id.setter
    def creator_id(self, creator_id):
        self._creator_id = creator_id

    @property
    def parent_post_id(self):
        return self._parent_post_id

    @parent_post_id.setter
    def parent_post_id(self, parent_post_id):
        self._parent_post_id = parent_post_id

    @property
    def parent_idea_id(self):
        return self._parent_idea_id

    @parent_idea_id.setter
    def parent_idea_id(self, parent_idea_id):
        self._parent_idea_id = parent_idea_id

    @property
    def text(self):
        return self._text

    @text.setter
    def text(self, text):
        self._text = text

    @property
    def posts(self):
        return self._posts

    @posts.setter
    def posts(self, posts):
        self._posts = posts

    @property
    def ideas_ids(self):
        return self._ideas_ids

    @ideas_ids.setter
    def ideas_ids(self, ideas_ids):
        self._ideas_ids = ideas_ids

    @permissions_check(PermissionType.ADD_POST)
    def persist(self):
        return super(Post, self).persist()

    @permissions_check(PermissionType.REMOVE_POST)
    def delete(self):
        return super(Post, self).delete()

    @permissions_check(PermissionType.ADD_POST)
    def reply_with_post(self, post):
        """ Reply to the current post by another post
        @:return the persisted reply to the post """
        if post.discussion_id == self.discussion_id:
            post.parent_post_id = self.id
            self.posts.append(post)
            return self.db.upsert(post)
        else:
            raise ForbiddenActionException('Reply to this post not allowed')

    def associate_with_idea(self, idea):
        """ Associate one Post with an Idea
        @:return updated Post """
        if self.discussion_id == idea.discussion_id:
            idea.parent_post_id = self.id
            self.ideas_ids.append(idea.id)
            return self.db.upsert(idea)
        else:
            raise ForbiddenActionException('Cannot associate this post with this idea')

    def get_all_children_posts(self):
        """ Given a Post, retrieve all children posts associated
        @:return list of children of a Post"""
        return self.__find_children_posts(self.db.find_all(self), [self.id])

    def __find_children_posts(self, posts, child_ids):
        """ Recursively build a list of find children posts
        @:return list of children of a Post"""
        children = self.db.find_list_by(self, 'parent_post_id', child_ids, in_operator=True)
        if len(children) > 0:
            return children + self.__find_children_posts(posts, [child.id for child in children])
        else:
            return []

    def print_all_posts(self):
        """ Print a tree representing the Post and its sub-posts """
        print(self.text)
        self.__print_posts_recursively(self.db.find_all(self), self.id)

    def __print_posts_recursively(self, posts, parent_id, level=1):
        """ Recursively print children posts hierarchy
        @:return list of children of an Post"""

        children = self.db.find_list_by(self, 'parent_post_id', parent_id)
        if len(children) > 0:
            for child in children:
                print('    ' * level + child.text)
                self.__print_posts_recursively(posts, child.id, level + 1)
            else:
                level -= 1
예제 #11
0
class Discussion(Base):
    """This describes the model for a Discussion.
       Discussion contains Ideas and Posts.
    """

    columns = [
        Column('id', ColumnType.STRING, primary_key=True),
        Column('title', ColumnType.STRING),
        Column('name', ColumnType.STRING),
        Column('description', ColumnType.STRING),
        Column('creator_id', ColumnType.STRING),
        Column('creation_date', ColumnType.DATETIME)
    ]

    def __init__(self, name, title, creator_id, description=None, creation_date=datetime.now(), **kwargs):
        super(Discussion, self).__init__(**kwargs)
        self._id = None
        self._name = name
        self._title = title
        self._description = description
        self._creator_id = creator_id
        self._creation_date = creation_date
        self._posts = []

    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, id):
        self._id = id

    @property
    def title(self):
        return self._title

    @title.setter
    def title(self, title):
        self._title = title

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, name):
        self._name = name

    @property
    def creator_id(self):
        return self._creator_id

    @creator_id.setter
    def creator_id(self, creator_id):
        self._creator_id = creator_id

    @property
    def creation_date(self):
        return self._creation_date

    @creation_date.setter
    def creation_date(self, creation_date):
        self._creation_date = creation_date

    @property
    def posts(self):
        return self._posts

    @posts.setter
    def posts(self, posts):
        self._posts = posts

    def add_post(self, post):
        self._posts = self.posts + post

    def number_of_posts(self):
        """ Return the total number of posts of the Discussion """
        return len(self.db.find_list_by(Post, 'discussion_id', self.id))

    def number_of_ideas(self):
        """ Return the total number of ideas of the Discussion """
        return len(self.db.find_list_by(Idea, 'discussion_id', self.id))

    def all_posts_associated_to_idea(self):
        raise NotImplementedError()

    def all_posts_not_associated_to_idea(self):
        raise NotImplementedError()

    def number_of_participants(self):
        raise NotImplementedError()