Exemplo n.º 1
0
class UserGroupInfo(gdb.Entity):
    """docstring for UserGroupProfile"""
    user = db.ReferenceProperty(required=True)

    topic_count = db.IntegerFlyProperty(default=0)
    post_count = db.IntegerFlyProperty(default=0)
    group_count = db.IntegerFlyProperty(default=0)

    recent_joined_groups = db.ListFlyProperty(default=[])

    def update_topic_count(self):
        """docstring for update_topic_count"""
        self.topic_count = GroupTopic.all(author=self.user).count()
        self.put()

    def update_post_count(self):
        """docstring for update_post_count"""
        self.post_count = GroupPost.all(author=self.user).count()
        self.put()

    def update_group_count(self):
        """docstring for update_group_count"""
        self.group_count = Group.all(creator=self.user).count()
        self.put()

    def get_recent_joined_groups(self):
        """docstring for joined_groups"""
        return db.get(self.recent_joined_groups)

    def update_recent_joined_groups(self):
        """docstring for get_joined_groups"""
        self.recent_joined_groups = Group.get_group_keys_by_user(self.user)\
          .fetch(limit=8, offset=0)

        logging.debug("recent_joined_groups : %s", self.recent_joined_groups)

        self.put()

    @classmethod
    def get_by_user(cls, user):
        """docstring for get_by_user"""
        # init all needed object here
        groupinfo = UserGroupInfo.all().filter("user =", user).get()
        if not groupinfo:
            groupinfo = UserGroupInfo(user=user)
            groupinfo.put()
        return groupinfo
Exemplo n.º 2
0
class Thing(gdb.Entity):
    """docstring for Thing"""

    creator = db.ReferenceProperty(User, required=True)

    title = db.StringProperty(required=True)

    introduction = db.StringProperty(required=True)

    photo_url = db.StringProperty(required=True)

    tags = db.StringListProperty(default=[])
    keyword_index = db.StringListProperty(required=True)

    # rank properties
    rank = db.FloatProperty(required=True)
    rank_counts = db.ListFlyProperty(default=[0] * 5)

    def can_own(self, user):
        """docstring for can_own"""
        pass

    def has_owner(self, user):
        """docstring for has_owner"""
        pass

    def add_owner(self, user):
        """docstring for add_owner"""
        pass

    def can_want(self, user):
        """docstring for can_want"""
        pass

    def has_wanting_one(self):
        """docstring for has_wantor"""
        pass

    def add_wanting_one(self):
        """docstring for add_wanting_one"""
        pass

    def get_rank(self, user):
        """docstring for get_rank"""
        pass

    def add_rank(self, user, rank):
        """docstring for add_rank"""
        pass

    def update_rank_info(self):
        """docstring for update_rank_info"""
        pass

    def rank_info(self):
        """docstring for rank_info"""
        pass

    def update_keyword_index(self):
        """docstring for update_keyword_index"""
        pass
Exemplo n.º 3
0
class UserStreamInfo(gdb.Entity):
    """docstring for UserStreamInfo"""
    user = db.ReferenceProperty(User, required=True)

    recent_follower_keys = db.ListFlyProperty(default=[])
    recent_following_keys = db.ListFlyProperty(default=[])

    comment_count = db.IntegerFlyProperty(default=1)
    stream_count = db.IntegerFlyProperty(default=1)
    follower_count = db.IntegerFlyProperty(default=1)
    following_count = db.IntegerFlyProperty(default=1)

    def can_view_all(self, user):
        """docstring for can_view"""
        # people always can view other's homepage
        return True

    def can_view_following(self, user):
        """docstring for can_view_following"""
        return self.is_me(user)

    def can_view_mention(self, user):
        """docstring for can_view_mention"""
        return True

    def can_follow(self, user):
        """docstring for can_follow"""
        return not user is Guest and not self.has_follower(
            user) and not self.is_me(user)

    def can_unfollow(self, user):
        """docstring for can_unfollow"""
        return self.has_follower(user) and not self.is_me(user)

    def follow(self, user):
        """docstring for follow"""
        self.user.link(FOLLOWED_BY, user)

        self.update_follower_count()

        UserStreamInfo.get_instance(user).update_following_count()

    def unfollow(self, user):
        """docstring for unfollow"""
        self.user.unlink(FOLLOWED_BY, user)

        self.update_follower_count()

        UserStreamInfo.get_instance(user).update_following_count()

    def has_follower(self, user):
        """docstring for has_follower"""
        return self.user.has_link(FOLLOWED_BY, user)

    def get_following_keys(self):
        """docstring for get_follower_keys"""
        return User.get_sources(FOLLOWED_BY, self.user, keys_only=True)

    def get_followers(self):
        """docstring for get_followers"""
        return db.MapQuery(self.get_following_keys(), lambda x: db.get(x),
                           True)

    def get_follower_keys(self):
        """docstring for get_following_keys"""
        return self.user.get_targets(FOLLOWED_BY, User, keys_only=True)

    def get_following(self):
        """docstring for get_following"""
        return db.MapQuery(self.get_following_keys(), lambda x: db.get(x),
                           True)

    def is_me(self, user):
        """docstring for is_me"""
        return user.key() == self.user.key()

    ############
    #
    # Stream related apis
    #
    ############

    def can_create_stream(self, user):
        """docstring for can_create_stream"""
        return self.is_me(user)

    def create_stream(self, stream):
        """docstring for create_stream"""
        stream.author_stream_info = self
        stream.author = self.user
        stream.init_keywords()

        stream.put()

        stream.notify(list(self.get_follower_keys()) + [self.user.key()])

        self.update_stream_count()

    def delete_stream(self, stream):
        """docstring for delete_stream"""
        stream.undo_notify()

        stream.delete()

        self.update_stream_count()

    def get_latest_streams(self):
        """docstring for get_all_streams"""
        return UserStream.all(author_stream_info=author_stream_info)\
          .order("-create_at")

    ############
    #
    # User info update
    #
    ############

    def update_field(self, field, value):
        """docstring for update_data"""
        setattr(self, field, value)
        self.put()

    def update_stream_count(self):
        """docstring for update_comment_count"""
        count = UserStream.all(author_stream_info=self).count()
        self.update_field('stream_count', count)

    def update_follower_count(self):
        """docstring for update_follower_count"""
        query = self.get_follower_keys()
        self.recent_follower_keys = list(query.fetch(10))
        self.update_field('follower_count', query.count())

    def update_following_count(self):
        """docstring for update_following_count"""
        query = self.get_following_keys()
        self.recent_following_keys = list(query.fetch(10))
        self.update_field('following_count', query.count())

    def recent_follower_users(self):
        """docstring for recent_follower_users"""
        return db.get(self.recent_follower_keys)

    def recent_following_users(self):
        """docstring for recent_following_users"""
        return db.get(self.recent_following_keys)

    @classmethod
    def get_instance(cls, user):
        """docstring for get_instance"""
        info = UserStreamInfo.all(user=user).get()
        if not info:
            info = UserStreamInfo(user=user)
            info.put()
        return info
