~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/bugs/model/structuralsubscription.py

  • Committer: Gavin Panella
  • Date: 2011-12-16 21:59:40 UTC
  • mto: This revision was merged to the branch mainline in revision 14638.
  • Revision ID: gavin.panella@canonical.com-20111216215940-3zpdu9h6kes62i1x
Split get_structural_subscribers() into get_structural_subscribers() and query_structural_subscriptions().

Show diffs side-by-side

added added

removed removed

Lines of Context:
588
588
        *conditions)
589
589
 
590
590
 
 
591
def query_structural_subscriptions(
 
592
    what, bug, bugtasks, level, exclude=None):
 
593
    """Query into structural subscriptions for a given bug.
 
594
 
 
595
    :param what: The fields to fetch. Choose from `Person`,
 
596
        `StructuralSubscription`, `BugSubscriptionFilter`, or a combo.
 
597
    :param bug: An `IBug`
 
598
    :param bugtasks: An iterable of `IBugTask`.
 
599
    :param level: A level from `BugNotificationLevel`.
 
600
    :param exclude: `Person`s to exclude (e.g. direct subscribers).
 
601
    """
 
602
    filter_id_query = (
 
603
        _get_structural_subscription_filter_id_query(
 
604
            bug, bugtasks, level, exclude))
 
605
    if not filter_id_query:
 
606
        return EmptyResultSet()
 
607
    from lp.registry.model.person import Person  # Circular.
 
608
    source = IStore(StructuralSubscription).using(
 
609
        StructuralSubscription,
 
610
        Join(BugSubscriptionFilter,
 
611
             BugSubscriptionFilter.structural_subscription_id ==
 
612
             StructuralSubscription.id),
 
613
        Join(Person,
 
614
             Person.id == StructuralSubscription.subscriberID))
 
615
    conditions = In(
 
616
        BugSubscriptionFilter.id, filter_id_query)
 
617
    return source.find(what, conditions)
 
618
 
 
619
 
591
620
def get_structural_subscribers(
592
621
    bug_or_bugtask, recipients, level, direct_subscribers=None):
593
622
    """Return subscribers for bug or bugtask at level.
594
623
 
595
 
    :param bug: a bug.
596
 
    :param recipients: a BugNotificationRecipients object or None.
597
 
                       Populates if given.
598
 
    :param level: a level from lp.bugs.enum.BugNotificationLevel.
599
 
    :param direct_subscribers: a collection of Person objects who are
600
 
                               directly subscribed to the bug.
 
624
    :param bug_or_bugtask: An `IBug` or `IBugTask`.
 
625
    :param recipients: A `BugNotificationRecipients` object or
 
626
        `None`, which will be populated if provided.
 
627
    :param level: A level from `BugNotificationLevel`.
 
628
    :param exclude: `Person`s to exclude (e.g. direct subscribers).
 
629
    """
 
630
    from lp.registry.model.person import Person  # Circular.
601
631
 
602
 
    Excludes structural subscriptions for people who are directly subscribed
603
 
    to the bug."""
604
632
    if IBug.providedBy(bug_or_bugtask):
605
633
        bug = bug_or_bugtask
606
634
        bugtasks = bug.bugtasks
609
637
        bugtasks = [bug_or_bugtask]
610
638
    else:
611
639
        raise ValueError('First argument must be bug or bugtask')
612
 
    filter_id_query = (
613
 
        _get_structural_subscription_filter_id_query(
614
 
            bug, bugtasks, level, direct_subscribers))
615
 
    if not filter_id_query:
616
 
        return EmptyResultSet()
617
 
    # This is here because of a circular import.
618
 
    from lp.registry.model.person import Person
619
 
    source = IStore(StructuralSubscription).using(
620
 
        StructuralSubscription,
621
 
        Join(BugSubscriptionFilter,
622
 
             BugSubscriptionFilter.structural_subscription_id ==
623
 
             StructuralSubscription.id),
624
 
        Join(Person,
625
 
             Person.id == StructuralSubscription.subscriberID),
626
 
        )
 
640
 
627
641
    if recipients is None:
628
 
        return source.find(
629
 
            Person,
630
 
            In(BugSubscriptionFilter.id,
631
 
               filter_id_query)).config(distinct=True).order_by()
 
642
        subscribers = query_structural_subscriptions(
 
643
            Person, bug, bugtasks, level, direct_subscribers)
 
644
        subscribers.config(distinct=True)
 
645
        return subscribers.order_by()
632
646
    else:
 
647
        results = query_structural_subscriptions(
 
648
            (Person, StructuralSubscription, BugSubscriptionFilter),
 
649
            bug, bugtasks, level, direct_subscribers)
633
650
        subscribers = []
634
 
        query_results = source.find(
635
 
            (Person, StructuralSubscription, BugSubscriptionFilter),
636
 
            In(BugSubscriptionFilter.id, filter_id_query))
637
 
        for person, subscription, filter in query_results:
638
 
            # Set up results.
 
651
        for person, subscription, filter in results:
639
652
            if person not in recipients:
640
653
                subscribers.append(person)
641
654
                recipients.addStructuralSubscriber(