示例#1
0
    def subscribe(user, type_, subject=None):
        """
        Safely subscribe a user to a subject.
        Returns False if the Subscription already exists, else True.

        :param user: A user object or a user id.
        :param type_: The subscription type to be used. May be a subclass of
                      :class:`SubscriptionType` or a name of a subscription
                      type.
        :param subject: The subject to be used (id or instance). May be None
                        if the type has not ``subject_type``.
        """
        if isinstance(type_, basestring):
            type_ = SubscriptionType.by_name(type_)

        subject_type = type_ and type_.subject_type or type(None)
        if not isinstance(subject, subject_type):
            raise ValueError('subject (%r) does not match the subject_type '
                             '(%r) of given SubscriptionType' %
                             (subject, subject_type))

        subject_id = None if subject is None else subject.id
        args = {
            'user': user,
            'type_name': type_.name,
            'subject_id': subject_id,
        }

        if Subscription.query.filter_by(**args).count():
            return False

        Subscription(**args)
        db.session.commit()
        return True
 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])
示例#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])
示例#4
0
    def unsubscribe(user_or_subscription, type_=None, subject=None):
        """
        Safely unsubscribe a user from a subject.
        Returns False if the Subscription did not exist, else True.

        :param user_or_subscription: A user object or a user id.
                                     May also be a Subscription object, in
                                     this case the other parameters must not
                                     be given.
        :param type_: The subscription type to be used. May be a subclass of
                      :class:`SubscriptionType` or a name of a subscription
                      type.
        :param subject: The subject to be used (id or instance). May be None
                        if the type has not ``subject_type``.
        """
        if isinstance(user_or_subscription, Subscription):
            assert type_ is None and subject is None
            db.session.delete(user_or_subscription)
            db.session.commit()
            return True
        else:
            user = user_or_subscription

        if isinstance(type_, basestring):
            type_ = SubscriptionType.by_name(type_)

        s = Subscription.query.filter_by(user=user,
                                         type_name=type_.name,
                                         subject_id=getattr(
                                             subject, 'id', subject)).all()

        if not len(s):
            return False
        if len(s) > 1:
            raise ValueError('Duplicate found!')

        db.session.delete(s[0])
        db.session.commit()
        return True
示例#5
0
 def type(self):
     return SubscriptionType.by_name(self.type_name)