Esempio n. 1
0
    def check_vote(cls, user, article_id):
        """Determine if user_id has voted or not
        Args:
            user (User): the current user
        Returns:
            None if the user has not voted yet
            True if the user has up-voted
            False if the user has down-voted
        """
        self = cls()
        self.id = article_id

        ref_name = get_remote_side(self, 'votes')
        if not user:
            return None

        key = self._user_vote_redis_key(user.id)
        redis_client = get_client(key)

        if not redis_client.exists(key):
            filter_data = {ref_name: self.id, 'user_id': user.id}
            cls = get_remote_side_class(self, 'votes')
            vote = cls.query.filter_by(**filter_data).first()

            if vote:
                user_vote = vote.up
            else:
                user_vote = None

            redis_client.set(key, self._vote_value_for_redis(vote))
        else:
            value = int(redis_client.get(key))
            user_vote = self._parse_vote_value_from_redis(value)

        return user_vote
Esempio n. 2
0
    def create_comment(self, content, user, ip, commit=False):
        ref_name = get_remote_side(self, 'comments')
        
        cls = get_remote_side_class(self, 'comments')

        # verify that the user has not created any comment for this article within the last 30s
        # TODO: cache this shit
        last_comment = cls.query.filter_by(ip=hash(ip)).order_by(cls.date_created.desc()).first()

        if last_comment:
            time_diff = now_ms() - last_comment.date_created
            limit = self.comment_limit

            if time_diff < limit:
                raise ModelException(
                    type='VALIDATION_FAILED',
                    message=_(u'Please wait %(time)s seconds before sending new comment', time=int(round((limit-time_diff)/1000)))
                )

        comment = cls()
        setattr(comment, ref_name, self.id)
        comment.user_id = user.id
        comment.content = content
        comment.ip = hash(ip)

        # also update the comment count
        cache.update_user_comment_count(self, user.id)

        db_session.add(comment)

        if commit:
            db_session.commit()

        return comment
Esempio n. 3
0
    def create_tags(self, tags, commit=False):
        if not tags:
            return self

        tags = [tag.lower() for tag in tags]
        tags = set(tags)

        # delete old tags
        for tag in self.tags:
            db_session.delete(tag)

        cls = get_remote_side_class(self, 'tags')
        ref_name = get_remote_side(self, 'tags')
        # create new tags
        for t in tags:
            tag = cls()
            tag.name = t
            setattr(tag, ref_name, self.id)
            db_session.add(tag)

        cache.delete_tags(self)

        if commit:
            db_session.commit()
        return self
Esempio n. 4
0
    def get_comments(self, limit=10, offset=0, order='date_created desc', json=False):
        ref_name = get_remote_side(self, 'comments')

        filter_data = {
            ref_name: self.id
        }
        cls = get_remote_side_class(self, 'comments')
        comments = cls.query.filter_by(**filter_data).order_by(order).limit(limit).offset(offset).all()
        if json:
            return [comment.json_data() for comment in comments]
        return comments
Esempio n. 5
0
    def list_by_tag(cls, name, limit=10, offset=0, json=False):
        obj = cls() # due to the lazy-init of backref, we can't have tags before creating new instance
        ref_name = get_remote_side(obj, 'tags')
        Tag = get_remote_side_class(obj, 'tags')
        tags = Tag.query.filter(getattr(Tag, ref_name) != None, Tag.name == name).order_by(ref_name+' DESC').limit(limit).offset(offset).all()

        ref_name = ref_name.split("_")[0]
        records = [getattr(tag, ref_name) for tag in tags]

        if json:
            return [record.json_data() for record in records]

        return records
Esempio n. 6
0
    def unvote(self, user, commit=False):
        ref_name = get_remote_side(self, 'votes')
        cls = get_remote_side_class(self, 'votes')

        data = {ref_name: self.id, "user_id": user.id}

        vote = cls.query.filter_by(**data).first()

        if vote:
            vote.delete(commit=commit)

        self._update_vote_value_for_redis(user.id, None)

        return vote
Esempio n. 7
0
    def get_comments(self,
                     limit=10,
                     offset=0,
                     order='date_created desc',
                     json=False):
        ref_name = get_remote_side(self, 'comments')

        filter_data = {ref_name: self.id}
        cls = get_remote_side_class(self, 'comments')
        comments = cls.query.filter_by(
            **filter_data).order_by(order).limit(limit).offset(offset).all()
        if json:
            return [comment.json_data() for comment in comments]
        return comments
Esempio n. 8
0
    def record(self, user, ip, data, reason=None, commit=False):
        ref_name = get_remote_side(self, 'edits')

        cls = get_remote_side_class(self, 'edits')
        edit = cls()
        setattr(edit, ref_name, self.id)
        edit.user_id = user.id
        edit.ip = hash(ip)
        edit.data = data
        edit.reason = reason

        db_session.add(edit)

        if commit:
            db_session.commit()