Exemplo n.º 4
0
class UserStreamComment(gdb.Message):
    """docstring for UserStreamComment"""
    stream = db.ReferenceProperty(UserStream)
    author = db.ReferenceProperty(User)

    content = db.StringFlyProperty(default='')
Exemplo n.º 5
0
class UserStream(gdb.Message):
    """docstring for UserStream"""
    author_stream_info = db.ReferenceProperty(UserStreamInfo, \
      collection_name = 'user_streams')
    author = db.ReferenceProperty(User, \
      collection_name = 'user_streams')

    target = db.ReferenceProperty()
    target_type = db.StringProperty(required=True, default=TEXT_STREAM)

    content = db.StringFlyProperty(default='')

    keywords = db.StringListProperty(required=True, default=[])

    def __init__(self, *args, **kwargs):
        """docstring for __init__"""

        for attr in ('target', ):
            if kwargs.has_key(attr) and kwargs.get(attr):
                kwargs[attr + '_type'] = gdb._get_type_name(kwargs.get(attr))

        super(UserStream, self).__init__(*args, **kwargs)

    def init_keywords(self):
        """docstring for _init_keywords"""
        # init keywords index

        if self.content:
            for pattern in _KEY_PATTERNS_:
                for keyword in pattern.findall(self.content):
                    self.keywords.append(keyword)

    def can_comment(self, user):
        """docstring for can_comment"""
        self.author_stream_info.has_follower(user)

    def create_comment(self, comment, user):
        """docstring for create_comment"""
        comment.stream = self
        comment.author = self.author

        comment.put()

        self.redo_notify()

    def delete_comment(self, comment, user):
        """docstring for delete_comment"""
        comment.delete()

    def get_all_comments(self):
        """docstring for get_all_comments"""
        return UserStreamComment.all(stream=self).order('create_at')

    @classmethod
    def latest_by_author(cls, author):
        """docstring for latest_by_author"""
        return cls.all(author=author).order('-create_at')

    @classmethod
    def latest_by_keyword(cls, keyword):
        """docstring for latest_by_keywords"""
        return cls.latest().filter('keywords', keyword)
Exemplo n.º 6
0
class GroupPost(gdb.Message):
    """docstring for Post"""
    author = db.ReferenceProperty(User)
    topic = db.ReferenceProperty(GroupTopic)
    content = db.TextFlyProperty(default='')
