Exemple #1
0
class Thing(gdb.Entity):
    """docstring for Thing"""
    tags = db.StringListProperty(default=[])

    title = db.StringProperty(required=True)

    introduction = db.StringProperty(required=True)

    keyword_index = db.StringListProperty(required=True)

    # rank properties
    rank = db.FloatProperty(required=True)

    rank_1 = db.IntegerFlyProperty(required=True)
    rank_2 = db.IntegerFlyProperty(required=True)
    rank_3 = db.IntegerFlyProperty(required=True)
    rank_4 = db.IntegerFlyProperty(required=True)
    rank_5 = db.IntegerFlyProperty(required=True)

    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 update_keyword_index(self):
        """docstring for update_keyword_index"""
        pass
Exemple #2
0
class User(gdb.Entity):
    """docstring for User"""
    fullname = db.StringProperty(required=True, default="Name")
    username = db.StringProperty(required=True)
    create_date = db.DateTimeProperty(auto_now_add=True)
    password = db.StringProperty(required=True)
    email = db.EmailProperty(required=True)
    photo_url = db.StringProperty(default=settings.DEFAULT_USER_PHOTO)

    status_message = db.StringFlyProperty(default="")
    introduction = db.StringFlyProperty(default="")

    def can_visit_key(self, user, key):
        """Privacy control, protect ur privacy here"""
        if key == 'password':
            return False
        elif key == 'email':
            # can be visible by the user himself
            return user and user.username == self.username
        return True

    def change_to_gravatar_icon(self):
        """docstring for change_to_gravatar"""
        # use gravatar icon
        import hashlib
        self.photo_url = settings.GRAVATAR_BASE + \
          str(hashlib.md5(self.email.lower()).hexdigest()) + "?d=identicon&s=48"

    def put(self, is_guest=False):
        """User could be built from sessions, need write through these users"""
        super(User, self).put()

        if is_guest:
            return

        from auth import get_current_user, update_current_user

        user = get_current_user()

        if user and user.username == self.username:
            update_current_user(self)

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

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

    def full_photo(self):
        """docstring for full_photo"""
        return self.photo_url
Exemple #3
0
class Subscription(db.Model):
    """The relation between entities and messages"""
    subscriber = db.WeakReferenceProperty(required=True)
    subscriber_type = db.StringProperty(required=True)
    topic = db.WeakReferenceProperty(required=True)
    topic_type = db.StringProperty(required=True)

    def __init__(self, *args, **kwargs):
        for attr in ('subscriber', ):
            if kwargs.has_key(attr):
                kwargs[attr + '_type'] = _get_type_name(kwargs.get(attr))

        super(Subscription, self).__init__(*args, **kwargs)
Exemple #4
0
class TestHuman(gdb.Entity):
    """docstring for TestHuman"""
    name = db.StringProperty()

    def love(self, other):
        """docstring for love"""
        self.link(LOVE, other)

    def really_love(self, other):
        """docstring for really_love"""
        self.link(LOVE, other, link_attr=REALLY)

    def is_really_loving(self, other):
        """docstring for is_really_loving"""
        return self.has_link(LOVE, other, link_attr=REALLY)

    def is_loving(self, other):
        """docstring for is_loving"""
        return self.has_link(LOVE, other)

    def unlove(self, other):
        """docstring for unlove"""
        self.unlink(LOVE, other)

    def get_lovers(self):
        """docstring for get_lovers"""
        return self.get_targets(LOVE, TestHuman, keys_only=True)
Exemple #5
0
class Site(db.Model):
    """a faked object"""
    site_name = db.StringProperty(required=True, default=_("Daoshicha.com"))
    site_slogan = db.StringProperty(required=True, \
      default= _("Want to be the best open source SNS!"))

    @classmethod
    @cache_result('globalsite-instance', 240)
    def get_instance(cls):
        """docstring for get_instance"""
        instance = super(Site, cls).all().get()
        if not instance:
            instance = Site()
            instance.put()

        return instance
Exemple #6
0
class Link(db.Model):
    """The model for the link between entites"""
    link_type = db.StringProperty(required=True)
    link_attr = db.StringProperty(default=None)

    source = db.WeakReferenceProperty(required=True)
    source_type = db.StringProperty(required=True)
    target = db.WeakReferenceProperty(required=True)
    target_type = db.StringProperty(required=True)

    def __init__(self, *args, **kwargs):
        for attr in ('source', 'target'):
            if kwargs.has_key(attr):
                kwargs[attr + '_type'] = _get_type_name(kwargs.get(attr))

        super(Link, self).__init__(*args, **kwargs)
Exemple #7
0
class TestTalk(gdb.Message):
    """docstring for TestTalk"""

    is_comment = db.BooleanProperty(default=False)
    content = db.StringProperty()

    @classmethod
    def get_cls_type_name(cls):
        """docstring for get_cls_type_name"""
        return cls.type_name

    type_name = 'TestTalk'
Exemple #8
0
class MessageIndex(db.Model):
    """docstring for MessageIndex"""
    subscribers = db.StringListProperty(required=True)
    target = db.WeakReferenceProperty(required=True)
    target_type = db.StringProperty(required=True)
    create_at = db.DateTimeProperty(auto_now_add=True)
    update_at = db.DateTimeProperty(auto_now=True)

    @classmethod
    def all(cls, **kwargs):
        if kwargs.has_key('subscribers'):
            value = kwargs['subscribers']
            if isinstance(value, db.Model):
                value = value.key()
            if not isinstance(value, db.Key):
                raise Exception('Subscriber must be key or model %s' % value)
            value = str(value)
            kwargs['subscribers'] = value

        return super(MessageIndex, cls).all(**kwargs)
Exemple #9
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
Exemple #10
0
class Photo(db.Model):
    """docstring for ProfilePhoto"""
    blob_key = db.StringProperty()
    url = db.StringProperty()
Exemple #11
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)
Exemple #12
0
class TestStudent(db.FlyModel):
    """docstring for Student"""
    name = db.StringProperty()
    age = db.IntegerFlyProperty()
    sex = db.StringFlyProperty()
Exemple #13
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)