Esempio n. 9
0
    def check_comment(cls, user, article_id):
        if not user:
            return None
        self = cls()
        self.id = article_id

        count = cache.get_user_comment_count(self, user.id)
        if not count:
            ref_name = get_remote_side(self, 'comments')
            filter_data = {ref_name: self.id, 'user_id': user.id}
            cls = get_remote_side_class(self, 'comments')
            count = cls.query.filter_by(**filter_data).count()
            count = cache.update_user_comment_count(self, user.id, count)

        return count > 0
Esempio n. 10
0
    def vote(self, user, ip, up=True, commit=False):
        ref_name = get_remote_side(self, 'votes')
        cls = get_remote_side_class(self, 'votes')

        data = {
            "user_id": user.id,
            ref_name: self.id,
            "up": up,
            "ip": hash(ip),
            "commit": commit
        }

        vote = cls.create_or_update(**data)
        self._update_vote_value_for_redis(user.id, vote)

        return vote
Esempio n. 11
0
    def vote(self, user, ip, up=True, commit=False):
        ref_name = get_remote_side(self, 'votes')
        cls = get_remote_side_class(self, 'votes')

        data = {
            "user_id": user.id,
            ref_name: self.id,
            "up": up,
            "ip": hash(ip),
            "commit": commit
        }

        vote = cls.create_or_update(**data)
        self._update_vote_value_for_redis(user.id, vote)

        return vote
Esempio n. 12
0
    def unvote(self, user, commit=False):
        ref_name = get_remote_side(self, 'votes')
        cls = get_remote_side_class(self, 'votes')

        data = {
            ref_name: self.id,
            "user_id": user.id
        }

        vote = cls.query.filter_by(**data).first()

        if vote:
            vote.delete(commit=commit)

        self._update_vote_value_for_redis(user.id, None)

        return vote
Esempio n. 13
0
    def check_comment(cls, user, article_id):
        if not user:
            return None
        self = cls()
        self.id = article_id

        count = cache.get_user_comment_count(self, user.id)
        if not count:
            ref_name = get_remote_side(self, 'comments')
            filter_data = {
                ref_name: self.id,
                'user_id': user.id
            }
            cls = get_remote_side_class(self, 'comments')
            count = cls.query.filter_by(**filter_data).count()
            count = cache.update_user_comment_count(self, user.id, count)

        return count > 0
Esempio n. 14
0
    def check_vote(cls, user, article_id):
        """Determine if user_id has voted or not
        Args:
            user (User): the current user
        Returns:
            None if the user has not voted yet
            True if the user has up-voted
            False if the user has down-voted
        """
        self = cls()
        self.id = article_id
        
        ref_name = get_remote_side(self, 'votes')
        if not user:
            return None
        
        key = self._user_vote_redis_key(user.id)
        redis_client = get_client(key)

        if not redis_client.exists(key):
            filter_data ={
                ref_name: self.id,
                'user_id': user.id
            }
            cls = get_remote_side_class(self, 'votes')
            vote = cls.query.filter_by(**filter_data).first()

            if vote:
                user_vote = vote.up
            else:
                user_vote = None

            redis_client.set(key, self._vote_value_for_redis(vote))
        else:
            value = int(redis_client.get(key))
            user_vote = self._parse_vote_value_from_redis(value)

        return user_vote
Esempio n. 15
0
    def create_comment(self, content, user, ip, commit=False):
        ref_name = get_remote_side(self, 'comments')

        cls = get_remote_side_class(self, 'comments')

        # verify that the user has not created any comment for this article within the last 30s
        # TODO: cache this shit
        last_comment = cls.query.filter_by(ip=hash(ip)).order_by(
            cls.date_created.desc()).first()

        if last_comment:
            time_diff = now_ms() - last_comment.date_created
            limit = self.comment_limit

            if time_diff < limit:
                raise ModelException(
                    type='VALIDATION_FAILED',
                    message=
                    _(u'Please wait %(time)s seconds before sending new comment',
                      time=int(round((limit - time_diff) / 1000))))

        comment = cls()
        setattr(comment, ref_name, self.id)
        comment.user_id = user.id
        comment.content = content
        comment.ip = hash(ip)

        # also update the comment count
        cache.update_user_comment_count(self, user.id)

        db_session.add(comment)

        if commit:
            db_session.commit()

        return comment
Esempio n. 16
0
 def count_by_tag(cls, name):
     obj = cls() # due to the lazy-init of backref, we can't have tags before creating new instance
     ref_name = get_remote_side(obj, 'tags')
     Tag = get_remote_side_class(obj, 'tags')
     return Tag.query.filter(getattr(Tag, ref_name) != None, Tag.name == name).count()