52
52
from canonical.launchpad.helpers import (
53
53
get_contact_email_addresses,
55
from lp.app.errors import UserCannotUnsubscribePerson
55
from lp.services.propertycache import (
56
59
from lp.blueprints.adapters import SpecificationDelta
57
60
from lp.blueprints.enums import (
58
61
NewSpecificationDefinitionStatus,
90
93
from lp.registry.interfaces.person import validate_public_person
91
94
from lp.registry.interfaces.productseries import IProductSeries
92
95
from lp.registry.interfaces.product import IProduct
93
from lp.services.propertycache import (
100
98
def recursive_blocked_query(spec):
223
221
def subscriptions(self):
224
222
"""Sort the subscriptions"""
225
from lp.registry.model.person import person_sort_key
227
self._subscriptions, key=lambda sub: person_sort_key(sub.person))
224
self._subscriptions, key=lambda sub: sub.person.displayname)
230
227
def target(self):
540
def subscribe(self, person, subscribed_by=None, essential=False):
541
"""See ISpecification."""
542
if subscribed_by is None:
543
subscribed_by = person
544
# Create or modify a user's subscription to this blueprint.
545
# First see if a relevant subscription exists, and if so, return it
537
def subscribe(self, person, user, essential):
538
"""Create or modify a user's subscription to this blueprint."""
539
# first see if a relevant subscription exists, and if so, return it
546
540
sub = self.subscription(person)
547
541
if sub is not None:
548
542
if sub.essential != essential:
556
550
# that we can get away with not examining the attribute
557
551
# at all - it's a boolean!
558
552
notify(ObjectModifiedEvent(
559
sub, sub, ['essential'], user=subscribed_by))
553
sub, sub, ['essential'], user=user))
561
555
# since no previous subscription existed, create and return a new one
562
556
sub = SpecificationSubscription(specification=self,
563
557
person=person, essential=essential)
564
558
property_cache = get_property_cache(self)
565
559
if 'subscription' in property_cache:
566
from lp.registry.model.person import person_sort_key
567
560
property_cache.subscriptions.append(sub)
568
561
property_cache.subscriptions.sort(
569
key=lambda sub: person_sort_key(sub.person))
570
notify(ObjectCreatedEvent(sub, user=subscribed_by))
562
key=lambda sub: sub.person.displayname)
563
notify(ObjectCreatedEvent(sub, user=user))
573
def unsubscribe(self, person, unsubscribed_by):
566
def unsubscribe(self, person):
574
567
"""See ISpecification."""
575
568
# see if a relevant subscription exists, and if so, delete it
577
person = unsubscribed_by
578
569
for sub in self.subscriptions:
579
570
if sub.person.id == person.id:
580
if not sub.canBeUnsubscribedByUser(unsubscribed_by):
581
raise UserCannotUnsubscribePerson(
582
'%s does not have permission to unsubscribe %s.' % (
583
unsubscribed_by.displayname,
585
571
get_property_cache(self).subscriptions.remove(sub)
586
572
SpecificationSubscription.delete(sub.id)