Пример #1
0
    def detail(self, request, slug):
        article = Article.query.filter_by(slug=slug).one()
        if article.hidden:
            # TODO: ACL Check
            request.flash(_(u"This article is hidden"), False)

        if article.comments_enabled:
            form = EditCommentForm(request.form)
            if form.validate_on_submit():
                if form.data.get("comment_id", None):
                    comment = Comment.query.get(form.comment_id.data)
                    comment.text = form.text.data
                    request.flash(_(u"The comment was successfully edited"), True)
                else:
                    comment = Comment(text=form.text.data, author=request.user)
                    article.comments.append(comment)
                    Subscription.new(comment, "news.comment.new")
                    request.flash(_(u"Your comment was successfully created"), True)
                db.session.commit()
                return redirect_to(comment)
        else:
            form = EditCommentForm()

        # increase counters
        article.touch()

        comments = list(article.comments.options(db.joinedload("author")))
        Subscription.accessed(request.user, object=article, subject=article)

        return {"article": article, "comments": comments, "form": form}
Пример #2
0
    def detail(self, request, slug):
        article = Article.query.filter_by(slug=slug).one()
        if article.hidden:
            #TODO: ACL Check
            request.flash(_(u'This article is hidden'), False)

        if article.comments_enabled:
            form = EditCommentForm(request.form)
            if form.validate_on_submit():
                if form.data.get('comment_id', None):
                    comment = Comment.query.get(form.comment_id.data)
                    comment.text = form.text.data
                    request.flash(_(u'The comment was successfully edited'),
                                  True)
                else:
                    comment = Comment(text=form.text.data, author=request.user)
                    article.comments.append(comment)
                    Subscription.new(comment, 'news.comment.new')
                    request.flash(_(u'Your comment was successfully created'),
                                  True)
                db.session.commit()
                return redirect_to(comment)
        else:
            form = EditCommentForm()

        # increase counters
        article.touch()

        comments = list(article.comments.options(db.joinedload('author')))
        Subscription.accessed(request.user, object=article, subject=article)

        return {'article': article, 'comments': comments, 'form': form}
Пример #3
0
 def check_bad_implemented_type(self):
     """check for not well implemented subscription types"""
     w1 = Wrapper()
     db.session.commit()
     assert_raises(
         NotImplementedError,
         lambda: Subscription.new(Other(wrapper=w1), u'__test_new_other'))
Пример #4
0
    def test_subscription(self):
        one = self.data['User'][0]
        cat1, cat2 = self.data['TestSubscriptionCategory']
        eq_(Subscription.subscribe(one, u'__test_category', cat1), True)
        eq_(Subscription.subscribe(one, u'__test_category', cat1), False)
        eq_(Subscription.subscribe(one, u'__test_blog'), True)
        eq_(Subscription.subscribe(one, u'__test_blog'), False)

        s = Subscription.query.filter_by(user_id=one.id,
                                         type_name=u'__test_category',
                                         subject_id=cat1.id).one()
        eq_(s.type, CategorySubscriptionType)
        eq_(s.subject, cat1)
        s = Subscription.query.filter_by(user_id=one.id,
                                         type_name=u'__test_blog').one()
        eq_(s.type, BlogSubscriptionType)
        eq_(s.subject, None)
Пример #5
0
    def test_subscription(self):
        one = self.data['User'][0]
        cat1, cat2 = self.data['TestSubscriptionCategory']
        eq_(Subscription.subscribe(one, u'__test_category', cat1), True)
        eq_(Subscription.subscribe(one, u'__test_category', cat1), False)
        eq_(Subscription.subscribe(one, u'__test_blog'), True)
        eq_(Subscription.subscribe(one, u'__test_blog'), False)

        s = Subscription.query.filter_by(user_id=one.id,
                                         type_name=u'__test_category',
                                         subject_id=cat1.id).one()
        eq_(s.type, CategorySubscriptionType)
        eq_(s.subject, cat1)
        s = Subscription.query.filter_by(user_id=one.id,
                                         type_name=u'__test_blog').one()
        eq_(s.type, BlogSubscriptionType)
        eq_(s.subject, None)
