591
def get_structural_subscribers(
592
bug_or_bugtask, recipients, level, direct_subscribers=None):
593
"""Return subscribers for bug or bugtask at level.
596
:param recipients: a BugNotificationRecipients object or None.
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.
602
Excludes structural subscriptions for people who are directly subscribed
604
if IBug.providedBy(bug_or_bugtask):
606
bugtasks = bug.bugtasks
607
elif IBugTask.providedBy(bug_or_bugtask):
608
bug = bug_or_bugtask.bug
609
bugtasks = [bug_or_bugtask]
611
raise ValueError('First argument must be bug or bugtask')
592
def query_structural_subscriptions(
593
what, bug, bugtasks, level, exclude=None):
594
"""Query into structural subscriptions for a given bug.
596
:param what: The fields to fetch. Choose from `Person`,
597
`StructuralSubscription`, `BugSubscriptionFilter`, or a combo.
598
:param bug: An `IBug`
599
:param bugtasks: An iterable of `IBugTask`.
600
:param level: A level from `BugNotificationLevel`. Filters below this
601
level will be excluded.
602
:param exclude: `Person`s to exclude (e.g. direct subscribers).
604
from lp.registry.model.person import Person # Circular.
612
605
filter_id_query = (
613
606
_get_structural_subscription_filter_id_query(
614
bug, bugtasks, level, direct_subscribers))
607
bug, bugtasks, level, exclude))
615
608
if not filter_id_query:
616
609
return EmptyResultSet()
617
# This is here because of a circular import.
618
from lp.registry.model.person import Person
619
610
source = IStore(StructuralSubscription).using(
620
611
StructuralSubscription,
621
612
Join(BugSubscriptionFilter,
622
613
BugSubscriptionFilter.structural_subscription_id ==
623
614
StructuralSubscription.id),
625
Person.id == StructuralSubscription.subscriberID),
616
Person.id == StructuralSubscription.subscriberID))
618
BugSubscriptionFilter.id, filter_id_query)
619
return source.find(what, conditions)
622
def get_bug_and_bugtasks(bug_or_bugtask):
623
"""Return a bug and a list of bugtasks given a bug or a bugtask.
625
:param bug_or_bugtask: An `IBug` or `IBugTask`.
626
:raises ValueError: If `bug_or_bugtask` does not provide `IBug` or
629
if IBug.providedBy(bug_or_bugtask):
630
return bug_or_bugtask, bug_or_bugtask.bugtasks
631
elif IBugTask.providedBy(bug_or_bugtask):
632
return bug_or_bugtask.bug, [bug_or_bugtask]
635
"Expected bug or bugtask, got %r" % (bug_or_bugtask,))
638
def get_structural_subscriptions(bug_or_bugtask, level, exclude=None):
639
"""Return subscriptions for bug or bugtask at level.
641
:param bug_or_bugtask: An `IBug` or `IBugTask`.
642
:param level: A level from `BugNotificationLevel`. Filters below this
643
level will be excluded.
644
:param exclude: `Person`s to exclude (e.g. direct subscribers).
646
from lp.registry.model.person import Person # Circular.
647
bug, bugtasks = get_bug_and_bugtasks(bug_or_bugtask)
648
subscriptions = query_structural_subscriptions(
649
StructuralSubscription, bug, bugtasks, level, exclude)
650
# Return only the first subscription and filter per subscriber.
651
subscriptions.config(distinct=(Person.id,))
652
subscriptions.order_by(
653
Person.id, StructuralSubscription.id,
654
BugSubscriptionFilter.id)
658
def get_structural_subscribers(
659
bug_or_bugtask, recipients, level, exclude=None):
660
"""Return subscribers for bug or bugtask at level.
662
:param bug_or_bugtask: An `IBug` or `IBugTask`.
663
:param recipients: A `BugNotificationRecipients` object or
664
`None`, which will be populated if provided.
665
:param level: A level from `BugNotificationLevel`. Filters below this
666
level will be excluded.
667
:param exclude: `Person`s to exclude (e.g. direct subscribers).
669
from lp.registry.model.person import Person # Circular.
670
bug, bugtasks = get_bug_and_bugtasks(bug_or_bugtask)
627
671
if recipients is None:
630
In(BugSubscriptionFilter.id,
631
filter_id_query)).config(distinct=True).order_by()
672
subscribers = query_structural_subscriptions(
673
Person, bug, bugtasks, level, exclude)
674
subscribers.config(distinct=True)
675
return subscribers.order_by()
677
results = query_structural_subscriptions(
678
(Person, StructuralSubscription, BugSubscriptionFilter),
679
bug, bugtasks, level, exclude)
634
query_results = source.find(
635
(Person, StructuralSubscription, BugSubscriptionFilter),
636
In(BugSubscriptionFilter.id, filter_id_query))
637
for person, subscription, filter in query_results:
681
for person, subscription, filter in results:
639
682
if person not in recipients:
640
683
subscribers.append(person)
641
684
recipients.addStructuralSubscriber(