Exemplo n.º 7
0
class GroupTopic(gdb.Message):
    """docstring for Thread"""
    author = db.ReferenceProperty(User)
    group = db.ReferenceProperty(Group)

    title = db.TextFlyProperty(default='')
    content = db.TextFlyProperty(default='')
    length = db.IntegerFlyProperty(default=0)
    hits = db.IntegerFlyProperty(default=0)

    def can_view(self, user):
        """docstring for can_view"""
        return self.group.can_view(user)

    def can_edit(self, user):
        """docstring for can_edit"""
        return self.is_author(user)

    def can_delete(self, user):
        """docstring for can_delete"""
        return self.is_author(user)

    def is_author(self, user):
        """docstring for is_author"""
        return self.author.key() == user.key()

    #######
    #
    # post related apis
    #
    #######

    def can_create_post(self, user):
        """docstring for can_create_thread"""
        return self.can_view(user) and user != Guest

    def can_delete_post(self, user):
        """docstring for can_delete_post"""
        return False

    def create_post(self, post, user):
        """docstring for add_group_post"""
        post.author = user
        post.topic = self
        post.put()

        self.length = self.get_all_posts().count()
        self.put()

        self.redo_notify()

        UserGroupInfo.get_by_user(user).update_post_count()

    def delete_post(self, post):
        """docstring for delete_post"""
        post.delete()

    def get_all_posts(self, has_order=False):
        """docstring for get_all_posts"""
        query = GroupPost.all(topic=self)
        if has_order:
            query = query.order("create_at")
        return query
Exemplo n.º 8
0
class Group(gdb.Entity):
    """docstring for Board"""
    creator = db.ReferenceProperty(User)
    title = db.StringProperty(default='')

    introduction = db.TextProperty(default='')
    photo_url = db.StringProperty(default=settings.DEFAULT_GROUP_PHOTO)

    recent_members = db.ListFlyProperty(default=[])
    member_count = db.IntegerFlyProperty(default=1)

    def can_view(self, user):
        """docstring for can_see"""
        return True

    def can_delete(self, user):
        """docstring for can_delete"""
        return self.is_creator(user)

    def is_creator(self, user):
        """docstring for is_creator"""
        return user.key() == self.creator.key()

    #######
    #
    # membership related apis
    #
    #######

    def can_join(self, user):
        """docstring for can_join"""
        return user != Guest and (not self.has_member(user))

    def can_quit(self, user):
        """docstring for can_quit"""
        return self.has_member(user) and not self.is_creator(user)

    def join(self, user):
        self.link(GROUP_MEMEBER, user)

        self._update_member_info()

        UserGroupInfo.get_by_user(user).update_recent_joined_groups()

    def quit(self, user):
        """docstring for quit"""
        self.unlink(GROUP_MEMEBER, user)

        self._update_member_info()

        UserGroupInfo.get_by_user(user).update_recent_joined_groups()

    def has_member(self, user):
        """docstring for has_member"""
        return self.has_link(GROUP_MEMEBER, user)

    def get_latest_joined_members(self):
        """docstring for get_latest_joined_members"""
        return db.get(self.recent_members.reverse())

    def get_member_keys(self):
        """docstring for get_members"""
        return self.get_targets(GROUP_MEMEBER, User, keys_only=True)

    def get_members(self):
        """docstring for get_members"""
        return db.MapQuery(self.get_member_keys(), lambda x: db.get(x), True)

    def _update_member_info(self):
        """docstring for _update_member_count"""
        self.member_count = self.get_member_keys().count()
        self.recent_members = list(self.get_member_keys().fetch(limit=6))
        self.put()

    #######
    #
    # admin related apis
    #
    #######

    def can_edit(self, user):
        """If the user could edit the group"""
        return self.has_admin(user)

    def can_add_admin(self, user):
        """docstring for can_add_admin"""
        return self.is_creator(user)

    def can_remove_admin(self, user):
        """docstring for can_remove_admin"""
        return self.is_creator(user)

    def add_admin(self, new_admin):
        """docstring for add_admin"""
        return self.link(GROUP_ADMIN, new_admin)

    def remove_admin(self, new_admin):
        """docstring for remove_admin"""
        return self.unlink(GROUP_ADMIN, new_admin)

    def has_admin(self, user):
        """docstring for has_admin"""
        return self.has_link(GROUP_ADMIN, user)

    def _get_admin_keys(self, limit=24, offset=0):
        """deprecated function"""
        return self.get_targets(GROUP_ADMIN, User, limit=limit,\
          offset=offset, keys_only=True)

    def _get_admins(self, limit=24, offset=0):
        """deprecated function"""
        return db.get(self.get_admin_keys(self, limit=limit, offset=offset))

    #######
    #
    # topic related apis
    #
    #######

    def can_create_topic(self, user):
        """docstring for can_create_thread"""
        return self.has_member(user)

    def create_topic(self, topic, user):
        """ Create a new topi in group
    TODO: remove the limit of 1000 for group member herr
    """
        topic.group = self
        topic.author = user

        topic.put()

        subscribers = list(self.get_member_keys()) + [Guest]
        topic.notify(subscribers)

        UserGroupInfo.get_by_user(user).update_topic_count()

    def delete_topic(self, topic):
        """docstring for delete_topic"""
        topic.undo_notify()
        topic.delete()

    def get_all_topics(self, has_order=False):
        """docstring for get_all_topics"""
        query = GroupTopic.all(group=self)
        if has_order: query = query.order('-update_at')
        return query

    @classmethod
    def get_group_keys_by_user(cls, user):
        """docstring for get_groups_by_user"""
        return cls.get_sources(GROUP_MEMEBER, user, keys_only=True)