Пример #6
0
    def test_sequent_subscriptions(self):
        """
        Test (with sequent mode) whether the subscription count and the unread
        information is accurate and whether notifications are sent correctly.
        """
        cat1, cat2 = self.data['TestSubscriptionCategory']
        one, two, three, four = self.data['User']
        e1, e2 = self.data['TestSubscriptionEntry'][:2]
        comments = self.data['TestSubscriptionComment']

        NotifyTrackerMixin.tracker = []

        Subscription.subscribe(three, u'__test_comments', e1)
        Subscription.subscribe(three, u'__test_comments', e2)
        Subscription.subscribe(four, u'__test_comments', e1)

        self._check_sequent_state(three, u'__test_comments', e1.id, None, 0)
        self._check_sequent_state(three, u'__test_comments', e2.id, None, 0)
        self._check_sequent_state(four, u'__test_comments', e1.id, None, 0)

        NotifyTrackerMixin.epoch = 1
        Subscription.new(comments[0], u'__test_new_comment')  # e1
        Subscription.new(comments[1], u'__test_new_comment')  # e2

        self._check_sequent_state(three, u'__test_comments', e1.id,
                                  comments[0].id, 1)
        self._check_sequent_state(three, u'__test_comments', e2.id,
                                  comments[1].id, 1)
        self._check_sequent_state(four, u'__test_comments', e1.id,
                                  comments[0].id, 1)

        Subscription.accessed(three, subject=e1)
        self._check_sequent_state(three, u'__test_comments', e1.id, None, 0)

        NotifyTrackerMixin.epoch = 2
        Subscription.new(comments[2], u'__test_new_comment')  # e1
        self._check_sequent_state(three, u'__test_comments', e1.id,
                                  comments[2].id, 1)

        NotifyTrackerMixin.epoch = 3
        Subscription.new(comments[3], u'__test_new_comment')  # e1
        self._check_sequent_state(three, u'__test_comments', e1.id,
                                  comments[2].id, 2)
        self._check_sequent_state(four, u'__test_comments', e1.id,
                                  comments[0].id, 3)

        Subscription.accessed(four, object=comments[3])
        self._check_sequent_state(four, u'__test_comments', e1.id, None, 0)

        NotifyTrackerMixin.epoch = 4
        Subscription.new(comments[4], u'__test_new_comment')  # e1
        Subscription.new(comments[5], u'__test_new_comment')  # e2
        self._check_sequent_state(three, u'__test_comments', e1.id,
                                  comments[2].id, 3)
        self._check_sequent_state(three, u'__test_comments', e2.id,
                                  comments[1].id, 2)
        self._check_sequent_state(four, u'__test_comments', e1.id,
                                  comments[4].id, 1)

        eq_(
            sorted(NotifyTrackerMixin.tracker),
            sorted([
                (1, u'__test_new_comment', three, comments[0], {
                    '__test_comments': [e1]
                }),
                (1, u'__test_new_comment', four, comments[0], {
                    '__test_comments': [e1]
                }),
                (1, u'__test_new_comment', three, comments[1], {
                    '__test_comments': [e2]
                }),
                # here three accesses entry 1
                (2, u'__test_new_comment', three, comments[2], {
                    '__test_comments': [e1]
                }),
                # here four accesses entry 1
                (4, u'__test_new_comment', four, comments[4], {
                    '__test_comments': [e1]
                }),
            ]))
