def test_subscriptiontype(self):
     eq_(SubscriptionType.by_name(u'__test_comments'), CommentsSubscriptionType)
     eq_(SubscriptionType.by_object_type(TestSubscriptionComment), [CommentsSubscriptionType])
     eq_(SubscriptionType.by_subject_type(TestSubscriptionCategory), [CategorySubscriptionType])
     eq_(sorted(SubscriptionType.by_action(NewEntrySubscriptionAction)),
         sorted([CategorySubscriptionType, BlogSubscriptionType, TagSubscriptionType]))
     eq_(SubscriptionType.by_action(u'__test_new_comment'), [CommentsSubscriptionType])
示例#2
0
    def accessed(user, **kwargs):
        """
        Mark subscriptions as read.
        This means to remove objects from the `unread_objects` or unset
        `first_unread_object` (depending on the type's mode) and decrement the
        unread count.

        Must be called whenever an object is accessed.

        :param object: Mark all subscriptions with this object as read.
        :param subject: Mark all subscriptions with this subject as read.
        """
        # enforce usage of keywords
        object = kwargs.pop('object', None)
        subject = kwargs.pop('subject', None)
        assert not kwargs, 'Invalid Arguments %r' % kwargs.keys()

        if object is not None:
            for t in SubscriptionType.by_object_type(type(object)):
                subjects = t.get_subjects(object)
                if not subjects:
                    continue
                subject_ids = [getattr(s, 'id', s) for s in subjects]

                cond = ((Subscription.type_name == t.name) &
                        (Subscription.user == user))
                if t.subject_type is not None:
                    cond &= Subscription.subject_id.in_(subject_ids)
                subscriptions = Subscription.query.filter(cond).all()
                for s in subscriptions:
                    if t.mode == 'sequent':
                        s.first_unread_object_id = None
                        s.count = 0
                    elif t.mode == 'multiple':
                        try:
                            s.unread_object_ids.remove(object.id)
                        except KeyError:
                            pass
                        else:
                            s.count -= 1
                db.session.commit()

        if subject is not None:
            for t in SubscriptionType.by_subject_type(type(subject)):
                try:
                    s = Subscription.query.filter_by(type_name=t.name,
                                                     subject_id=subject.id,
                                                     user=user).one()
                except db.NoResultFound:
                    continue

                s.count = 0
                if t.mode == 'sequent':
                    s.first_unread_object_id = None
                elif t.mode == 'multiple':
                    s.unread_object_ids = []
                db.session.commit()
示例#3
0
 def test_subscriptiontype(self):
     eq_(SubscriptionType.by_name(u'__test_comments'),
         CommentsSubscriptionType)
     eq_(SubscriptionType.by_object_type(TestSubscriptionComment),
         [CommentsSubscriptionType])
     eq_(SubscriptionType.by_subject_type(TestSubscriptionCategory),
         [CategorySubscriptionType])
     eq_(
         sorted(SubscriptionType.by_action(NewEntrySubscriptionAction)),
         sorted([
             CategorySubscriptionType, BlogSubscriptionType,
             TagSubscriptionType
         ]))
     eq_(SubscriptionType.by_action(u'__test_new_comment'),
         [CommentsSubscriptionType])