Пример #7
0
    def test_multiple_multiple_subscriptions(self):
        """
        Test (with multiple mode and a SubscriptionType where there is more
        then one subject per object) whether the subscription count and the
        unread information is accurate and whether notifications are sent
        correctly.
        """
        entries = self.data['TestSubscriptionEntry']
        cat1, cat2 = self.data['TestSubscriptionCategory']
        one, two, three, four = self.data['User']
        t1, t2, t3 = self.data['TestSubscriptionTag']

        NotifyTrackerMixin.tracker = []

        Subscription.subscribe(three, u'__test_tag', t1)
        Subscription.subscribe(three, u'__test_tag', t2)
        Subscription.subscribe(three, u'__test_category', cat2)
        Subscription.subscribe(four, u'__test_tag', t3)

        self._check_multiple_state(three, u'__test_tag', t1.id, set(), 0)
        self._check_multiple_state(three, u'__test_tag', t2.id, set(), 0)
        self._check_multiple_state(four, u'__test_tag', t3.id, set(), 0)

        NotifyTrackerMixin.epoch = 1
        Subscription.new(entries[0], u'__test_new_entry')
        NotifyTrackerMixin.epoch = 2
        Subscription.new(entries[2], u'__test_new_entry')
        self._check_multiple_state(three, u'__test_tag', t1.id,
                                   set((entries[0].id, )), 1)
        self._check_multiple_state(three, u'__test_tag', t2.id,
                                   set((entries[0].id, )), 1)

        NotifyTrackerMixin.epoch = 3
        Subscription.new(entries[1], u'__test_new_entry')
        self._check_multiple_state(three, u'__test_tag', t2.id,
                                   set((entries[0].id, entries[1].id)), 2)

        self._check_multiple_state(three, u'__test_category', cat2.id,
                                   set((entries[1].id, )), 1)
        Subscription.accessed(three, object=entries[1])
        Subscription.accessed(three, object=entries[1])
        self._check_multiple_state(three, u'__test_category', cat2.id, set(),
                                   0)

        NotifyTrackerMixin.epoch = 4
        Subscription.new(entries[3], u'__test_new_entry')
        self._check_multiple_state(three, u'__test_tag', t1.id,
                                   set((entries[0].id, )), 1)
        self._check_multiple_state(three, u'__test_tag', t2.id,
                                   set((entries[0].id, entries[3].id)), 2)
        self._check_multiple_state(four, u'__test_tag', t3.id,
                                   set((entries[3].id, )), 1)

        eq_(
            sorted(NotifyTrackerMixin.tracker),
            sorted([
                (1, u'__test_new_entry', three, entries[0], {
                    '__test_tag': [t1, t2]
                }),
                (3, u'__test_new_entry', three, entries[1], {
                    '__test_tag': [t2],
                    '__test_category': [cat2]
                }),
                (4, u'__test_new_entry', three, entries[3], {
                    '__test_tag': [t2]
                }),
                (4, u'__test_new_entry', four, entries[3], {
                    '__test_tag': [t3]
                }),
            ]))
Пример #8
0
    def test_multiple_subscriptions(self):
        """
        Test (with multiple mode) whether the subscription count and the unread
        information is accurate and whether notifications are sent correctly.
        """
        cat1, cat2 = self.data['TestSubscriptionCategory']
        one, two, three, four = self.data['User']
        entries = self.data['TestSubscriptionEntry']
        NotifyTrackerMixin.tracker = []

        #: ensure this does not fail but passes silently
        Subscription.accessed(one, object=entries[0])

        Subscription.subscribe(one, u'__test_category', cat1)
        Subscription.subscribe(two, u'__test_blog')

        NotifyTrackerMixin.epoch = 1
        Subscription.new(entries[0], u'__test_new_entry')
        Subscription.new(entries[1], u'__test_new_entry')
        self._check_multiple_state(one, u'__test_category', cat1.id,
                                   set((entries[0].id, )), 1)
        self._check_multiple_state(two, u'__test_blog', None,
                                   set((entries[0].id, entries[1].id)), 2)

        NotifyTrackerMixin.epoch = 2
        Subscription.new(entries[2], u'__test_new_entry')
        Subscription.new(entries[3], u'__test_new_entry')
        self._check_multiple_state(
            one, u'__test_category', cat1.id,
            set((entries[0].id, entries[2].id, entries[3].id)), 3)
        self._check_multiple_state(
            two, u'__test_blog', None,
            set((entries[0].id, entries[1].id, entries[2].id, entries[3].id)),
            4)

        Subscription.accessed(two, object=entries[2])
        Subscription.accessed(two, object=entries[1])
        self._check_multiple_state(two, u'__test_blog', None,
                                   set((entries[0].id, entries[3].id)), 2)

        Subscription.accessed(one, subject=cat1)
        Subscription.accessed(one,
                              subject=cat1)  # must work also if not unread
        self._check_multiple_state(one, u'__test_category', cat1.id, set(), 0)

        eq_(
            sorted(NotifyTrackerMixin.tracker),
            sorted([
                (1, u'__test_new_entry', one, entries[0], {
                    '__test_category': [cat1]
                }),
                (1, u'__test_new_entry', two, entries[0], {
                    '__test_blog': [None]
                }),
                (1, u'__test_new_entry', two, entries[1], {
                    '__test_blog': [None]
                }),
                (2, u'__test_new_entry', one, entries[2], {
                    '__test_category': [cat1]
                }),
                (2, u'__test_new_entry', one, entries[3], {
                    '__test_category': [cat1]
                }),
                (2, u'__test_new_entry', two, entries[2], {
                    '__test_blog': [None]
                }),
                (2, u'__test_new_entry', two, entries[3], {
                    '__test_blog': [None]
                }),
            ]))
Пример #9
0
 def check_bad_implemented_type(self):
     """check for not well implemented subscription types"""
     w1 = Wrapper()
     db.session.commit()
     assert_raises(NotImplementedError,
         lambda: Subscription.new(Other(wrapper=w1), u'__test_new_other'))
Пример #10
0
    def test_sequent_subscriptions(self):
        """
        Test (with sequent mode) whether the subscription count and the unread
        information is accurate and whether notifications are sent correctly.
        """
        cat1, cat2 = self.data['TestSubscriptionCategory']
        one, two, three, four = self.data['User']
        e1, e2 = self.data['TestSubscriptionEntry'][:2]
        comments = self.data['TestSubscriptionComment']

        NotifyTrackerMixin.tracker = []

        Subscription.subscribe(three, u'__test_comments', e1)
        Subscription.subscribe(three, u'__test_comments', e2)
        Subscription.subscribe(four, u'__test_comments', e1)

        self._check_sequent_state(three, u'__test_comments', e1.id,
                                  None, 0)
        self._check_sequent_state(three, u'__test_comments', e2.id,
                                  None, 0)
        self._check_sequent_state(four, u'__test_comments', e1.id,
                                  None, 0)

        NotifyTrackerMixin.epoch = 1
        Subscription.new(comments[0], u'__test_new_comment') # e1
        Subscription.new(comments[1], u'__test_new_comment') # e2

        self._check_sequent_state(three, u'__test_comments', e1.id,
                                  comments[0].id, 1)
        self._check_sequent_state(three, u'__test_comments', e2.id,
                                  comments[1].id, 1)
        self._check_sequent_state(four, u'__test_comments', e1.id,
                                  comments[0].id, 1)

        Subscription.accessed(three, subject=e1)
        self._check_sequent_state(three, u'__test_comments', e1.id,
                                  None, 0)

        NotifyTrackerMixin.epoch = 2
        Subscription.new(comments[2], u'__test_new_comment') # e1
        self._check_sequent_state(three, u'__test_comments', e1.id,
                             comments[2].id, 1)

        NotifyTrackerMixin.epoch = 3
        Subscription.new(comments[3], u'__test_new_comment') # e1
        self._check_sequent_state(three, u'__test_comments', e1.id,
                             comments[2].id, 2)
        self._check_sequent_state(four, u'__test_comments', e1.id,
                             comments[0].id, 3)

        Subscription.accessed(four, object=comments[3])
        self._check_sequent_state(four, u'__test_comments', e1.id,
                             None, 0)

        NotifyTrackerMixin.epoch = 4
        Subscription.new(comments[4], u'__test_new_comment') # e1
        Subscription.new(comments[5], u'__test_new_comment') # e2
        self._check_sequent_state(three, u'__test_comments', e1.id,
                             comments[2].id, 3)
        self._check_sequent_state(three, u'__test_comments', e2.id,
                             comments[1].id, 2)
        self._check_sequent_state(four, u'__test_comments', e1.id,
                             comments[4].id, 1)

        eq_(sorted(NotifyTrackerMixin.tracker), sorted([
            (1, u'__test_new_comment', three, comments[0], {'__test_comments': [e1]}),
            (1, u'__test_new_comment', four, comments[0], {'__test_comments': [e1]}),
            (1, u'__test_new_comment', three, comments[1], {'__test_comments': [e2]}),
            # here three accesses entry 1
            (2, u'__test_new_comment', three, comments[2], {'__test_comments': [e1]}),
            # here four accesses entry 1
            (4, u'__test_new_comment', four, comments[4], {'__test_comments': [e1]}),
        ]))
Пример #11
0
    def test_multiple_multiple_subscriptions(self):
        """
        Test (with multiple mode and a SubscriptionType where there is more
        then one subject per object) whether the subscription count and the
        unread information is accurate and whether notifications are sent
        correctly.
        """
        entries = self.data['TestSubscriptionEntry']
        cat1, cat2 = self.data['TestSubscriptionCategory']
        one, two, three, four = self.data['User']
        t1, t2, t3 = self.data['TestSubscriptionTag']

        NotifyTrackerMixin.tracker = []

        Subscription.subscribe(three, u'__test_tag', t1)
        Subscription.subscribe(three, u'__test_tag', t2)
        Subscription.subscribe(three, u'__test_category', cat2)
        Subscription.subscribe(four, u'__test_tag', t3)

        self._check_multiple_state(three, u'__test_tag', t1.id,
                                   set(), 0)
        self._check_multiple_state(three, u'__test_tag', t2.id,
                                   set(), 0)
        self._check_multiple_state(four, u'__test_tag', t3.id,
                                   set(), 0)

        NotifyTrackerMixin.epoch = 1
        Subscription.new(entries[0], u'__test_new_entry')
        NotifyTrackerMixin.epoch = 2
        Subscription.new(entries[2], u'__test_new_entry')
        self._check_multiple_state(three, u'__test_tag', t1.id,
                                   set((entries[0].id,)), 1)
        self._check_multiple_state(three, u'__test_tag', t2.id,
                                   set((entries[0].id,)), 1)

        NotifyTrackerMixin.epoch = 3
        Subscription.new(entries[1], u'__test_new_entry')
        self._check_multiple_state(three, u'__test_tag', t2.id,
                                   set((entries[0].id, entries[1].id)), 2)

        self._check_multiple_state(three, u'__test_category', cat2.id,
                                   set((entries[1].id,)), 1)
        Subscription.accessed(three, object=entries[1])
        Subscription.accessed(three, object=entries[1])
        self._check_multiple_state(three, u'__test_category', cat2.id,
                                   set(), 0)

        NotifyTrackerMixin.epoch = 4
        Subscription.new(entries[3], u'__test_new_entry')
        self._check_multiple_state(three, u'__test_tag', t1.id,
                                   set((entries[0].id,)), 1)
        self._check_multiple_state(three, u'__test_tag', t2.id,
                                   set((entries[0].id, entries[3].id)), 2)
        self._check_multiple_state(four, u'__test_tag', t3.id,
                                   set((entries[3].id,)), 1)

        eq_(sorted(NotifyTrackerMixin.tracker), sorted([
            (1, u'__test_new_entry', three, entries[0], {'__test_tag': [t1, t2]}),
            (3, u'__test_new_entry', three, entries[1], {'__test_tag': [t2],
                                                '__test_category': [cat2]}),
            (4, u'__test_new_entry', three, entries[3], {'__test_tag': [t2]}),
            (4, u'__test_new_entry', four, entries[3], {'__test_tag': [t3]}),
        ]))
Пример #12
0
    def test_multiple_subscriptions(self):
        """
        Test (with multiple mode) whether the subscription count and the unread
        information is accurate and whether notifications are sent correctly.
        """
        cat1, cat2 = self.data['TestSubscriptionCategory']
        one, two, three, four = self.data['User']
        entries = self.data['TestSubscriptionEntry']
        NotifyTrackerMixin.tracker = []

        #: ensure this does not fail but passes silently
        Subscription.accessed(one, object=entries[0])

        Subscription.subscribe(one, u'__test_category', cat1)
        Subscription.subscribe(two, u'__test_blog')

        NotifyTrackerMixin.epoch = 1
        Subscription.new(entries[0], u'__test_new_entry')
        Subscription.new(entries[1], u'__test_new_entry')
        self._check_multiple_state(one, u'__test_category', cat1.id,
                              set((entries[0].id,)), 1)
        self._check_multiple_state(two, u'__test_blog', None,
                              set((entries[0].id, entries[1].id)), 2)

        NotifyTrackerMixin.epoch = 2
        Subscription.new(entries[2], u'__test_new_entry')
        Subscription.new(entries[3], u'__test_new_entry')
        self._check_multiple_state(one, u'__test_category', cat1.id,
                              set((entries[0].id, entries[2].id, entries[3].id)), 3)
        self._check_multiple_state(two, u'__test_blog', None,
                              set((entries[0].id, entries[1].id,
                                   entries[2].id, entries[3].id)), 4)

        Subscription.accessed(two, object=entries[2])
        Subscription.accessed(two, object=entries[1])
        self._check_multiple_state(two, u'__test_blog', None,
                              set((entries[0].id, entries[3].id)), 2)

        Subscription.accessed(one, subject=cat1)
        Subscription.accessed(one, subject=cat1) # must work also if not unread
        self._check_multiple_state(one, u'__test_category', cat1.id,
                              set(), 0)

        eq_(sorted(NotifyTrackerMixin.tracker), sorted([
            (1, u'__test_new_entry', one, entries[0], {'__test_category': [cat1]}),
            (1, u'__test_new_entry', two, entries[0], {'__test_blog': [None]}),
            (1, u'__test_new_entry', two, entries[1], {'__test_blog': [None]}),
            (2, u'__test_new_entry', one, entries[2], {'__test_category': [cat1]}),
            (2, u'__test_new_entry', one, entries[3], {'__test_category': [cat1]}),
            (2, u'__test_new_entry', two, entries[2], {'__test_blog': [None]}),
            (2, u'__test_new_entry', two, entries[3], {'__test_blog': [None]}),
        ]))